7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
0
|
// sum.c, version 1.0.1 |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
1
|
// OMKOV coreutils sum |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
4
|
|
b181413 |
Jamozed |
2022-02-05 22:32:00 |
5
|
#include "util/error.h" |
b181413 |
Jamozed |
2022-02-05 22:32:00 |
6
|
#include "util/optget.h" |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
7
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
8
|
#include <stdbool.h> |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
9
|
#include <stdint.h> |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
10
|
#include <stdio.h> |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
11
|
|
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
12
|
#define VERSION "1.0.1" |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
13
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
14
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
20
|
static inline int sum(const char *file); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
21
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
22
|
static inline uint32_t bsd(FILE *fi, size_t *fl); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
23
|
static inline uint32_t sysv(FILE *fi, size_t *fl); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
24
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
25
|
static uint32_t (*fn)(FILE *, size_t *) = bsd; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
26
|
|
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
27
|
static void hlp(void); |
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
28
|
static void ver(void); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
29
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
30
|
int main(int ac, char *av[]) { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
31
|
struct opt opt = OPTGET_INIT; opt.str = "rs"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
32
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
33
|
case 'r': { fn = bsd; break; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
34
|
case 's': { fn = sysv; break; } |
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
35
|
case 256: { hlp(); return 0; } |
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
36
|
case 257: { ver(); return 0; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
37
|
default: { return 1; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
38
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
39
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
40
|
bool warned = false; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
41
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
42
|
if (opt.ind == ac) { sum(NULL); } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
43
|
else for (char **p = &av[opt.ind]; *p; ++p) if (sum(*p)) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
44
|
warn("%s: %s", *p, serr()); warned = true; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
45
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
46
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
47
|
return warned; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
48
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
49
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
50
|
static inline int sum(const char *file) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
51
|
FILE *fi; size_t bl = 0; register uint32_t sum; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
52
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
53
|
if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
54
|
else if (!(fi = fopen(file, "r"))) { return 1; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
55
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
56
|
sum = fn(fi, &bl); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
57
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
58
|
printf("%u %zu", sum, bl); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
59
|
if (file) { printf(" %s", file); } fputc('\n', stdout); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
60
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
61
|
if (fi != stdin) { fclose(fi); } return 0; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
62
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
63
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
64
|
static inline uint32_t bsd(FILE *fi, size_t *bl) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
65
|
uint8_t buf[BUFSIZ * 16]; register uint16_t sum = 0; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
66
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
67
|
for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); *bl += c) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
68
|
for (register size_t i = 0; i != c; ++i) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
69
|
sum = (sum >> 1) + ((sum & 1) << 15); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
70
|
sum += buf[i]; sum &= 0xFFFF; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
71
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
72
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
73
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
74
|
*bl = *bl / 1024 + 1; return sum; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
75
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
76
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
77
|
static inline uint32_t sysv(FILE *fi, size_t *bl) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
78
|
uint8_t buf[BUFSIZ * 16]; register uint32_t sum = 0; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
79
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
80
|
for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); *bl += c) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
81
|
for (register size_t i = 0; i != c; ++i) { sum += buf[i]; } |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
82
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
83
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
84
|
sum = (sum & 0xFFFF) + ((sum & 0xFFFFFFFF) >> 16); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
85
|
sum = (sum & 0xFFFF) + (sum >> 16); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
86
|
|
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
87
|
*bl = *bl / 512 + 1; return sum; |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
88
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
89
|
|
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
90
|
static void hlp(void) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
91
|
puts("sum - write file checksums and block counts\n"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
92
|
puts("usage: sum [-rs] [file...]\n"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
93
|
puts("options:"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
94
|
puts(" -r Use BSD algorithm and 1024 byte blocks"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
95
|
puts(" -s Use System V algorithm and 512 byte blocks"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
96
|
puts(" --help Display help information"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
97
|
puts(" --version Display version information"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
98
|
} |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
99
|
|
7c8ef26 |
Jamozed |
2020-08-14 01:20:07 |
100
|
static void ver(void) { |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
101
|
puts("OMKOV coreutils sum, version " VERSION); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
102
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
103
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
5e04987 |
Jamozed |
2020-07-26 21:28:20 |
104
|
} |
|
|
|
105
|
|