lib/getopt_long: clear 'optarg' every time
[fio.git] / lib / getopt_long.c
index 3d80627f5395c6bba4d5d28ac73170c46290e198..11d879ad2bfe70f4ab76d06bcc8aad20d36c4c16 100644 (file)
@@ -6,15 +6,19 @@
  * - Option reordering is not supported
  * - -W foo is not supported
  * - First optstring character "-" not supported.
+ *
+ * This file was imported from the klibc library from hpa
  */
 
 #include <stdint.h>
 #include <unistd.h>
 #include <string.h>
-#include <getopt.h>
 
-char *optarg;
-int optind, opterr, optopt;
+#include "getopt.h"
+
+char *optarg = NULL;
+int optind = 0, opterr = 0, optopt = 0;
+
 static struct getopt_private_state {
        const char *optptr;
        const char *last_optstring;
@@ -42,6 +46,8 @@ int getopt_long_only(int argc, char *const *argv, const char *optstring,
        const char *osptr;
        int opt;
 
+       optarg = NULL;
+
        /* getopt() relies on a number of different global state
           variables, which can make this really confusing if there is
           more than one use of getopt() in the same program.  This
@@ -123,7 +129,16 @@ int getopt_long_only(int argc, char *const *argv, const char *optstring,
                        } else {
                                /* Argument-taking option with non-attached
                                   argument */
-                               if (argv[optind + 1]) {
+                               if (osptr[2] == ':') {
+                                       if (argv[optind + 1]) {
+                                               optarg = (char *)argv[optind+1];
+                                               optind += 2;
+                                       } else {
+                                               optarg = NULL;
+                                               optind++;
+                                       }
+                                       return opt;
+                               } else if (argv[optind + 1]) {
                                        optarg = (char *)argv[optind+1];
                                        optind += 2;
                                } else {