mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:24:58 +00:00
LibC: Correctly reset the getopt state on optind = 1
The Linux `getopt_long` manpage tells users to reset `optind` to 1 when scanning the same argument vector or a new argument vector again. This makes sense, since `optind` denotes the _next_ option to be processed. The behavior of setting `optind` to 0 doesn't seem to be specified anywhere, so let's also remove that comment from `unistd.h`.
This commit is contained in:
parent
b5594bf9a2
commit
515f31339c
2 changed files with 3 additions and 4 deletions
|
@ -33,7 +33,7 @@ int getopt(int argc, char* const* argv, char const* short_options)
|
||||||
for (auto i = 1; i < argc; ++i)
|
for (auto i = 1; i < argc; ++i)
|
||||||
s_args.append({ argv[i], strlen(argv[i]) });
|
s_args.append({ argv[i], strlen(argv[i]) });
|
||||||
|
|
||||||
if (optind == 0 || optreset == 1) {
|
if (optind == 1 || optreset == 1) {
|
||||||
s_parser.reset_state();
|
s_parser.reset_state();
|
||||||
optind = 1;
|
optind = 1;
|
||||||
optreset = 0;
|
optreset = 0;
|
||||||
|
@ -75,7 +75,7 @@ int getopt_long(int argc, char* const* argv, char const* short_options, const st
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optind == 0 || optreset == 1) {
|
if (optind == 1 || optreset == 1) {
|
||||||
s_parser.reset_state();
|
s_parser.reset_state();
|
||||||
optind = 1;
|
optind = 1;
|
||||||
optreset = 0;
|
optreset = 0;
|
||||||
|
|
|
@ -166,8 +166,7 @@ extern int optopt;
|
||||||
// Index of the next argument to process upon a getopt*() call.
|
// Index of the next argument to process upon a getopt*() call.
|
||||||
extern int optind;
|
extern int optind;
|
||||||
// If set, reset the internal state kept by getopt*(). You may also want to set
|
// If set, reset the internal state kept by getopt*(). You may also want to set
|
||||||
// optind to 1 in that case. Alternatively, setting optind to 0 is treated like
|
// optind to 1 in that case.
|
||||||
// doing both of the above.
|
|
||||||
extern int optreset;
|
extern int optreset;
|
||||||
// After parsing an option that accept an argument, set to point to the argument
|
// After parsing an option that accept an argument, set to point to the argument
|
||||||
// value.
|
// value.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue