diff options
author | Felix Fietkau <nbd@openwrt.org> | 2014-10-21 22:24:50 +0200 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2014-11-15 23:44:53 -0500 |
commit | acccc93e084641861ca553317edb7da7791833b5 (patch) | |
tree | 3e18e6ed37e3710900624fd09657fbf7fb2c5c9f | |
parent | 941644e98c3d05761b4639a8ae5afacd8586d1b9 (diff) | |
download | musl-acccc93e084641861ca553317edb7da7791833b5.tar.gz musl-acccc93e084641861ca553317edb7da7791833b5.tar.bz2 musl-acccc93e084641861ca553317edb7da7791833b5.tar.xz musl-acccc93e084641861ca553317edb7da7791833b5.zip |
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.
-rw-r--r-- | src/misc/getopt.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/misc/getopt.c b/src/misc/getopt.c index 8a2e4d50..f94c4f70 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; |