[hackers] [sbase][PATCH v2] Support -- in all utilities except echo(1)

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sun, 8 Jul 2018 19:59:50 +0200

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 <maandree_AT_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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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;
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
_AT_@ -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
Received on Sun Jul 08 2018 - 19:59:50 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 08 2018 - 20:00:38 CEST