From 88bcda88e2b25d11cd7f81cc93a5ae2bd6d00d3e Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Fri, 5 Dec 2025 01:43:07 -0500 Subject: unalf: Error checking for -n option. --- TODO.txt | 2 -- src/opts.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/TODO.txt b/TODO.txt index 5b0b9d9..48ec376 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,3 @@ -- error checking for unalf -n (dup option, bad #, wildcards) - - maybe: alf foo.alf unix.filename=ATARI.NAM ...unalf could use this too. - fuzz alf and unalf with afl++ or similar. this is a work in progress, diff --git a/src/opts.c b/src/opts.c index 06ad9dc..861ef2b 100644 --- a/src/opts.c +++ b/src/opts.c @@ -26,7 +26,30 @@ static void show_globs(void) { #endif int parsenum(const char opt, const char *arg) { - return atoi(arg); /* TODO: use strtol(), die on error */ + const char *p = arg; + int result, err = 0; + + if(opts.extract_num) { + fprintf(stderr, "%s: fatal: can't use multiple -%c arguments (try --help).\n", self, opt); + exit(1); + } + + for(p = arg; *p && (*p >= '0' && *p <= '9'); p++) + ; + + if(*p || p == arg) { + err++; + } else { + result = atoi(arg); + if(!result) err++; + } + + if(err) { + fprintf(stderr, "%s: fatal: invalid number '%s' for -%c option (try --help).\n", self, arg, opt); + exit(1); + } + + return result; } void parse_opts(int argc, char * const *argv) { @@ -72,6 +95,11 @@ void parse_opts(int argc, char * const *argv) { include_globs = &argv[optind + 1]; /* might be null, that's OK */ ig = (char **)include_globs; + if(opts.extract_num && *ig) { + fprintf(stderr, "%s: fatal: can't use wildcards with -n (try -h or --help)\n.", self); + exit(1); + } + while(*ig) { if(globmatch("*.alf", *ig)) { fprintf(stderr, "%s: ALF files don't normally contain other ALF files, are you trying to extract/list multiple ALF files at once?\n", self); -- cgit v1.2.3