fix getopt_long arguments to partial matches

If we find a partial option name match, we need to keep looking for
ambiguous/conflicting options. However, we need to remember the position
in the candidate argument to find its option-argument later, if there is
one. This fixes e.g. option "foobar" being given as "--fooba=baz".
This commit is contained in:
Samuel Holland 2018-01-29 20:36:41 -06:00 committed by Rich Felker
parent 14edadb542
commit f1abc29bd0

View File

@ -58,13 +58,14 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
{ {
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':'; int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
int i, cnt, match; int i, cnt, match;
char *opt; char *arg, *opt;
for (cnt=i=0; longopts[i].name; i++) { for (cnt=i=0; longopts[i].name; i++) {
const char *name = longopts[i].name; const char *name = longopts[i].name;
opt = argv[optind]+1; opt = argv[optind]+1;
if (*opt == '-') opt++; if (*opt == '-') opt++;
for (; *name && *name == *opt; name++, opt++); for (; *name && *name == *opt; name++, opt++);
if (*opt && *opt != '=') continue; if (*opt && *opt != '=') continue;
arg = opt;
match = i; match = i;
if (!*name) { if (!*name) {
cnt = 1; cnt = 1;
@ -74,6 +75,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
} }
if (cnt==1) { if (cnt==1) {
i = match; i = match;
opt = arg;
optind++; optind++;
if (*opt == '=') { if (*opt == '=') {
if (!longopts[i].has_arg) { if (!longopts[i].has_arg) {