[hackers] [sbase][PATCH] basename, dirname, printf: recognise -- and fail if options are used.

From: Mattias Andrée <maandree_AT_kth.se>
Date: Fri, 25 Mar 2016 21:31:37 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 basename.c |  5 ++++-
 dirname.c  |  6 +++++-
 printf.c   | 12 ++++++++----
 3 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/basename.c b/basename.c
index 4a6ac2a..94a2848 100644
--- a/basename.c
+++ b/basename.c
_AT_@ -17,7 +17,10 @@ main(int argc, char *argv[])
 	ssize_t off;
 	char *p;
 
-	argv0 = argv[0], argc--, argv++;
+	ARGBEGIN {
+	default:
+		usage();
+	} ARGEND
 
 	if (argc != 1 && argc != 2)
 		usage();
diff --git a/dirname.c b/dirname.c
index 4bef9a4..3207f3e 100644
--- a/dirname.c
+++ b/dirname.c
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <libgen.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "util.h"
 
_AT_@ -13,7 +14,10 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-	argv0 = argv[0], argc--, argv++;
+	ARGBEGIN {
+	default:
+		usage();
+	} ARGEND
 
 	if (argc != 1)
 		usage();
diff --git a/printf.c b/printf.c
index 2e24817..e729550 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)
+		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;
-- 
2.7.3
Received on Fri Mar 25 2016 - 21:31:37 CET

This archive was generated by hypermail 2.3.0 : Fri Mar 25 2016 - 21:36:15 CET