From 48c719803a04b9995a2fd9e4312f051296046b00 Mon Sep 17 00:00:00 2001 From: Simon Schubert Date: Thu, 20 Aug 2009 20:21:26 +0200 Subject: [PATCH] printf(1): don't use getopt, second take printf must not use getopt, since any passed string is valid, also strings starting with -, i.e. printf -f%s oo has to print "-foo" and not error out with "illegal option". However, if printf is passed "--" as an argument separator, we have to discard it and may not print it. --- usr.bin/printf/printf.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index f077eeb1db..05291f5d83 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -98,27 +98,33 @@ main(int argc, char **argv) #endif { static const char *skip1, *skip2; - int ch, chopped, end, fieldwidth, precision, rval; + int chopped, end, fieldwidth, precision, rval; char convch, nextch, *format, *fmt, *start; #ifndef BUILTIN setlocale(LC_NUMERIC, ""); #endif - while ((ch = getopt(argc, argv, "")) != -1) - switch (ch) { - case '?': - default: - usage(); - return (1); - } - argc -= optind; - argv += optind; - if (argc < 1) { + /* + * We may not use getopt(3) because calling + * "printf -f%s oo" may not result in an invalid + * option error. + * However common usage and other implementations seem + * to indicate that we need to allow -- as a discardable + * option separator. + */ + if (argc > 1 && strcmp(argv[1], "--") == 0) { + argc--; + argv++; + } + + if (argc < 2) { usage(); return (1); } + argv++; + /* * Basic algorithm is to scan the format string for conversion * specifications -- once one is found, find out if the field -- 2.11.4.GIT