libbb: move nuke_str() from passwd into libbb
[oweals/busybox.git] / libbb / getopt32.c
index f3f1cfcbaac8e9cde4b8b98e57e48759a9c63408..d0e83d88ee4d31090527a7f2f632907e9ebe0312 100644 (file)
@@ -7,7 +7,9 @@
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
-#include <getopt.h>
+#if ENABLE_LONG_OPTS || ENABLE_FEATURE_GETOPT_LONG
+# include <getopt.h>
+#endif
 #include "libbb.h"
 
 /*      Documentation
@@ -465,13 +467,17 @@ getopt32(char **argv, const char *applet_opts, ...)
                }
                for (on_off = complementary; on_off->opt_char; on_off++)
                        if (on_off->opt_char == *s)
-                               break;
+                               goto found_opt;
+               /* Without this, diagnostic of such bugs is not easy */
+               bb_error_msg_and_die("NO OPT %c!", *s);
+ found_opt:
                if (c == ':' && s[2] == ':') {
                        on_off->param_type = PARAM_LIST;
                        continue;
                }
                if (c == '+' && (s[2] == ':' || s[2] == '\0')) {
                        on_off->param_type = PARAM_INT;
+                       s++;
                        continue;
                }
                if (c == ':' || c == '\0') {
@@ -542,8 +548,6 @@ getopt32(char **argv, const char *applet_opts, ...)
 #endif
        /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
 
-       pargv = NULL;
-
        /* Note: just "getopt() <= 0" will not work well for
         * "fake" short options, like this one:
         * wget $'-\203' "Test: test" http://kernel.org/
@@ -574,19 +578,16 @@ getopt32(char **argv, const char *applet_opts, ...)
                flags ^= trigger;
                if (on_off->counter)
                        (*(on_off->counter))++;
-               if (on_off->param_type == PARAM_LIST) {
-                       if (optarg)
+               if (optarg) {
+                       if (on_off->param_type == PARAM_LIST) {
                                llist_add_to_end((llist_t **)(on_off->optarg), optarg);
-               } else if (on_off->param_type == PARAM_INT) {
-                       if (optarg)
+                       } else if (on_off->param_type == PARAM_INT) {
 //TODO: xatoi_positive indirectly pulls in printf machinery
                                *(unsigned*)(on_off->optarg) = xatoi_positive(optarg);
-               } else if (on_off->optarg) {
-                       if (optarg)
+                       } else if (on_off->optarg) {
                                *(char **)(on_off->optarg) = optarg;
+                       }
                }
-               if (pargv != NULL)
-                       break;
        }
 
        /* check depending requires for given options */