In POSIX-2017 it was clarified via the documentation for basename(1) and dirname(1) that all programs should support -- unless specified otherwise.
Signed-off-by: Mattias Andrée <maand...@kth.se> --- chroot.c | 5 ++++- cksum.c | 11 ++++++++++- expr.c | 11 ++++++++++- hostname.c | 5 ++++- link.c | 5 ++++- logname.c | 5 ++++- nohup.c | 5 ++++- printenv.c | 11 ++++++++++- printf.c | 12 ++++++++---- setsid.c | 5 ++++- sleep.c | 5 ++++- sponge.c | 5 ++++- sync.c | 5 ++++- tsort.c | 2 +- tty.c | 5 ++++- unlink.c | 5 ++++- whoami.c | 5 ++++- yes.c | 11 ++++++++++- 18 files changed, 97 insertions(+), 21 deletions(-) diff --git a/chroot.c b/chroot.c index 22bc62e..45f2dc7 100644 --- a/chroot.c +++ b/chroot.c @@ -17,7 +17,10 @@ main(int argc, char *argv[]) char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *cmd; int savederrno; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) usage(); diff --git a/cksum.c b/cksum.c index 4e7dce6..50107b2 100644 --- a/cksum.c +++ b/cksum.c @@ -92,12 +92,21 @@ cksum(int fd, const char *s) putchar('\n'); } +static void +usage(void) +{ + eprintf("usage: %s [file ...]\n", argv0); +} + int main(int argc, char *argv[]) { int fd; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) { cksum(0, NULL); diff --git a/expr.c b/expr.c index d9c758d..ea3c58b 100644 --- a/expr.c +++ b/expr.c @@ -252,12 +252,21 @@ parse(char *expr[], int numexpr) return (valp->str && *valp->str) || valp->num; } +static void +usage(void) +{ + eprintf("usage: %s expression\n", argv0); +} + int main(int argc, char *argv[]) { int ret; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND ret = !parse(argv, argc); diff --git a/hostname.c b/hostname.c index 495d40d..2532ec8 100644 --- a/hostname.c +++ b/hostname.c @@ -16,7 +16,10 @@ main(int argc, char *argv[]) { char host[HOST_NAME_MAX + 1]; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) { if (gethostname(host, sizeof(host)) < 0) diff --git a/link.c b/link.c index a260136..7cee4d0 100644 --- a/link.c +++ b/link.c @@ -12,7 +12,10 @@ usage(void) int main(int argc, char *argv[]) { - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc != 2) usage(); diff --git a/logname.c b/logname.c index 8eb8eea..12908f5 100644 --- a/logname.c +++ b/logname.c @@ -15,7 +15,10 @@ main(int argc, char *argv[]) { char *login; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc) usage(); diff --git a/nohup.c b/nohup.c index c75ea45..2825c5d 100644 --- a/nohup.c +++ b/nohup.c @@ -19,7 +19,10 @@ main(int argc, char *argv[]) { int fd, savederrno; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) usage(); diff --git a/printenv.c b/printenv.c index 7ff5393..19b5b7d 100644 --- a/printenv.c +++ b/printenv.c @@ -6,13 +6,22 @@ extern char **environ; +static void +usage(void) +{ + eprintf("usage: %s [var ...]\n", argv0); +} + int main(int argc, char *argv[]) { char *var; int ret = 0; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) { for (; *environ; environ++) diff --git a/printf.c b/printf.c index 4bc645b..094240a 100644 --- a/printf.c +++ b/printf.c @@ -25,11 +25,15 @@ main(int argc, char *argv[]) int cooldown = 0, width, precision, ret = 0; char *format, *tmp, *arg, *fmt, flag; - argv0 = argv[0]; - if (argc < 2) + ARGBEGIN { + default: usage(); + } ARGEND - format = argv[1]; + if (argc < 1) + usage(); + + format = argv[0]; if ((tmp = strstr(format, "\\c"))) { *tmp = 0; cooldown = 1; @@ -38,7 +42,7 @@ main(int argc, char *argv[]) if (formatlen == 0) return 0; lastargi = 0; - for (i = 0, argi = 2; !cooldown || i < formatlen; i++, i = cooldown ? i : (i % formatlen)) { + for (i = 0, argi = 1; !cooldown || i < formatlen; i++, i = cooldown ? i : (i % formatlen)) { if (i == 0) { if (lastargi == argi) break; diff --git a/setsid.c b/setsid.c index eb682f2..28d3442 100644 --- a/setsid.c +++ b/setsid.c @@ -15,7 +15,10 @@ main(int argc, char *argv[]) { int savederrno; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (!argc) usage(); diff --git a/sleep.c b/sleep.c index 36dfb1c..00ea268 100644 --- a/sleep.c +++ b/sleep.c @@ -14,7 +14,10 @@ main(int argc, char *argv[]) { unsigned seconds; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc != 1) usage(); diff --git a/sponge.c b/sponge.c index 11c8ad4..7a0b272 100644 --- a/sponge.c +++ b/sponge.c @@ -17,7 +17,10 @@ main(int argc, char *argv[]) char tmp[] = "/tmp/sponge-XXXXXX"; int fd, tmpfd; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc != 1) usage(); diff --git a/sync.c b/sync.c index d3390de..f1b9818 100644 --- a/sync.c +++ b/sync.c @@ -12,7 +12,10 @@ usage(void) int main(int argc, char *argv[]) { - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc) usage(); diff --git a/tsort.c b/tsort.c index d093df8..f147e3b 100644 --- a/tsort.c +++ b/tsort.c @@ -188,7 +188,7 @@ main(int argc, char *argv[]) ARGBEGIN { default: usage(); - } ARGEND; + } ARGEND if (argc > 1) usage(); diff --git a/tty.c b/tty.c index 7ffb04a..6515128 100644 --- a/tty.c +++ b/tty.c @@ -15,7 +15,10 @@ main(int argc, char *argv[]) { char *tty; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc) usage(); diff --git a/unlink.c b/unlink.c index 241cf25..c695fa8 100644 --- a/unlink.c +++ b/unlink.c @@ -12,7 +12,10 @@ usage(void) int main(int argc, char *argv[]) { - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc != 1) usage(); diff --git a/whoami.c b/whoami.c index c991ac2..b6f58f7 100644 --- a/whoami.c +++ b/whoami.c @@ -18,7 +18,10 @@ main(int argc, char *argv[]) uid_t uid; struct passwd *pw; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND if (argc) usage(); diff --git a/yes.c b/yes.c index dd97ea6..ffc77f0 100644 --- a/yes.c +++ b/yes.c @@ -3,12 +3,21 @@ #include "util.h" +static void +usage(void) +{ + eprintf("usage: %s [string ...]\n", argv0); +} + int main(int argc, char *argv[]) { char **p; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + ARGBEGIN { + default: + usage(); + } ARGEND for (p = argv; ; p = (*p && *(p + 1)) ? p + 1 : argv) { fputs(*p ? *p : "y", stdout); -- 2.11.1