mirror of
git://git.musl-libc.org/musl
synced 2025-02-24 14:56:49 +00:00
getopt: update optarg and optind correctly on missing argument
the text of the specification for getopt's handling of options that require an argument, which requires updating optarg and optind, does not exclude the error case where the end of the argument list has been reached. in that case, it is expected that optarg be assigned argv[argc] (normally null) and optind be incremented by 2, resulting in a value of argc+1.
This commit is contained in:
parent
184ef36f85
commit
6aeb131b4c
@ -84,8 +84,12 @@ int getopt(int argc, char * const argv[], const char *optstring)
|
||||
return '?';
|
||||
}
|
||||
if (optstring[i] == ':') {
|
||||
if (optstring[i+1] == ':') optarg = 0;
|
||||
else if (optind >= argc) {
|
||||
optarg = 0;
|
||||
if (optstring[i+1] != ':' || optpos) {
|
||||
optarg = argv[optind++] + optpos;
|
||||
optpos = 0;
|
||||
}
|
||||
if (optind > argc) {
|
||||
optopt = c;
|
||||
if (optstring[0] == ':') return ':';
|
||||
if (opterr) __getopt_msg(argv[0],
|
||||
@ -93,10 +97,6 @@ int getopt(int argc, char * const argv[], const char *optstring)
|
||||
optchar, k);
|
||||
return '?';
|
||||
}
|
||||
if (optstring[i+1] != ':' || optpos) {
|
||||
optarg = argv[optind++] + optpos;
|
||||
optpos = 0;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user