]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: reset optind to 0 when GNU extensions in optstring are used
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 21 Mar 2023 01:05:33 +0000 (10:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 29 Mar 2023 01:34:41 +0000 (10:34 +0900)
Otherwise, if getopt() and friends are used before parse_argv(), then
the GNU extensions may be ignored.

This should not change any behavior at least now, as we usually use
getopt_long() only once per invocation. But in the next commit,
getopt_long() will be used for other arrays, hence this change will
become necessary.

12 files changed:
src/activate/activate.c
src/ask-password/ask-password.c
src/cgls/cgls.c
src/journal/cat.c
src/login/inhibit.c
src/machine/machinectl.c
src/nspawn/nspawn.c
src/run/run.c
src/shutdown/shutdown.c
src/udev/udevadm-lock.c
src/udev/udevadm.c
src/userdb/userdbctl.c

index 4a639703260764c219c14a590895ecff1c2b9f8f..1caa30d7d474f7058588124f301b4b0dd11007e5 100644 (file)
@@ -347,6 +347,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+hl:aE:d", options, NULL)) >= 0)
                 switch (c) {
                 case 'h':
index f161e659730d863a66d36532fc5496e1b5be1dc3..e9c09b78252b8b567124070e08491c822d02014d 100644 (file)
@@ -108,6 +108,10 @@ static int parse_argv(int argc, char *argv[]) {
 
         /* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
          * not. */
+
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+hen", options, NULL)) >= 0)
 
                 switch (c) {
index 32780c606a8bf7506968d5d8707b3610ecc4f301..a34b0aa604b0da2d2d8dbc873afefa744e4a1feb 100644 (file)
@@ -93,6 +93,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "-hkalM:u::xc", options, NULL)) >= 0)
 
                 switch (c) {
index 5908758a8f03ade9be2b4fb0b3ca188aa8b9f991..d3f7785ad3409fbe9ef0e4b62683c5878f5b3a69 100644 (file)
@@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
 
                 switch (c) {
index 7cd2fd3e6688b9cdac378140c9cc9628d02bc10a..25ba848492dee9527de92a1c2cd460d160632b6a 100644 (file)
@@ -210,6 +210,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
 
                 switch (c) {
index 6a29a32bcd22bf97a069bb04e561e2d1bfbdcdef..447f1a70b0a0f8a85d02a8521a753d0b877c5399 100644 (file)
@@ -2720,6 +2720,10 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
+
         for (;;) {
                 static const char option_string[] = "-hp:als:H:M:qn:o:E:";
 
index 982dffd1b89c1b7d99532edddf305b80de3e5597..75349c3b0ecd9657d92566f68094a8502b152899 100644 (file)
@@ -815,6 +815,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:P", options, NULL)) >= 0)
                 switch (c) {
 
index 8377c2e8cdd0a4bbc60670b9ebab3d17b62e6f19..ee4a151a4abc468705b61334f189ed28c8a86f9d 100644 (file)
@@ -242,6 +242,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdSu:", options, NULL)) >= 0)
 
                 switch (c) {
index 42111d27728892762baee4365de031988a004bc7..5dee1b3a926c00c7916c3e07a11c46353662a073 100644 (file)
@@ -75,6 +75,10 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
+
         /* "-" prevents getopt from permuting argv[] and moving the verb away
          * from argv[1]. Our interface to initrd promises it'll be there. */
         while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0)
index d19e7561f81f166cabefc3007f732d8ee3d56e6c..6d8a5c336f5b58e15974c4686cea49062ebcf1a6 100644 (file)
@@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, arg_print ? "hVd:b:t:p" : "+hVd:b:t:p", options, NULL)) >= 0)
 
                 switch (c) {
index 30a72f2a429a7b7d064802928d51a5eebd433968..b803f7bb0f5f0e29e020739bf49237174e788313 100644 (file)
@@ -62,6 +62,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
         while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
                 switch (c) {
 
index eab0c3af1515987c6b1ef053fca7260f52613969..67675b4b7f7ea6237e5e9585e5b52650ef468827 100644 (file)
@@ -1150,6 +1150,10 @@ static int parse_argv(int argc, char *argv[]) {
                 arg_services = l;
         }
 
+        /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
+         * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
+        optind = 0;
+
         for (;;) {
                 int c;