From e2da2bffe58a76c091d3496bd3ca2d2f18ea2eb6 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Thu, 13 Nov 2025 05:39:38 -0500 Subject: initial commit --- src/alfsum.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/alfsum.c (limited to 'src/alfsum.c') diff --git a/src/alfsum.c b/src/alfsum.c new file mode 100644 index 0000000..7f30c7c --- /dev/null +++ b/src/alfsum.c @@ -0,0 +1,53 @@ +#include +#include + +/* 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. */ + +char *self; + +void alfsum(const char *file, FILE *f) { + int c; + unsigned long sum = 0; + + while((c = fgetc(f)) != EOF) + sum += c; + + printf("%8s\t%04x\n", file, (unsigned int)(sum & 0xffff)); +} + +int main(int argc, char **argv) { + int errs = 0; + char *file; + FILE *f; + + self = argv[0]; + + /* if the first arg is a - followed by anything at all, assume --help */ + if(argc < 2 || (argc == 2 && argv[1][0] == '-' && argv[1][1])) { + fprintf(stderr, + "usage: %s filename [filename ...]\n" + "\t(use - to read from standard input)\n", + self); + return -1; + } + + 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; +} -- cgit v1.2.3