1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include "unalf.h"
/* 20251104 bkw: implement the simple checksum used by ALF.
Dumbest possible algo: all the bytes are added together and
the bottom 16 bits of the result is the checksum. */
void alfsum(const char *file, FILE *f) {
int c;
unsigned long sum = 0;
while((c = fgetc(f)) != EOF)
sum += c;
printf("%04x\t%s\n", (unsigned int)(sum & 0xffff), file);
}
int main(int argc, char **argv) {
int errs = 0;
char *file;
FILE *f;
set_self(argv[0]);
if(argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
printf("alfsum v" VERSION " by B. Watson. WTFPL.\n"
"Usage: %s filename [filename(s) ...]\n"
"\t(use - to read from standard input)\n",
self);
return (argc < 2) ? -1 : 0;
}
if(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
puts(VERSION);
exit(0);
}
while((file = *++argv)) {
if(argv[0][0] == '-' && !argv[0][1]) {
if(isatty(0))
fprintf(stderr, "%s: reading from stdin...\n", self);
f = stdin;
file = " (stdin)";
} else if(!(f = fopen(file, "rb"))) {
fprintf(stderr, "%s: ", self);
perror(file);
errs++;
continue;
}
alfsum(file, f);
fclose(f);
}
return errs > 254 ? 254 : errs;
}
|