fix getopt wrongly treating colons in optstring as valid option chars
[musl] / src / misc / getopt_long.c
1 #define _GNU_SOURCE
2 #include <stddef.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 extern int __optpos, __optreset;
8
9 static void permute(char *const *argv, int dest, int src)
10 {
11         char **av = (char **)argv;
12         char *tmp = av[src];
13         int i;
14         for (i=src; i>dest; i--)
15                 av[i] = av[i-1];
16         av[dest] = tmp;
17 }
18
19 void __getopt_msg(const char *, const char *, const char *, size_t);
20
21 static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
22
23 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
24 {
25         int ret, skipped, resumed;
26         if (!optind || __optreset) {
27                 __optreset = 0;
28                 __optpos = 0;
29                 optind = 1;
30         }
31         if (optind >= argc || !argv[optind]) return -1;
32         skipped = optind;
33         if (optstring[0] != '+' && optstring[0] != '-') {
34                 int i;
35                 for (i=optind; ; i++) {
36                         if (i >= argc || !argv[i]) return -1;
37                         if (argv[i][0] == '-' && argv[i][1]) break;
38                 }
39                 optind = i;
40         }
41         resumed = optind;
42         ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
43         if (resumed > skipped) {
44                 int i, cnt = optind-resumed;
45                 for (i=0; i<cnt; i++)
46                         permute(argv, skipped, optind-1);
47                 optind = skipped + cnt;
48         }
49         return ret;
50 }
51
52 static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
53 {
54         optarg = 0;
55         if (longopts && argv[optind][0] == '-' &&
56                 ((longonly && argv[optind][1] && argv[optind][1] != '-') ||
57                  (argv[optind][1] == '-' && argv[optind][2])))
58         {
59                 int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
60                 int i, cnt, match;
61                 char *arg, *opt;
62                 for (cnt=i=0; longopts[i].name; i++) {
63                         const char *name = longopts[i].name;
64                         opt = argv[optind]+1;
65                         if (*opt == '-') opt++;
66                         while (*opt && *opt != '=' && *opt == *name)
67                                 name++, opt++;
68                         if (*opt && *opt != '=') continue;
69                         arg = opt;
70                         match = i;
71                         if (!*name) {
72                                 cnt = 1;
73                                 break;
74                         }
75                         cnt++;
76                 }
77                 if (cnt==1) {
78                         i = match;
79                         opt = arg;
80                         optind++;
81                         if (*opt == '=') {
82                                 if (!longopts[i].has_arg) {
83                                         optopt = longopts[i].val;
84                                         if (colon || !opterr)
85                                                 return '?';
86                                         __getopt_msg(argv[0],
87                                                 ": option does not take an argument: ",
88                                                 longopts[i].name,
89                                                 strlen(longopts[i].name));
90                                         return '?';
91                                 }
92                                 optarg = opt+1;
93                         } else if (longopts[i].has_arg == required_argument) {
94                                 if (!(optarg = argv[optind])) {
95                                         optopt = longopts[i].val;
96                                         if (colon) return ':';
97                                         if (!opterr) return '?';
98                                         __getopt_msg(argv[0],
99                                                 ": option requires an argument: ",
100                                                 longopts[i].name,
101                                                 strlen(longopts[i].name));
102                                         return '?';
103                                 }
104                                 optind++;
105                         }
106                         if (idx) *idx = i;
107                         if (longopts[i].flag) {
108                                 *longopts[i].flag = longopts[i].val;
109                                 return 0;
110                         }
111                         return longopts[i].val;
112                 }
113                 if (argv[optind][1] == '-') {
114                         optopt = 0;
115                         if (!colon && opterr)
116                                 __getopt_msg(argv[0], cnt ?
117                                         ": option is ambiguous: " :
118                                         ": unrecognized option: ",
119                                         argv[optind]+2,
120                                         strlen(argv[optind]+2));
121                         optind++;
122                         return '?';
123                 }
124         }
125         return getopt(argc, argv, optstring);
126 }
127
128 int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
129 {
130         return __getopt_long(argc, argv, optstring, longopts, idx, 0);
131 }
132
133 int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
134 {
135         return __getopt_long(argc, argv, optstring, longopts, idx, 1);
136 }