1 #define _GNU_SOURCE
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <getopt.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include "stdio_impl.h"
9
10 extern int __optpos, __optreset;
11
12 int __posix_getopt(int argc, char * const argv[], const char *optstring);
13
permute(char * const * argv,int dest,int src)14 static void permute(char *const *argv, int dest, int src)
15 {
16 char **av = (char **)argv;
17 char *tmp = av[src];
18 int i;
19 for (i=src; i>dest; i--)
20 av[i] = av[i-1];
21 av[dest] = tmp;
22 }
23
24 static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
25
__getopt_long(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx,int longonly)26 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
27 {
28 int ret, skipped, resumed;
29 if (!optind || __optreset) {
30 __optreset = 0;
31 __optpos = 0;
32 optind = 1;
33 }
34 if (optind >= argc || !argv[optind]) return -1;
35 skipped = optind;
36 if (optstring[0] != '+' && optstring[0] != '-') {
37 int i;
38 for (i=optind; ; i++) {
39 if (i >= argc || !argv[i]) return -1;
40 if (argv[i][0] == '-' && argv[i][1]) break;
41 }
42 optind = i;
43 }
44 resumed = optind;
45 ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
46 if (resumed > skipped) {
47 int i, cnt = optind-resumed;
48 for (i=0; i<cnt; i++)
49 permute(argv, skipped, optind-1);
50 optind = skipped + cnt;
51 }
52 return ret;
53 }
54
__getopt_long_core(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx,int longonly)55 static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
56 {
57 optarg = 0;
58 if (longopts && argv[optind][0] == '-' &&
59 ((longonly && argv[optind][1] && argv[optind][1] != '-') ||
60 (argv[optind][1] == '-' && argv[optind][2])))
61 {
62 int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
63 int i, cnt, match;
64 char *arg, *opt, *start = argv[optind]+1;
65 for (cnt=i=0; longopts[i].name; i++) {
66 const char *name = longopts[i].name;
67 opt = start;
68 if (*opt == '-') opt++;
69 while (*opt && *opt != '=' && *opt == *name)
70 name++, opt++;
71 if (*opt && *opt != '=') continue;
72 arg = opt;
73 match = i;
74 if (!*name) {
75 cnt = 1;
76 break;
77 }
78 cnt++;
79 }
80 if (cnt==1 && longonly && arg-start == mblen(start, MB_LEN_MAX)) {
81 int l = arg-start;
82 for (i=0; optstring[i]; i++) {
83 int j;
84 for (j=0; j<l && start[j]==optstring[i+j]; j++);
85 if (j==l) {
86 cnt++;
87 break;
88 }
89 }
90 }
91 if (cnt==1) {
92 i = match;
93 opt = arg;
94 optind++;
95 if (*opt == '=') {
96 if (!longopts[i].has_arg) {
97 optopt = longopts[i].val;
98 if (colon || !opterr)
99 return '?';
100 __getopt_msg(argv[0],
101 ": option does not take an argument: ",
102 longopts[i].name,
103 strlen(longopts[i].name));
104 return '?';
105 }
106 optarg = opt+1;
107 } else if (longopts[i].has_arg == required_argument) {
108 if (!(optarg = argv[optind])) {
109 optopt = longopts[i].val;
110 if (colon) return ':';
111 if (!opterr) return '?';
112 __getopt_msg(argv[0],
113 ": option requires an argument: ",
114 longopts[i].name,
115 strlen(longopts[i].name));
116 return '?';
117 }
118 optind++;
119 }
120 if (idx) *idx = i;
121 if (longopts[i].flag) {
122 *longopts[i].flag = longopts[i].val;
123 return 0;
124 }
125 return longopts[i].val;
126 }
127 if (argv[optind][1] == '-') {
128 optopt = 0;
129 if (!colon && opterr)
130 __getopt_msg(argv[0], cnt ?
131 ": option is ambiguous: " :
132 ": unrecognized option: ",
133 argv[optind]+2,
134 strlen(argv[optind]+2));
135 optind++;
136 return '?';
137 }
138 }
139 return __posix_getopt(argc, argv, optstring);
140 }
141
getopt_long(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx)142 int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
143 {
144 return __getopt_long(argc, argv, optstring, longopts, idx, 0);
145 }
146
getopt_long_only(int argc,char * const * argv,const char * optstring,const struct option * longopts,int * idx)147 int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
148 {
149 return __getopt_long(argc, argv, optstring, longopts, idx, 1);
150 }
151
152 /* ANDROID CHANGE: implement getopt via getopt_long to continue parsing options
153 * after the first non-option argument to match the user visible behavior of
154 * glibc.
155 */
getopt(int argc,char * const argv[],const char * optstring)156 int getopt(int argc, char * const argv[], const char *optstring)
157 {
158 static int posixly_correct = -1;
159
160 if (posixly_correct == -1 || __optreset)
161 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
162
163 if (posixly_correct)
164 return __posix_getopt(argc, argv, optstring);
165 else
166 return __getopt_long(argc, argv, optstring, NULL, NULL, 0);
167 }
168