aboutsummaryrefslogtreecommitdiff
path: root/src/alfsum.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2025-11-13 05:39:38 -0500
committerB. Watson <urchlay@slackware.uk>2025-11-13 05:39:38 -0500
commite2da2bffe58a76c091d3496bd3ca2d2f18ea2eb6 (patch)
tree5195b221457842d781fadcb94331c93058046744 /src/alfsum.c
downloadunalf-e2da2bffe58a76c091d3496bd3ca2d2f18ea2eb6.tar.gz
initial commit
Diffstat (limited to 'src/alfsum.c')
-rw-r--r--src/alfsum.c53
1 files changed, 53 insertions, 0 deletions
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 <stdio.h>
+#include <unistd.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. */
+
+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;
+}