From 0282b60e650305182ab13448e58284da5067fb6c Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 20 Jul 2024 07:18:59 -0600 Subject: [PATCH] dj(1), mm(1), npc(1), scrut(1), str(1): consistent argv[0] handling --- src/dj.c | 9 +++++---- src/mm.c | 17 +++++++++-------- src/npc.c | 7 +++++-- src/scrut.c | 7 ++++--- src/str.c | 11 ++++++----- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/dj.c b/src/dj.c index 2301eb4..894feba 100644 --- a/src/dj.c +++ b/src/dj.c @@ -30,7 +30,7 @@ extern int errno; -char *program_name = "dj"; +static char *program_name = "dj"; /* dj uses two structures that respectively correspond to the reading and * writing ends of its jockeyed "pipe". User-configurable members are noted @@ -150,7 +150,7 @@ usage(char *s) { fprintf( stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n" "\t[-i file] [-b block_size] [-s offset]\n" - "\t[-o file] [-B block_size] [-S offset]\n", program_name + "\t[-o file] [-B block_size] [-S offset]\n", s ); return EX_USAGE; @@ -163,6 +163,7 @@ int main(int argc, char *argv[]) { size_t i; /* side of io being modified */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ struct Io io[2 /* { in, out } */]; + char *s = (argv[0] == NULL ? program_name : argv[0]); /* Set defaults. */ align = -1; @@ -233,7 +234,7 @@ int main(int argc, char *argv[]) { /* FALLTHROUGH */ default: - return usage(program_name); + return usage(s); } } } @@ -241,7 +242,7 @@ int main(int argc, char *argv[]) { assert(io->fd != STDIN_FILENO || io->fl == read_flags); assert(io->fd != STDOUT_FILENO || io->fl == write_flags); - if (argc > optind) { return usage(program_name); } + if (argc > optind) { return usage(s); } for (i = 0; i < (sizeof io) / (sizeof *io); ++i) { /* buffer allocation */ diff --git a/src/mm.c b/src/mm.c index 0aced76..459d3b3 100644 --- a/src/mm.c +++ b/src/mm.c @@ -48,7 +48,7 @@ struct Files{ #endif /* pre-allocated strings */ -static char *program_name = ""; +static char *program_name = "mm"; static char *stdin_name = ""; static char *stdout_name = ""; static char *stderr_name = ""; @@ -125,6 +125,7 @@ int main(int argc, char *argv[]) { size_t j; size_t k; /* loop index but also unbuffer status */ int retval; + char *s = argv[0] == NULL ? program_name : argv[0]; /* Initializes the files structs with their default values, standard * input and standard output. If an input or an output is specified @@ -149,7 +150,7 @@ int main(int argc, char *argv[]) { k = 0; - if (argc > 0) { program_name = argv[0]; } + if (argc > 0) { program_name = s; } if (argc > 1) { while ((c = getopt(argc, argv, "aehi:no:u")) != -1) { @@ -163,7 +164,7 @@ int main(int argc, char *argv[]) { break; } - retval = oserr(argv[0], "-e"); + retval = oserr(s, "-e"); terminate; case 'i': if ( @@ -172,7 +173,7 @@ int main(int argc, char *argv[]) { || Files_open(&files[0], optarg) != NULL ) { break; } - retval = oserr(argv[0], optarg); + retval = oserr(s, optarg); terminate; case 'o': if ( @@ -190,25 +191,25 @@ int main(int argc, char *argv[]) { } } - retval = oserr(argv[0], optarg); + retval = oserr(s, optarg); terminate; case 'n': if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; } - retval = oserr(argv[0], "-n"); + retval = oserr(s, "-n"); terminate; case 'u': k = 1; break; default: - retval = usage(argv[0]); + retval = usage(s); terminate; } } } if (optind != argc) { - retval = usage(argv[0]); + retval = usage(s); terminate; } diff --git a/src/npc.c b/src/npc.c index 5e00351..0767163 100644 --- a/src/npc.c +++ b/src/npc.c @@ -23,6 +23,8 @@ #include /* getopt(3) */ #include /* EX_OK, EX_USAGE */ +static char *program_name = "dj"; + int usage(char *s) { fprintf(stderr, "Usage: %s [-et]\n", s); return EX_USAGE; @@ -32,18 +34,19 @@ int main(int argc, char *argv[]) { int c; char showend = 0; /* print a dollar sign before each newline */ char showtab = 0; /* prints tab characters in caret notation */ + char *s = (argv[0] == NULL ? program_name : argv[0]); if (argc > 0) { while ((c = getopt(argc, argv, "et")) != -1) { switch (c){ case 'e': showend = 1; break; case 't': showtab = 1; break; - default: return usage(argv[0]); + default: return usage(s); } } } - if (argc > optind) { return usage(argv[0]); } + if (argc > optind) { return usage(s); } while ((c = getc(stdin)) != EOF) { if ((c & 0x80) != 0) { fputs("M-", stdout); } diff --git a/src/scrut.c b/src/scrut.c index 5e41def..ad2ed92 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -26,7 +26,7 @@ * S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK, * S_ISUID, S_ISVTX */ -char *program_name = "scrut"; +static char *program_name = "scrut"; static char args[] = "bcdefgkprsuwxLS"; int usage(char *s) { @@ -36,15 +36,16 @@ int usage(char *s) { int main(int argc, char *argv[]) { char sel[(sizeof args) / (sizeof *args)]; + char *s = (argv[0] == NULL ? program_name : argv[0]); - if (argc < 2) { return usage(argv[0] == NULL ? program_name : argv[0]); } + if (argc < 2) { return usage(s); } { /* option parsing */ char *p; memset(sel, '\0', sizeof sel); for (int c; (c = getopt(argc, argv, args)) != -1;) { - if ((p = strchr(args, c)) == NULL) { return usage(argv[0]); } + if ((p = strchr(args, c)) == NULL) { return usage(s); } else { sel[p - args] = c; } } diff --git a/src/str.c b/src/str.c index d6a89c3..646e4c7 100644 --- a/src/str.c +++ b/src/str.c @@ -53,8 +53,9 @@ int usage(char *s){ int main(int argc, char *argv[]){ size_t ctype; /* selected from ctypes.h; index of ctype */ int retval; + char *s = (argv[0] == NULL ? program_name : argv[0]); - if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); } + if (argc < 3) { return usage(s); } for ( /* iterate ctypes */ ctype = 0; @@ -63,15 +64,15 @@ int main(int argc, char *argv[]){ ++ctype ); - if (ctypes[ctype].f == NULL) { return usage(argv[0]); } + if (ctypes[ctype].f == NULL) { return usage(s); } /* iterate args */ for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) { - for (size_t i = 0; argv[0][i] != '\0'; ++i) { /* iterate arg bytes */ - /* First checks if argv[0][i] is valid ASCII; ctypes(3) don't + for (size_t i = 0; s[i] != '\0'; ++i) { /* iterate arg bytes */ + /* First checks if s[i] is valid ASCII; ctypes(3) don't * handle non-ASCII. This is bad. */ if( - (unsigned char)argv[0][i] < 0x80 && !ctypes[ctype].f(argv[0][i]) + (unsigned char)s[i] < 0x80 && !ctypes[ctype].f(s[i]) ) { return EXIT_FAILURE; } else { retval = EXIT_SUCCESS; } }