add support for non-option arguments extension to getopt
[musl] / src / misc / getopt_long.c
index 6d3a4a6..3d318ce 100644 (file)
@@ -3,11 +3,19 @@
 #include <getopt.h>
 #include <stdio.h>
 
+extern int __optpos, __optreset;
+
 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
 {
-       if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
-       if ((longonly && argv[optind][1]) ||
-               (argv[optind][1] == '-' && argv[optind][2]))
+       if (!optind || __optreset) {
+               __optreset = 0;
+               __optpos = 0;
+               optind = 1;
+       }
+       if (optind >= argc || !argv[optind]) return -1;
+       if (argv[optind][0] == '-' &&
+               ((longonly && argv[optind][1]) ||
+                (argv[optind][1] == '-' && argv[optind][2])))
        {
                int i;
                for (i=0; longopts[i].name; i++) {