dj(1), mm(1), npc(1), scrut(1), str(1): consistent argv[0] handling
This commit is contained in:
parent
f96ed9c1f3
commit
0282b60e65
9
src/dj.c
9
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 */
|
||||
|
17
src/mm.c
17
src/mm.c
@ -48,7 +48,7 @@ struct Files{
|
||||
#endif
|
||||
|
||||
/* pre-allocated strings */
|
||||
static char *program_name = "<no argv[0]>";
|
||||
static char *program_name = "mm";
|
||||
static char *stdin_name = "<stdin>";
|
||||
static char *stdout_name = "<stdout>";
|
||||
static char *stderr_name = "<stderr>";
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include <unistd.h> /* getopt(3) */
|
||||
#include <sysexits.h> /* 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); }
|
||||
|
@ -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; }
|
||||
}
|
||||
|
||||
|
11
src/str.c
11
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; }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user