summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/getopt.cpp
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2023-03-12 13:36:33 +0100
committerAndreas Kling <kling@serenityos.org>2023-03-12 22:09:09 +0100
commit515f31339cac323d0939b8e9c6c72089fcd7b278 (patch)
tree1dccf15df19578acfd15196db8ae00934da6c228 /Userland/Libraries/LibC/getopt.cpp
parentb5594bf9a2587e76a759e668fb825748d955a405 (diff)
downloadserenity-515f31339cac323d0939b8e9c6c72089fcd7b278.zip
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`.
Diffstat (limited to 'Userland/Libraries/LibC/getopt.cpp')
-rw-r--r--Userland/Libraries/LibC/getopt.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/Userland/Libraries/LibC/getopt.cpp b/Userland/Libraries/LibC/getopt.cpp
index 6e8d1a09e9..4f9a6bc816 100644
--- a/Userland/Libraries/LibC/getopt.cpp
+++ b/Userland/Libraries/LibC/getopt.cpp
@@ -33,7 +33,7 @@ int getopt(int argc, char* const* argv, char const* short_options)
for (auto i = 1; i < argc; ++i)
s_args.append({ argv[i], strlen(argv[i]) });
- if (optind == 0 || optreset == 1) {
+ if (optind == 1 || optreset == 1) {
s_parser.reset_state();
optind = 1;
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();
optind = 1;
optreset = 0;