sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 4d38f6068500104693796059da3950a2de296af3
parent 75c97de5932ab4effa51a9ac7ca949a827800490
Author: Christoph Lohmann <20h@r-36.net>
Date:   Fri, 14 Jun 2013 20:20:47 +0200

Eliminating the getopt disgrace.

Diffstat:
Mchmod.c | 36+++++++++++++++++++++---------------
Mchown.c | 40+++++++++++++++++++++++++---------------
Mcmp.c | 53+++++++++++++++++++++++++++++++----------------------
Mdate.c | 40++++++++++++++++++++++++----------------
Mdirname.c | 22+++++++++++++++++-----
Mecho.c | 32++++++++++++++++++++------------
Mfold.c | 48++++++++++++++++++++++++++++--------------------
Mhead.c | 38+++++++++++++++++++++++---------------
Mkill.c | 64+++++++++++++++++++++++++++++++++-------------------------------
Mln.c | 37++++++++++++++++++++++---------------
Mls.c | 57+++++++++++++++++++++++++++++++++------------------------
Mmc.c | 39++++++++++++++++++++++-----------------
Mmkdir.c | 40+++++++++++++++++++++++++---------------
Mmkfifo.c | 25+++++++++++++++++++------
Mmv.c | 27+++++++++++++++++++--------
Mnl.c | 69++++++++++++++++++++++++++++++++++++++++++---------------------------
Mnohup.c | 29++++++++++++++++++++++-------
Mpaste.c | 60++++++++++++++++++++++++++++++++----------------------------
Mpwd.c | 31++++++++++++++++++++-----------
Mrm.c | 36+++++++++++++++++++++---------------
Mseq.c | 68+++++++++++++++++++++++++++++++++++++++++---------------------------
Msleep.c | 23+++++++++++++++++------
Msort.c | 52++++++++++++++++++++++++++++++++--------------------
Mtail.c | 45+++++++++++++++++++++++++--------------------
Mtee.c | 41++++++++++++++++++++++++++---------------
Mtouch.c | 39+++++++++++++++++++++++----------------
Muname.c | 57++++++++++++++++++++++++++++++++-------------------------
Muniq.c | 50++++++++++++++++++++++++++++----------------------
28 files changed, 723 insertions(+), 475 deletions(-)

diff --git a/chmod.c b/chmod.c @@ -13,26 +13,32 @@ static bool rflag = false; static char oper = '='; static mode_t mode = 0; +static void +usage(void) +{ + eprintf("usage: %s [-r] mode [file...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; - while((c = getopt(argc, argv, "r")) != -1) - switch(c) { - case 'r': - rflag = true; - break; - default: - exit(EXIT_FAILURE); - } - if(optind == argc) - eprintf("usage: %s [-r] mode [file...]\n", argv[0]); + ARGBEGIN { + case 'r': + rflag = true; + break; + default: + usage(); + } ARGEND; + + if(argc < 1) + usage(); - parsemode(argv[optind++]); - for(; optind < argc; optind++) - chmodr(argv[optind]); - return EXIT_SUCCESS; + parsemode(argv[0]); + for(++argv; argc > 0; argc--) + chmodr(argv[0]); + return 0; } void diff --git a/chown.c b/chown.c @@ -14,22 +14,30 @@ static bool rflag = false; static struct passwd *pw = NULL; static struct group *gr = NULL; +static void +usage(void) +{ + eprintf("usage: %s [-r] [owner][:[group]] file...\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c, *owner, *group; + char *owner, *group; - while((c = getopt(argc, argv, "r")) != -1) - switch(c) { - case 'r': - rflag = true; - break; - default: - exit(EXIT_FAILURE); - } - if(optind == argc) - eprintf("usage: %s [-r] [owner][:group] [file...]\n", argv[0]); - owner = argv[optind++]; + ARGBEGIN { + case 'r': + rflag = true; + break; + default: + usage(); + } ARGEND; + if(argc == 0) + usage(); + + owner = argv[0]; + argv++; if((group = strchr(owner, ':'))) *group++ = '\0'; @@ -49,9 +57,10 @@ main(int argc, char *argv[]) else if(!gr) eprintf("getgrnam %s: no such group\n", group); } - for(; optind < argc; optind++) - chownpwgr(argv[optind]); - return EXIT_SUCCESS; + for(; argc > 0; argc--, argv++) + chownpwgr(argv[0]); + + return 0; } void @@ -63,3 +72,4 @@ chownpwgr(const char *path) if(rflag) recurse(path, chownpwgr); } + diff --git a/cmp.c b/cmp.c @@ -7,51 +7,60 @@ enum { Same = 0, Diff = 1, Error = 2 }; +static void +usage(void) +{ + enprintf(Error, "usage: %s [-ls] file1 file2\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { bool lflag = false; bool sflag = false; bool same = true; - char c; int b[2], i; long line = 1, n = 1; FILE *fp[2]; - if((c = getopt(argc, argv, "ls")) != -1) - switch(c) { - case 'l': - lflag = true; - break; - case 's': - sflag = true; - break; - default: - exit(Error); - } - if(optind != argc-2) - enprintf(Error, "usage: %s [-ls] file1 file2\n", argv[0]); - for(i = 0; i < 2; i++) - if(!(fp[i] = fopen(argv[optind+i], "r"))) - enprintf(Error, "fopen %s:", argv[optind+i]); - for(n = 1; ((b[0] = getc(fp[0])) != EOF) | ((b[1] = getc(fp[1])) != EOF); n++) { + ARGBEGIN { + case 'l': + lflag = true; + break; + case 's': + sflag = true; + break; + default: + usage(); + } ARGEND; + + if(argc < 2) + usage(); + + for(i = 0; i < 2; i++) { + if(!(fp[i] = fopen(argv[i], "r"))) + enprintf(Error, "fopen %s:", argv[i]); + } + for(n = 1; ((b[0] = getc(fp[0])) != EOF) \ + | ((b[1] = getc(fp[1])) != EOF); n++) { if(b[0] == '\n') line++; if(b[0] == b[1]) continue; for(i = 0; i < 2; i++) if(b[i] == EOF) - enprintf(Diff, "cmp: EOF on %s\n", argv[optind+i]); + enprintf(Diff, "cmp: EOF on %s\n", argv[i]); if(!lflag) { if(!sflag) printf("%s %s differ: char %ld, line %ld\n", - argv[optind], argv[optind+1], n, line); + argv[0], argv[1], n, line); exit(Diff); - } - else { + } else { printf("%4ld %3o %3o\n", n, b[0], b[1]); same = false; } } return same ? Same : Diff; } + diff --git a/date.c b/date.c @@ -5,10 +5,17 @@ #include <unistd.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [-u] [-d format] [+FORMAT]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char buf[BUFSIZ], c; + char buf[BUFSIZ]; char *fmt = "%c"; struct tm *now = NULL; struct tm *(*tztime)(const time_t *) = localtime; @@ -16,24 +23,25 @@ main(int argc, char *argv[]) time_t t; t = time(NULL); - while((c = getopt(argc, argv, "d:u")) != -1) - switch(c) { - case 'd': - t = estrtol(optarg, 0); - break; - case 'u': - tztime = gmtime; - tz = "gm"; - break; - default: - exit(EXIT_FAILURE); - } - if(optind < argc && argv[optind][0] == '+') - fmt = &argv[optind][1]; + ARGBEGIN { + case 'd': + t = estrtol(EARGF(usage()), 0); + break; + case 'u': + tztime = gmtime; + tz = "gm"; + break; + default: + usage(); + } ARGEND; + if(argc > 0 && argv[0][0] == '+') + fmt = &argv[0][1]; if(!(now = tztime(&t))) eprintf("%stime failed\n", tz); strftime(buf, sizeof buf, fmt, now); puts(buf); - return EXIT_SUCCESS; + + return 0; } + diff --git a/dirname.c b/dirname.c @@ -5,14 +5,26 @@ #include <unistd.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s string\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - if(getopt(argc, argv, "") != -1) - exit(EXIT_FAILURE); - if(optind != argc-1) - eprintf("usage: %s string\n", argv[0]); + ARGBEGIN { + default: + usage(); + } ARGEND; + + if(argc < 1) + usage(); puts(dirname(argv[optind])); - return EXIT_SUCCESS; + + return 0; } + diff --git a/echo.c b/echo.c @@ -5,23 +5,31 @@ #include <unistd.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [-n] text\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { bool nflag = false; - char c; - while((c = getopt(argc, argv, "n")) != -1) - switch(c) { - case 'n': - nflag = true; - break; - default: - exit(EXIT_FAILURE); - } - for(; optind < argc; optind++) - putword(argv[optind]); + ARGBEGIN { + case 'n': + nflag = true; + break; + default: + usage(); + } ARGEND; + + for(; argc > 0; argc--, argv++) + putword(argv[0]); if(!nflag) putchar('\n'); - return EXIT_SUCCESS; + + return 0; } + diff --git a/fold.c b/fold.c @@ -13,36 +13,43 @@ static void foldline(const char *, long); static bool bflag = false; static bool sflag = false; +static void +usage(void) +{ + eprintf("usage: %s [-bs] [-w width] [FILE...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; long width = 80; FILE *fp; - while((c = getopt(argc, argv, "bsw:")) != -1) - switch(c) { - case 'b': - bflag = true; - break; - case 's': - sflag = true; - break; - case 'w': - width = estrtol(optarg, 0); - break; - default: - exit(EXIT_FAILURE); - } - if(optind == argc) + ARGBEGIN { + case 'b': + bflag = true; + break; + case 's': + sflag = true; + break; + case 'w': + width = estrtol(EARGF(usage()), 0); + break; + default: + usage(); + } ARGEND; + + if(argc == 0) { fold(stdin, width); - else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); fold(fp, width); fclose(fp); } - return EXIT_SUCCESS; + + return 0; } void @@ -96,3 +103,4 @@ foldline(const char *str, long width) putchar('\n'); } while(str[i = n] && str[i] != '\n'); } + diff --git a/head.c b/head.c @@ -7,30 +7,37 @@ static void head(FILE *, const char *, long); +static void +usage(void) +{ + eprintf("usage: %s [-n] [FILE...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; long n = 10; FILE *fp; - while((c = getopt(argc, argv, "n:")) != -1) - switch(c) { - case 'n': - n = estrtol(optarg, 0); - break; - default: - exit(EXIT_FAILURE); - } - if(optind == argc) + ARGBEGIN { + case 'n': + n = estrtol(EARGF(usage()), 0); + break; + default: + usage(); + } ARGEND; + + if(argc == 0) { head(stdin, "<stdin>", n); - else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); - head(fp, argv[optind], n); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); + head(fp, argv[0], n); fclose(fp); } - return EXIT_SUCCESS; + + return 0; } void @@ -47,3 +54,4 @@ head(FILE *fp, const char *str, long n) if(ferror(fp)) eprintf("%s: read error:", str); } + diff --git a/kill.c b/kill.c @@ -19,60 +19,62 @@ struct { #undef SIG }; -static void usage(void); +static void +usage(void) +{ + eprintf("usage: %s [-s signal] [pid...]\n" + " %s -l [signum]\n", argv0, argv0); + exit(1); +} int main(int argc, char *argv[]) { bool lflag = false; - char c, *end; + char *end, *v; int sig = SIGTERM; pid_t pid; size_t i; - while((c = getopt(argc, argv, "ls:")) != -1) - switch(c) { - case 'l': - lflag = true; + ARGBEGIN { + case 'l': + lflag = true; + break; + case 's': + v = EARGF(usage()); + sig = strtol(v, &end, 0); + if(*end == '\0') break; - case 's': - sig = strtol(optarg, &end, 0); - if(*end == '\0') + for(i = 0; i < LEN(sigs); i++) { + if(!strcasecmp(v, sigs[i].name)) { + sig = sigs[i].sig; break; - for(i = 0; i < LEN(sigs); i++) - if(!strcasecmp(optarg, sigs[i].name)) { - sig = sigs[i].sig; - break; - } - if(i == LEN(sigs)) - eprintf("%s: unknown signal\n", optarg); - break; - default: - usage(); + } } - if(optind < argc-1) + if(i == LEN(sigs)) + eprintf("%s: unknown signal\n", v); + break; + default: + usage(); + } ARGEND; + + if(argc < 2) usage(); if(lflag) { - sig = (optind == argc) ? 0 : estrtol(argv[optind], 0); + sig = (argc > 0) ? 0 : estrtol(argv[0], 0); if(sig > 128) sig = WTERMSIG(sig); for(i = 0; i < LEN(sigs); i++) if(sigs[i].sig == sig || sig == 0) putword(sigs[i].name); putchar('\n'); - } - else for(; optind < argc; optind++) { - pid = estrtol(argv[optind], 0); + } else for(; argc > 0; argc--, argv++) { + pid = estrtol(argv[0], 0); if(kill(pid, sig) == -1) eprintf("kill %d:", pid); } - return EXIT_SUCCESS; -} -void -usage(void) -{ - eprintf("usage: %s [-s signal] [pid...]\n" - " %s -l [signum]\n", argv0, argv0); + return 0; } + diff --git a/ln.c b/ln.c @@ -11,24 +11,30 @@ static int ln(const char *, const char *); static bool sflag = false; static bool fflag = false; +static void +usage(void) +{ + eprintf("usage: %s [-fs] target linkname\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; - - while((c = getopt(argc, argv, "fs")) != -1) - switch(c) { - case 'f': - fflag = true; - break; - case 's': - sflag = true; - break; - default: - exit(EXIT_FAILURE); - } - enmasse(argc - optind, &argv[optind], ln); - return EXIT_SUCCESS; + ARGBEGIN { + case 'f': + fflag = true; + break; + case 's': + sflag = true; + break; + default: + usage(); + } ARGEND; + + enmasse(argc, &argv[0], ln); + + return 0; } int @@ -45,3 +51,4 @@ ln(const char *s1, const char *s2) } return -1; } + diff --git a/ls.c b/ls.c @@ -35,44 +35,52 @@ static bool tflag = false; static bool first = true; static bool many; +static void +usage(void) +{ + eprintf("usage: %s [-adlt] [FILE...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; int i, n; Entry *ents; - while((c = getopt(argc, argv, "adlt")) != -1) - switch(c) { - case 'a': - aflag = true; - break; - case 'd': - dflag = true; - break; - case 'l': - lflag = true; - break; - case 't': - tflag = true; - break; - default: - exit(EXIT_FAILURE); - } - many = (argc > optind+1); - - if((n = argc - optind) > 0) { + ARGBEGIN { + case 'a': + aflag = true; + break; + case 'd': + dflag = true; + break; + case 'l': + lflag = true; + break; + case 't': + tflag = true; + break; + default: + usage(); + } ARGEND; + + many = (argc > 1); + + if((n = argc) > 0) { if(!(ents = malloc(n * sizeof *ents))) eprintf("malloc:"); for(i = 0; i < n; i++) - mkent(&ents[i], argv[optind+i]); - qsort(ents, n, sizeof *ents, (int (*)(const void *, const void *))entcmp); + mkent(&ents[i], argv[i]); + qsort(ents, n, sizeof *ents, + (int (*)(const void *, const void *))entcmp); for(i = 0; i < n; i++) ls(ents[i].name); } else ls("."); - return EXIT_SUCCESS; + + return 0; } int @@ -229,3 +237,4 @@ output(Entry *ent) } putchar('\n'); } + diff --git a/mc.c b/mc.c @@ -14,32 +14,37 @@ static struct linebuf b = EMPTY_LINEBUF; static long n_columns; static long n_rows; +static void +usage(void) +{ + eprintf("usage: %s [-c chars] [FILE...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - int c; long i, l, col; size_t maxlen = 0; char *space; FILE *fp; - while ((c = getopt(argc, argv, "c:")) != -1) - switch (c) { - case 'c': - chars = estrtol(optarg, 0); - if (chars < 3) - eprintf("%d: too few character columns"); - break; - default: - exit(2); - } + ARGBEGIN { + case 'c': + chars = estrtol(EARGF(usage()), 0); + if(chars < 3) + eprintf("%d: too few character columns"); + break; + default: + usage(); + } ARGEND; /* XXX librarify this chunk, too? only useful in sponges though */ - if(optind == argc) + if(argc == 0) { getlines(stdin, &b); - else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); getlines(fp, &b); fclose(fp); } @@ -59,7 +64,7 @@ main(int argc, char *argv[]) for(l = 0; l < b.nlines; ++l) { fputs(b.lines[l], stdout); } - return EXIT_SUCCESS; + return 0; } if(!(space = malloc(maxlen + 2))) @@ -78,6 +83,6 @@ main(int argc, char *argv[]) fputs("\n", stdout); } - return EXIT_SUCCESS; + return 0; } diff --git a/mkdir.c b/mkdir.c @@ -10,26 +10,35 @@ static void mkdirp(char *); +static void +usage(void) +{ + eprintf("usage: %s [-p] directory...\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { bool pflag = false; - char c; - - while((c = getopt(argc, argv, "p")) != -1) - switch(c) { - case 'p': - pflag = true; - break; - default: - exit(EXIT_FAILURE); + + ARGBEGIN { + case 'p': + pflag = true; + break; + default: + usage(); + } ARGEND; + + for(; argc > 0; argc--, argv++) { + if(pflag) { + mkdirp(argv[0]); + } else if(mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) == -1) { + eprintf("mkdir %s:", argv[0]); } - for(; optind < argc; optind++) - if(pflag) - mkdirp(argv[optind]); - else if(mkdir(argv[optind], S_IRWXU|S_IRWXG|S_IRWXO) == -1) - eprintf("mkdir %s:", argv[optind]); - return EXIT_SUCCESS; + } + + return 0; } void @@ -46,3 +55,4 @@ mkdirp(char *path) *p = '/'; } while(p); } + diff --git a/mkfifo.c b/mkfifo.c @@ -5,13 +5,26 @@ #include <sys/stat.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s name...\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - while(getopt(argc, argv, "") != -1) - exit(EXIT_FAILURE); - for(; optind < argc; optind++) - if(mkfifo(argv[optind], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) == -1) - eprintf("mkfifo %s:", argv[optind]); - return EXIT_SUCCESS; + ARGBEGIN { + default: + usage(); + } ARGEND; + for(; argc > 0; argc--, argv++) { + if(mkfifo(argv[0], S_IRUSR|S_IWUSR|S_IRGRP|\ + S_IWGRP|S_IROTH|S_IWOTH) == -1) { + eprintf("mkfifo %s:", argv[0]); + } + } + return 0; } + diff --git a/mv.c b/mv.c @@ -9,26 +9,36 @@ int mv(const char *, const char *); +static void +usage(void) +{ + eprintf("usage: %s source... dest\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { struct stat st; - - if(getopt(argc, argv, "") != -1) - exit(EXIT_FAILURE); + + ARGBEGIN { + default: + usage(); + } ARGEND; + if(argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode))) eprintf("%s: not a directory\n", argv[argc-1]); - enmasse(argc - optind, &argv[optind], mv); - return EXIT_SUCCESS; + enmasse(argc, &argv[0], mv); + + return 0; } int -mv (const char *s1, const char *s2) +mv(const char *s1, const char *s2) { if (rename(s1, s2) == 0) return 0; - if (errno == EXDEV) - { + if (errno == EXDEV) { cp_rflag = true; rm_rflag = true; cp(s1, s2); @@ -37,3 +47,4 @@ mv (const char *s1, const char *s2) } return -1; } + diff --git a/nl.c b/nl.c @@ -14,39 +14,50 @@ static const char *sep = "\t"; static long incr = 1; static regex_t preg; +static void +usage(void) +{ + eprintf("usage: %s [-b style] [-i increment] [-s sep] [FILE...]\n", + argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; FILE *fp; + char *r; - while((c = getopt(argc, argv, "b:i:s:")) != -1) - switch(c) { - case 'b': - mode = optarg[0]; - if(optarg[0] == 'p') - regcomp(&preg, &optarg[1], REG_NOSUB); - else if(!strchr("ant", optarg[0]) || optarg[1] != '\0') - eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]); - break; - case 'i': - incr = estrtol(optarg, 0); - break; - case 's': - sep = optarg; - break; - default: - exit(2); + ARGBEGIN { + case 'b': + r = EARGF(usage()); + mode = r[0]; + if(r[0] == 'p') { + regcomp(&preg, &r[1], REG_NOSUB); + } else if(!strchr("ant", mode)) { + usage(); } - if(optind == argc) + break; + case 'i': + incr = estrtol(EARGF(usage()), 0); + break; + case 's': + sep = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + if(argc == 0) { nl(stdin); - else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); nl(fp); fclose(fp); } - return EXIT_SUCCESS; + + return 0; } void @@ -56,12 +67,16 @@ nl(FILE *fp) long n = 0; size_t size = 0; - while(afgets(&buf, &size, fp)) + while(afgets(&buf, &size, fp)) { if((mode == 'a') - || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0)) - || (mode == 't' && buf[0] != '\n')) + || (mode == 'p' + && !regexec(&preg, buf, 0, NULL, 0)) + || (mode == 't' && buf[0] != '\n')) { printf("%6ld%s%s", n += incr, sep, buf); - else + } else { printf(" %s", buf); + } + } free(buf); } + diff --git a/nohup.c b/nohup.c @@ -9,21 +9,34 @@ enum { Error = 127, Found = 126 }; +static void +usage(void) +{ + eprintf("usage: %s command [argument...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { int fd; - if(getopt(argc, argv, "") != -1) - exit(Error); - if(optind == argc) - enprintf(Error, "usage: %s command [argument...]\n", argv[0]); + ARGBEGIN { + default: + usage(); + } ARGEND; + + if(argc == 0) + usage(); if(signal(SIGHUP, SIG_IGN) == SIG_ERR) enprintf(Error, "signal HUP:"); + if(isatty(STDOUT_FILENO)) { - if((fd = open("nohup.out", O_APPEND|O_CREAT, S_IRUSR|S_IWUSR)) == -1) + if((fd = open("nohup.out", O_APPEND|O_CREAT, + S_IRUSR|S_IWUSR)) == -1) { enprintf(Error, "open nohup.out:"); + } if(dup2(fd, STDOUT_FILENO) == -1) enprintf(Error, "dup2:"); close(fd); @@ -32,7 +45,9 @@ main(int argc, char *argv[]) if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1) enprintf(Error, "dup2:"); - execvp(argv[optind], &argv[optind]); - enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[optind]); + execvp(argv[0], &argv[0]); + enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]); + return Error; } + diff --git a/paste.c b/paste.c @@ -13,42 +13,44 @@ typedef struct { const char *name; } Fdescr; -static void eusage(void); static size_t unescape(wchar_t *); static wint_t in(Fdescr *); static void out(wchar_t); static void sequential(Fdescr *, int, const wchar_t *, size_t); static void parallel(Fdescr *, int, const wchar_t *, size_t); +static void +usage(void) +{ + eprintf("usage: %s [-s] [-d list] file...\n", argv0); + exit(1); +} + int -main(int argc, char **argv) { +main(int argc, char *argv[]) +{ const char *adelim = NULL; bool seq = false; wchar_t *delim; size_t len; Fdescr *dsc; - int i, c; + int i; setlocale(LC_CTYPE, ""); - while((c = getopt(argc, argv, "sd:")) != -1) - switch(c) { - case 's': - seq = true; - break; - case 'd': - adelim = optarg; - break; - case '?': - default: - eusage(); - break; - } + ARGBEGIN { + case 's': + seq = true; + break; + case 'd': + adelim = EARGF(usage()); + break; + default: + usage(); + } ARGEND; - argc -= optind; - argv += optind; if(argc == 0) - eusage(); + usage(); /* populate delimeters */ if(!adelim) @@ -98,16 +100,14 @@ main(int argc, char **argv) { free(delim); free(dsc); + return 0; } -static void -eusage(void) { - eprintf("usage: paste [-s][-d list] file...\n"); -} static size_t -unescape(wchar_t *delim) { +unescape(wchar_t *delim) +{ wchar_t c; size_t i; size_t len; @@ -140,7 +140,8 @@ unescape(wchar_t *delim) { } static wint_t -in(Fdescr *f) { +in(Fdescr *f) +{ wint_t c = fgetwc(f->fp); if(c == WEOF && ferror(f->fp)) @@ -150,14 +151,16 @@ in(Fdescr *f) { } static void -out(wchar_t c) { +out(wchar_t c) +{ putwchar(c); if(ferror(stdout)) eprintf("write error:"); } static void -sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) { +sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) +{ int i; for(i = 0; i < len; i++) { @@ -185,7 +188,8 @@ sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) { } static void -parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) { +parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) +{ int last; do { diff --git a/pwd.c b/pwd.c @@ -7,24 +7,32 @@ static const char *getpwd(const char *cwd); +static void +usage(void) +{ + eprintf("usage: %s [-LP]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char *cwd, c; + char *cwd; char mode = 'L'; - while((c = getopt(argc, argv, "LP")) != -1) - switch(c) { - case 'L': - case 'P': - mode = c; - break; - default: - exit(EXIT_FAILURE); - } + ARGBEGIN { + case 'L': + case 'P': + mode = ARGC(); + break; + default: + usage(); + } ARGEND; + cwd = agetcwd(); puts((mode == 'L') ? getpwd(cwd) : cwd); - return EXIT_SUCCESS; + + return 0; } const char * @@ -42,3 +50,4 @@ getpwd(const char *cwd) else return cwd; } + diff --git a/rm.c b/rm.c @@ -7,23 +7,29 @@ #include "fs.h" #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [-fr] FILE...\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; + ARGBEGIN { + case 'f': + rm_fflag = true; + break; + case 'r': + rm_rflag = true; + break; + default: + usage(); + } ARGEND; + for(; argc > 0; argc--, argv++) + rm(argv[0]); - while((c = getopt(argc, argv, "fr")) != -1) - switch(c) { - case 'f': - rm_fflag = true; - break; - case 'r': - rm_rflag = true; - break; - default: - exit(EXIT_FAILURE); - } - for(; optind < argc; optind++) - rm(argv[optind]); - return EXIT_SUCCESS; + return 0; } + diff --git a/seq.c b/seq.c @@ -11,43 +11,56 @@ static int digitsright(const char *); static double estrtod(const char *); static bool validfmt(const char *); +static void +usage(void) +{ + eprintf("usage: %s [-f format] [-s sep] [-w width] first" + " [inc [last]]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n"; bool wflag = false; - char c, ftmp[BUFSIZ], *fmt = ftmp; + char ftmp[BUFSIZ], *fmt = ftmp; double start, step, end, out, dir; - while((c = getopt(argc, argv, "f:s:w")) != -1) - switch(c) { - case 'f': - if(!validfmt(optarg)) - eprintf("%s: invalid format\n", optarg); - fmt = optarg; - break; - case 's': - sep = optarg; - break; - case 'w': - wflag = true; - break; - } - - switch(argc-optind) { + ARGBEGIN { + case 'f': + if(!validfmt(EARGF(usage()))) + eprintf("%s: invalid format\n", ARGF()); + fmt = ARGF(); + break; + case 's': + sep = EARGF(usage()); + break; + case 'w': + wflag = true; + break; + default: + usage(); + } ARGEND; + + switch(argc) { case 3: - starts = argv[optind++]; - steps = argv[optind++]; - ends = argv[optind++]; + starts = argv[0]; + argv++; + steps = argv[0]; + argv++; + ends = argv[0]; + argv++; break; case 2: - starts = argv[optind++]; + starts = argv[0]; + argv++; /* fallthrough */ case 1: - ends = argv[optind++]; + ends = argv[0]; break; default: - eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]] end\n", argv[0]); + usage(); } start = estrtod(starts); step = estrtod(steps); @@ -65,9 +78,9 @@ main(int argc, char *argv[]) if(wflag) { int left = MAX(digitsleft(starts), digitsleft(ends)); - snprintf(ftmp, sizeof ftmp, "%%0%d.%df", right+left+(right != 0), right); - } - else + snprintf(ftmp, sizeof ftmp, "%%0%d.%df", + right+left+(right != 0), right); + } else snprintf(ftmp, sizeof ftmp, "%%.%df", right); } for(out = start; out * dir <= end * dir; out += step) { @@ -77,7 +90,7 @@ main(int argc, char *argv[]) } printf("\n"); - return EXIT_SUCCESS; + return 0; } int @@ -155,3 +168,4 @@ format: return false; } } + diff --git a/sleep.c b/sleep.c @@ -3,18 +3,29 @@ #include <unistd.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s seconds\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { unsigned int seconds; - if(getopt(argc, argv, "") != -1) - exit(EXIT_FAILURE); - if(optind != argc-1) - eprintf("usage: %s seconds\n", argv[0]); + ARGBEGIN { + default: + usage(); + } ARGEND; - seconds = estrtol(argv[optind], 0); + if(argc < 1) + usage(); + + seconds = estrtol(argv[0], 0); while((seconds = sleep(seconds)) > 0) ; - return EXIT_SUCCESS; + return 0; } + diff --git a/sort.c b/sort.c @@ -14,38 +14,49 @@ static bool uflag = false; static struct linebuf linebuf = EMPTY_LINEBUF; +static void +usage(void) +{ + eprintf("usage: %s [-ru] [file...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; long i; FILE *fp; - while((c = getopt(argc, argv, "ru")) != -1) - switch(c) { - case 'r': - rflag = true; - break; - case 'u': - uflag = true; - break; - default: - exit(2); - } - if(optind == argc) + ARGBEGIN { + case 'r': + rflag = true; + break; + case 'u': + uflag = true; + break; + default: + usage(); + } ARGEND; + + if(argc == 0) { getlines(stdin, &linebuf); - else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); + } else for(; argc > 0; argc--, argv++) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); getlines(fp, &linebuf); fclose(fp); } - qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, (int (*)(const void *, const void *))linecmp); + qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, + (int (*)(const void *, const void *))linecmp); - for(i = 0; i < linebuf.nlines; i++) - if(!uflag || i == 0 || strcmp(linebuf.lines[i], linebuf.lines[i-1]) != 0) + for(i = 0; i < linebuf.nlines; i++) { + if(!uflag || i == 0 || strcmp(linebuf.lines[i], + linebuf.lines[i-1]) != 0) { fputs(linebuf.lines[i], stdout); - return EXIT_SUCCESS; + } + } + + return 0; } int @@ -53,3 +64,4 @@ linecmp(const char **a, const char **b) { return strcmp(*a, *b) * (rflag ? -1 : +1); } + diff --git a/tail.c b/tail.c @@ -9,36 +9,40 @@ static void dropinit(FILE *, const char *, long); static void taketail(FILE *, const char *, long); +static void +usage(void) +{ + eprintf("usage: %s [-n lines] [file]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; long n = 10; FILE *fp; void (*tail)(FILE *, const char *, long) = taketail; - while((c = getopt(argc, argv, "n:")) != -1) - switch(c) { - case 'n': - n = abs(estrtol(optarg, 0)); - if(optarg[0] == '+') - tail = dropinit; - break; - default: - exit(EXIT_FAILURE); - } - if(optind == argc) + ARGBEGIN { + case 'n': + n = abs(estrtol(EARGF(usage()), 0)); + if(optarg[0] == '+') + tail = dropinit; + break; + default: + usage(); + } ARGEND; + if(argc == 0) { tail(stdin, "<stdin>", n); - else if(optind == argc-1) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); - tail(fp, argv[optind], n); + } else if(argc == 1) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); + tail(fp, argv[0], n); fclose(fp); - } - else - eprintf("usage: %s [-n lines] [file]\n", argv[0]); + } else + usage(); - return EXIT_SUCCESS; + return 0; } void @@ -76,3 +80,4 @@ taketail(FILE *fp, const char *str, long n) free(ring); free(size); } + diff --git a/tee.c b/tee.c @@ -5,38 +5,49 @@ #include <unistd.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [-a] [file...]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { bool aflag = false; - char buf[BUFSIZ], c; + char buf[BUFSIZ]; int i, nfps = 1; size_t n; FILE **fps; - while((c = getopt(argc, argv, "a")) != -1) - switch(c) { - case 'a': - aflag = true; - break; - default: - exit(EXIT_FAILURE); - } + ARGBEGIN { + case 'a': + aflag = true; + break; + default: + usage(); + } ARGEND; + if(!(fps = malloc(sizeof *fps))) eprintf("malloc:"); fps[nfps-1] = stdout; - for(; optind < argc; optind++) { + for(; argc > 0; argc--, argv++) { if(!(fps = realloc(fps, ++nfps * sizeof *fps))) eprintf("realloc:"); - if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w"))) - eprintf("fopen %s:", argv[optind]); + if(!(fps[nfps-1] = fopen(argv[0], aflag ? "a" : "w"))) + eprintf("fopen %s:", argv[0]); } - while((n = fread(buf, 1, sizeof buf, stdin)) > 0) - for(i = 0; i < nfps; i++) + while((n = fread(buf, 1, sizeof buf, stdin)) > 0) { + for(i = 0; i < nfps; i++) { if(fwrite(buf, 1, n, fps[i]) != n) eprintf("%s: write error:", buf); + } + } if(ferror(stdin)) eprintf("<stdin>: read error:"); - return EXIT_SUCCESS; + + return 0; } + diff --git a/touch.c b/touch.c @@ -14,26 +14,32 @@ static void touch(const char *); static bool cflag = false; static time_t t; +static void +usage(void) +{ + eprintf("usage: %s [-c] [-t stamp] file...\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - char c; - t = time(NULL); - while((c = getopt(argc, argv, "ct:")) != -1) - switch(c) { - case 'c': - cflag = true; - break; - case 't': - t = estrtol(optarg, 0); - break; - default: - exit(EXIT_FAILURE); - } - for(; optind < argc; optind++) - touch(argv[optind]); - return EXIT_SUCCESS; + + ARGBEGIN { + case 'c': + cflag = true; + break; + case 't': + t = estrtol(EARGF(usage()), 0); + break; + default: + usage(); + } ARGEND; + for(; argc > 0; argc--, argv++) + touch(argv[0]); + + return 0; } void @@ -60,3 +66,4 @@ touch(const char *str) close(fd); touch(str); } + diff --git a/uname.c b/uname.c @@ -6,6 +6,13 @@ #include <sys/utsname.h> #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [-amnrsv]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { @@ -14,32 +21,30 @@ main(int argc, char *argv[]) bool rflag = false; bool sflag = false; bool vflag = false; - char c; struct utsname u; - while((c = getopt(argc, argv, "amnrsv")) != -1) - switch(c) { - case 'a': - mflag = nflag = rflag = sflag = vflag = true; - break; - case 'm': - mflag = true; - break; - case 'n': - nflag = true; - break; - case 'r': - rflag = true; - break; - case 's': - sflag = true; - break; - case 'v': - vflag = true; - break; - default: - exit(EXIT_FAILURE); - } + ARGBEGIN { + case 'a': + mflag = nflag = rflag = sflag = vflag = true; + break; + case 'm': + mflag = true; + break; + case 'n': + nflag = true; + break; + case 'r': + rflag = true; + break; + case 's': + sflag = true; + break; + case 'v': + vflag = true; + break; + default: + usage(); + } ARGEND; if(uname(&u) == -1) eprintf("uname:"); @@ -54,5 +59,7 @@ main(int argc, char *argv[]) if(mflag) putword(u.machine); putchar('\n'); - return EXIT_SUCCESS; + + return 0; } + diff --git a/uniq.c b/uniq.c @@ -19,39 +19,44 @@ static bool uflag = false; static char *prev_line = NULL; static long prev_line_count = 0; +static void +usage(void) +{ + eprintf("usage: %s [-cdu] [input]]\n", argv0); + exit(1); +} + int main(int argc, char *argv[]) { - int c; FILE *fp; - while((c = getopt(argc, argv, "cdu")) != -1) - switch(c) { - case 'c': - countfmt = "%7ld "; - break; - case 'd': - dflag = true; - break; - case 'u': - uflag = true; - break; - default: - exit(2); - } + ARGBEGIN { + case 'c': + countfmt = "%7ld "; + break; + case 'd': + dflag = true; + break; + case 'u': + uflag = true; + break; + default: + usage(); + } ARGEND; - if(optind == argc) + if(argc == 0) { uniq(stdin, "<stdin>"); - else if(optind == argc - 1) { - if(!(fp = fopen(argv[optind], "r"))) - eprintf("fopen %s:", argv[optind]); - uniq(fp, argv[optind]); + } else if(argc == 1) { + if(!(fp = fopen(argv[0], "r"))) + eprintf("fopen %s:", argv[0]); + uniq(fp, argv[0]); fclose(fp); } else - enprintf(2, "too many arguments\n"); + usage(); uniq_finish(); - return EXIT_SUCCESS; + return 0; } void @@ -96,3 +101,4 @@ uniq_finish() { uniq_line(NULL); } +