dj(1), mm(1), npc(1), scrut(1), str(1): consistent argv[0] handling

This commit is contained in:
Emma Tebibyte 2024-07-20 07:18:59 -06:00
parent f96ed9c1f3
commit 0282b60e65
Signed by: emma
GPG Key ID: 06FA419A1698C270
5 changed files with 29 additions and 22 deletions

View File

@ -30,7 +30,7 @@
extern int errno; extern int errno;
char *program_name = "dj"; static char *program_name = "dj";
/* dj uses two structures that respectively correspond to the reading and /* dj uses two structures that respectively correspond to the reading and
* writing ends of its jockeyed "pipe". User-configurable members are noted * writing ends of its jockeyed "pipe". User-configurable members are noted
@ -150,7 +150,7 @@ usage(char *s) {
fprintf( fprintf(
stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n" stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n"
"\t[-i file] [-b block_size] [-s offset]\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; return EX_USAGE;
@ -163,6 +163,7 @@ int main(int argc, char *argv[]) {
size_t i; /* side of io being modified */ size_t i; /* side of io being modified */
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
struct Io io[2 /* { in, out } */]; struct Io io[2 /* { in, out } */];
char *s = (argv[0] == NULL ? program_name : argv[0]);
/* Set defaults. */ /* Set defaults. */
align = -1; align = -1;
@ -233,7 +234,7 @@ int main(int argc, char *argv[]) {
/* FALLTHROUGH */ /* FALLTHROUGH */
default: 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 != STDIN_FILENO || io->fl == read_flags);
assert(io->fd != STDOUT_FILENO || io->fl == write_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) { for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
/* buffer allocation */ /* buffer allocation */

View File

@ -48,7 +48,7 @@ struct Files{
#endif #endif
/* pre-allocated strings */ /* pre-allocated strings */
static char *program_name = "<no argv[0]>"; static char *program_name = "mm";
static char *stdin_name = "<stdin>"; static char *stdin_name = "<stdin>";
static char *stdout_name = "<stdout>"; static char *stdout_name = "<stdout>";
static char *stderr_name = "<stderr>"; static char *stderr_name = "<stderr>";
@ -125,6 +125,7 @@ int main(int argc, char *argv[]) {
size_t j; size_t j;
size_t k; /* loop index but also unbuffer status */ size_t k; /* loop index but also unbuffer status */
int retval; int retval;
char *s = argv[0] == NULL ? program_name : argv[0];
/* Initializes the files structs with their default values, standard /* Initializes the files structs with their default values, standard
* input and standard output. If an input or an output is specified * input and standard output. If an input or an output is specified
@ -149,7 +150,7 @@ int main(int argc, char *argv[]) {
k = 0; k = 0;
if (argc > 0) { program_name = argv[0]; } if (argc > 0) { program_name = s; }
if (argc > 1) { if (argc > 1) {
while ((c = getopt(argc, argv, "aehi:no:u")) != -1) { while ((c = getopt(argc, argv, "aehi:no:u")) != -1) {
@ -163,7 +164,7 @@ int main(int argc, char *argv[]) {
break; break;
} }
retval = oserr(argv[0], "-e"); retval = oserr(s, "-e");
terminate; terminate;
case 'i': case 'i':
if ( if (
@ -172,7 +173,7 @@ int main(int argc, char *argv[]) {
|| Files_open(&files[0], optarg) != NULL || Files_open(&files[0], optarg) != NULL
) { break; } ) { break; }
retval = oserr(argv[0], optarg); retval = oserr(s, optarg);
terminate; terminate;
case 'o': case 'o':
if ( if (
@ -190,25 +191,25 @@ int main(int argc, char *argv[]) {
} }
} }
retval = oserr(argv[0], optarg); retval = oserr(s, optarg);
terminate; terminate;
case 'n': case 'n':
if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; } if (signal(SIGINT, SIG_IGN) != SIG_ERR) { break; }
retval = oserr(argv[0], "-n"); retval = oserr(s, "-n");
terminate; terminate;
case 'u': case 'u':
k = 1; k = 1;
break; break;
default: default:
retval = usage(argv[0]); retval = usage(s);
terminate; terminate;
} }
} }
} }
if (optind != argc) { if (optind != argc) {
retval = usage(argv[0]); retval = usage(s);
terminate; terminate;
} }

View File

@ -23,6 +23,8 @@
#include <unistd.h> /* getopt(3) */ #include <unistd.h> /* getopt(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE */ #include <sysexits.h> /* EX_OK, EX_USAGE */
static char *program_name = "dj";
int usage(char *s) { int usage(char *s) {
fprintf(stderr, "Usage: %s [-et]\n", s); fprintf(stderr, "Usage: %s [-et]\n", s);
return EX_USAGE; return EX_USAGE;
@ -32,18 +34,19 @@ int main(int argc, char *argv[]) {
int c; int c;
char showend = 0; /* print a dollar sign before each newline */ char showend = 0; /* print a dollar sign before each newline */
char showtab = 0; /* prints tab characters in caret notation */ char showtab = 0; /* prints tab characters in caret notation */
char *s = (argv[0] == NULL ? program_name : argv[0]);
if (argc > 0) { if (argc > 0) {
while ((c = getopt(argc, argv, "et")) != -1) { while ((c = getopt(argc, argv, "et")) != -1) {
switch (c){ switch (c){
case 'e': showend = 1; break; case 'e': showend = 1; break;
case 't': showtab = 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) { while ((c = getc(stdin)) != EOF) {
if ((c & 0x80) != 0) { fputs("M-", stdout); } if ((c & 0x80) != 0) { fputs("M-", stdout); }

View File

@ -26,7 +26,7 @@
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK, * S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
* S_ISUID, S_ISVTX */ * S_ISUID, S_ISVTX */
char *program_name = "scrut"; static char *program_name = "scrut";
static char args[] = "bcdefgkprsuwxLS"; static char args[] = "bcdefgkprsuwxLS";
int usage(char *s) { int usage(char *s) {
@ -36,15 +36,16 @@ int usage(char *s) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char sel[(sizeof args) / (sizeof *args)]; 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 */ { /* option parsing */
char *p; char *p;
memset(sel, '\0', sizeof sel); memset(sel, '\0', sizeof sel);
for (int c; (c = getopt(argc, argv, args)) != -1;) { 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; } else { sel[p - args] = c; }
} }

View File

@ -53,8 +53,9 @@ int usage(char *s){
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
size_t ctype; /* selected from ctypes.h; index of ctype */ size_t ctype; /* selected from ctypes.h; index of ctype */
int retval; 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 */ for ( /* iterate ctypes */
ctype = 0; ctype = 0;
@ -63,15 +64,15 @@ int main(int argc, char *argv[]){
++ctype ++ctype
); );
if (ctypes[ctype].f == NULL) { return usage(argv[0]); } if (ctypes[ctype].f == NULL) { return usage(s); }
/* iterate args */ /* iterate args */
for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) { for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) {
for (size_t i = 0; argv[0][i] != '\0'; ++i) { /* iterate arg bytes */ for (size_t i = 0; s[i] != '\0'; ++i) { /* iterate arg bytes */
/* First checks if argv[0][i] is valid ASCII; ctypes(3) don't /* First checks if s[i] is valid ASCII; ctypes(3) don't
* handle non-ASCII. This is bad. */ * handle non-ASCII. This is bad. */
if( 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; } ) { return EXIT_FAILURE; }
else { retval = EXIT_SUCCESS; } else { retval = EXIT_SUCCESS; }
} }