From 699d4532f6a8f792271c7f46608e2505ca3afc21 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 11 Jan 2015 16:32:47 -0500 Subject: fix regression in getopt_long support for non-option arguments commit b72cd07f176b876aa51864d93aa8101477b1d732 added support for a this feature in getopt, but it was later broken in the case where getopt_long is used as a side effect of the changes made in commit 91184c4f16b143107fa9935edebe5d2b20bd70d8, which prevented the underlying getopt call from seeing the leading '-' or '+' character in optstring. this commit changes the logic in the getopt_long core to check for a leading colon, possibly after the leading '-' or '+', without depending on the latter having been skipped by the caller. a minor incorrectness in the return value for one error condition in getopt_long is also fixed when opterr has been set to zero but optstring has no leading ':'. --- src/misc/getopt_long.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c index e5a4a75a..ffbcd423 100644 --- a/src/misc/getopt_long.c +++ b/src/misc/getopt_long.c @@ -23,7 +23,6 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) { int ret, skipped, resumed; - const char *optstring2 = optstring + 1; if (!optind || __optreset) { __optreset = 0; __optpos = 0; @@ -38,10 +37,9 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con if (argv[i][0] == '-' && argv[i][1]) break; } optind = i; - optstring2 = optstring; } resumed = optind; - ret = __getopt_long_core(argc, argv, optstring2, longopts, idx, longonly); + ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly); if (resumed > skipped) { int i, cnt = optind-resumed; for (i=0; i