#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; }