diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-11-18 06:57:14 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-11-18 06:57:14 -0500 |
| commit | ebfad791508af77f7f8e942c88a6f5dc88bd6d41 (patch) | |
| tree | c4414ef1e0fccc49c261dd5305dcdfb2feb53efc /src | |
| parent | 7ec4a5150710643e99f982b5a82f8aeb49302d81 (diff) | |
| download | unalf-ebfad791508af77f7f8e942c88a6f5dc88bd6d41.tar.gz | |
Improve alfsum usage a bit.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 4 | ||||
| -rw-r--r-- | src/alfsum.1 | 21 | ||||
| -rw-r--r-- | src/alfsum.c | 23 | ||||
| -rw-r--r-- | src/alfsum.rst | 19 | ||||
| -rw-r--r-- | src/extract.c | 6 | ||||
| -rw-r--r-- | src/glob.c | 5 | ||||
| -rw-r--r-- | src/io.c | 3 | ||||
| -rw-r--r-- | src/listalf.c | 3 | ||||
| -rw-r--r-- | src/opts.c | 3 | ||||
| -rw-r--r-- | src/self.c | 13 | ||||
| -rw-r--r-- | src/unalf.c | 18 | ||||
| -rw-r--r-- | src/unalf.h | 13 |
12 files changed, 73 insertions, 58 deletions
diff --git a/src/Makefile b/src/Makefile index 9113572..ea983e9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -32,7 +32,9 @@ CFLAGS=-DVERSION='"$(VERSION)"' -Wall -I../f65 $(COPT) all: unalf unalf.1 alfsum alfsum.1 -unalf: unalf.o io.o listalf.o extract.o ../f65/f65.o glob.o opts.o usage.o +unalf: unalf.o io.o listalf.o extract.o ../f65/f65.o glob.o opts.o usage.o self.o + +alfsum: alfsum.o self.o usage.o: usage.c diff --git a/src/alfsum.1 b/src/alfsum.1 index 6c51056..127b2f4 100644 --- a/src/alfsum.1 +++ b/src/alfsum.1 @@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. -.TH "ALFSUM" 1 "2025-11-17" "0.1.0" "Urchlay's Atari 8-bit Tools" +.TH "ALFSUM" 1 "2025-11-18" "0.1.0" "Urchlay's Atari 8-bit Tools" .SH NAME alfsum \- calculate ALF checksums .\" RST source for alfsum(1) man page. Convert with: @@ -36,14 +36,25 @@ alfsum \- calculate ALF checksums . .SH SYNOPSIS .sp -alfsum \fBfile\fP [\fBfile\fP ...] +alfsum \fBfile\fP [\fBfile\fP ...] | [\fB\-\-help\fP | \fB\-h\fP] | [\fB\-\-version\fP | \fB\-V\fP] .SH DESCRIPTION .sp \fBalfsum\fP calculates the checksums used by the \fBALF\fP compression utility on the Atari 8\-bit platform. .sp -There are no options. Use \fB\-\fP to read from standard input. Use -\fB\&./\-file\fP to read a file whose name begins with \fB\-\fP\&. +To read from standard input, use \fB\-\fP as a filename +.sp +Output lines have a tab separating the checksum and filename, for ease +of scripting (e.g. with \fBcut\fP(1) or \fBawk\fP(1)). +.SH OPTIONS +.INDENT 0.0 +.TP +.B \-h\fP,\fB \-\-help +Show built\-in help message. +.TP +.B \-V\fP,\fB \-\-version +Show \fBalfsum\fP version number and exit. +.UNINDENT .SH NOTES .sp The checksum algorithm is very simple: all the bytes in the file are @@ -58,7 +69,7 @@ Success. File I/O error count. If there are more than 254 I/O errors, 254 is returned. .TP .B 255 -Error in command\-line arguments. +Error in command\-line arguments: no filenames given. .UNINDENT .SH COPYRIGHT .sp diff --git a/src/alfsum.c b/src/alfsum.c index 7f30c7c..2456e60 100644 --- a/src/alfsum.c +++ b/src/alfsum.c @@ -1,12 +1,9 @@ -#include <stdio.h> -#include <unistd.h> +#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. */ -char *self; - void alfsum(const char *file, FILE *f) { int c; unsigned long sum = 0; @@ -14,7 +11,7 @@ void alfsum(const char *file, FILE *f) { while((c = fgetc(f)) != EOF) sum += c; - printf("%8s\t%04x\n", file, (unsigned int)(sum & 0xffff)); + printf("%04x\t%s\n", (unsigned int)(sum & 0xffff), file); } int main(int argc, char **argv) { @@ -22,15 +19,19 @@ int main(int argc, char **argv) { char *file; FILE *f; - self = argv[0]; + set_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" + 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 -1; + return (argc < 2) ? -1 : 0; + } + + if(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) { + puts(VERSION); + exit(0); } while((file = *++argv)) { diff --git a/src/alfsum.rst b/src/alfsum.rst index 69d8f8f..cf9c7d3 100644 --- a/src/alfsum.rst +++ b/src/alfsum.rst @@ -21,7 +21,7 @@ calculate ALF checksums SYNOPSIS ======== -alfsum **file** [**file** ...] +alfsum **file** [**file** ...] | [**--help** | **-h**] | [**--version** | **-V**] DESCRIPTION =========== @@ -29,8 +29,19 @@ DESCRIPTION **alfsum** calculates the checksums used by the **ALF** compression utility on the Atari 8-bit platform. -There are no options. Use **-** to read from standard input. Use -**./-file** to read a file whose name begins with **-**. +To read from standard input, use **-** as a filename + +Output lines have a tab separating the checksum and filename, for ease +of scripting (e.g. with **cut**\(1) or **awk**\(1)). + +OPTIONS +======= + +-h, --help + Show built-in help message. + +-V, --version + Show **alfsum** version number and exit. NOTES ===== @@ -48,7 +59,7 @@ EXIT STATUS File I/O error count. If there are more than 254 I/O errors, 254 is returned. 255 - Error in command-line arguments. + Error in command-line arguments: no filenames given. COPYRIGHT ========= diff --git a/src/extract.c b/src/extract.c index 21d2bb4..3c85142 100644 --- a/src/extract.c +++ b/src/extract.c @@ -1,9 +1,3 @@ -#include <stdio.h> -#include <stdlib.h> -#include <ctype.h> -#include <string.h> -#include <limits.h> -#include <f65.h> #include "unalf.h" #include "addrs.h" @@ -1,8 +1,3 @@ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> #include "unalf.h" /* this is an Atari DOS or MS-DOS style match: "*.*" matches everything, @@ -1,6 +1,3 @@ -#include <stdio.h> -#include <stdlib.h> -#include <f65.h> #include "unalf.h" #include "addrs.h" diff --git a/src/listalf.c b/src/listalf.c index c4cd713..2c236c9 100644 --- a/src/listalf.c +++ b/src/listalf.c @@ -1,6 +1,3 @@ -#include <stdio.h> -#include <stdlib.h> -#include <f65.h> #include "unalf.h" #include "addrs.h" @@ -1,6 +1,3 @@ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> #include "unalf.h" #define OPTIONS "aeklLopqtvVd:x:" diff --git a/src/self.c b/src/self.c new file mode 100644 index 0000000..298608a --- /dev/null +++ b/src/self.c @@ -0,0 +1,13 @@ +#include "unalf.h" + +char *self; + +void set_self(char *argv0) { + char *p; + + self = argv0; + p = strrchr(self, '/'); + if(!p) p = strrchr(self, '\\'); // windows exe needs this + if(p) self = p + 1; +} + diff --git a/src/unalf.c b/src/unalf.c index 7b29240..ee235b1 100644 --- a/src/unalf.c +++ b/src/unalf.c @@ -1,29 +1,13 @@ -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <sys/stat.h> -#include <f65.h> #include "unalf.h" FILE *in_file, *out_file; -char *in_filename, *self; +char *in_filename; opts_t opts; const char *exclude_globs[MAX_EXCLUDES]; int exclude_count; char * const *include_globs; static void create_outdir(void); -static void set_self(char *argv0) { - char *p; - - self = argv0; - p = strrchr(self, '/'); - if(!p) p = strrchr(self, '\\'); // windows exe needs this - if(p) self = p + 1; -} - /* like "mkdir -p" (no error if dir already exists), followed by "cd" (which will error if the existing "directory" turns out to be a file or broken symlink */ diff --git a/src/unalf.h b/src/unalf.h index 12654ec..c21944e 100644 --- a/src/unalf.h +++ b/src/unalf.h @@ -1,3 +1,12 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> +#include <sys/stat.h> +#include <f65.h> + #ifndef VERSION #define VERSION "???" #warning "VERSION not defined, defaulting to \"???\"" @@ -54,6 +63,10 @@ void fix_filename(void); /* opts.c */ void parse_opts(int argc, char * const *argv); +/* self.c */ +extern char *self; +void set_self(char *argv0); + /* unalf.c */ extern char *self; extern FILE *out_file; |
