|
Message-Id: <1413923090-71355-1-git-send-email-nbd@openwrt.org> Date: Tue, 21 Oct 2014 22:24:50 +0200 From: Felix Fietkau <nbd@...nwrt.org> To: musl@...ts.openwall.com Subject: [PATCH v2] getopt: fix optional argument processing Processing an option character with optional argument fails if the option is last on the command line. This happens because the if (optind >= argc) check runs first before testing for optional argument. --- src/misc/getopt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/misc/getopt.c b/src/misc/getopt.c index 8a2e4d5..f94c4f7 100644 --- a/src/misc/getopt.c +++ b/src/misc/getopt.c @@ -55,7 +55,8 @@ int getopt(int argc, char * const argv[], const char *optstring) return '?'; } if (optstring[i+1] == ':') { - if (optind >= argc) { + if (optstring[i+2] == ':') optarg = 0; + else if (optind >= argc) { if (optstring[0] == ':') return ':'; if (opterr) { write(2, argv[0], strlen(argv[0])); @@ -65,7 +66,6 @@ int getopt(int argc, char * const argv[], const char *optstring) } return '?'; } - if (optstring[i+2] == ':') optarg = 0; if (optstring[i+2] != ':' || optpos) { optarg = argv[optind++] + optpos; optpos = 0; -- 2.0.4
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.