use syscall_arg_t and __scc macro for arguments to __alt_socketcall
[musl] / src / misc / getopt_long.c
index 480c001..6949ab1 100644 (file)
@@ -1,8 +1,11 @@
 #define _GNU_SOURCE
 #include <stddef.h>
+#include <stdlib.h>
+#include <limits.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <string.h>
+#include "stdio_impl.h"
 
 extern int __optpos, __optreset;
 
@@ -16,8 +19,6 @@ static void permute(char *const *argv, int dest, int src)
        av[dest] = tmp;
 }
 
-void __getopt_msg(const char *, const char *, const char *, size_t);
-
 static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
 
 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
@@ -53,18 +54,20 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
 {
        optarg = 0;
        if (longopts && argv[optind][0] == '-' &&
-               ((longonly && argv[optind][1]) ||
+               ((longonly && argv[optind][1] && argv[optind][1] != '-') ||
                 (argv[optind][1] == '-' && argv[optind][2])))
        {
                int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
                int i, cnt, match;
-               char *opt;
+               char *arg, *opt, *start = argv[optind]+1;
                for (cnt=i=0; longopts[i].name; i++) {
                        const char *name = longopts[i].name;
-                       opt = argv[optind]+1;
+                       opt = start;
                        if (*opt == '-') opt++;
-                       for (; *name && *name == *opt; name++, opt++);
+                       while (*opt && *opt != '=' && *opt == *name)
+                               name++, opt++;
                        if (*opt && *opt != '=') continue;
+                       arg = opt;
                        match = i;
                        if (!*name) {
                                cnt = 1;
@@ -72,12 +75,24 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
                        }
                        cnt++;
                }
+               if (cnt==1 && longonly && arg-start == mblen(start, MB_LEN_MAX)) {
+                       int l = arg-start;
+                       for (i=0; optstring[i]; i++) {
+                               int j;
+                               for (j=0; j<l && start[j]==optstring[i+j]; j++);
+                               if (j==l) {
+                                       cnt++;
+                                       break;
+                               }
+                       }
+               }
                if (cnt==1) {
                        i = match;
+                       opt = arg;
                        optind++;
-                       optopt = longopts[i].val;
                        if (*opt == '=') {
                                if (!longopts[i].has_arg) {
+                                       optopt = longopts[i].val;
                                        if (colon || !opterr)
                                                return '?';
                                        __getopt_msg(argv[0],
@@ -89,6 +104,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
                                optarg = opt+1;
                        } else if (longopts[i].has_arg == required_argument) {
                                if (!(optarg = argv[optind])) {
+                                       optopt = longopts[i].val;
                                        if (colon) return ':';
                                        if (!opterr) return '?';
                                        __getopt_msg(argv[0],
@@ -107,6 +123,7 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
                        return longopts[i].val;
                }
                if (argv[optind][1] == '-') {
+                       optopt = 0;
                        if (!colon && opterr)
                                __getopt_msg(argv[0], cnt ?
                                        ": option is ambiguous: " :