Initialize global and non-static variables
[fio.git] / lib / getopt_long.c
1 /*
2  * getopt.c
3  *
4  * getopt_long(), or at least a common subset thereof:
5  *
6  * - Option reordering is not supported
7  * - -W foo is not supported
8  * - First optstring character "-" not supported.
9  *
10  * This file was imported from the klibc library from hpa
11  */
12
13 #include <stdint.h>
14 #include <unistd.h>
15 #include <string.h>
16
17 #include "getopt.h"
18
19 char *optarg = NULL;
20 int optind = 0, opterr = 0, optopt = 0;
21
22 static struct getopt_private_state {
23         const char *optptr;
24         const char *last_optstring;
25         char *const *last_argv;
26 } pvt;
27
28 static inline const char *option_matches(const char *arg_str,
29                                          const char *opt_name)
30 {
31         while (*arg_str != '\0' && *arg_str != '=') {
32                 if (*arg_str++ != *opt_name++)
33                         return NULL;
34         }
35
36         if (*opt_name)
37                 return NULL;
38
39         return arg_str;
40 }
41
42 int getopt_long_only(int argc, char *const *argv, const char *optstring,
43                 const struct option *longopts, int *longindex)
44 {
45         const char *carg;
46         const char *osptr;
47         int opt;
48
49         /* getopt() relies on a number of different global state
50            variables, which can make this really confusing if there is
51            more than one use of getopt() in the same program.  This
52            attempts to detect that situation by detecting if the
53            "optstring" or "argv" argument have changed since last time
54            we were called; if so, reinitialize the query state. */
55
56         if (optstring != pvt.last_optstring || argv != pvt.last_argv ||
57             optind < 1 || optind > argc) {
58                 /* optind doesn't match the current query */
59                 pvt.last_optstring = optstring;
60                 pvt.last_argv = argv;
61                 optind = 1;
62                 pvt.optptr = NULL;
63         }
64
65         carg = argv[optind];
66
67         /* First, eliminate all non-option cases */
68
69         if (!carg || carg[0] != '-' || !carg[1])
70                 return -1;
71
72         if (carg[1] == '-') {
73                 const struct option *lo;
74                 const char *opt_end = NULL;
75
76                 optind++;
77
78                 /* Either it's a long option, or it's -- */
79                 if (!carg[2]) {
80                         /* It's -- */
81                         return -1;
82                 }
83
84                 for (lo = longopts; lo->name; lo++) {
85                         if ((opt_end = option_matches(carg+2, lo->name)))
86                             break;
87                 }
88                 if (!opt_end)
89                         return '?';
90
91                 if (longindex)
92                         *longindex = lo-longopts;
93
94                 if (*opt_end == '=') {
95                         if (lo->has_arg)
96                                 optarg = (char *)opt_end+1;
97                         else
98                                 return '?';
99                 } else if (lo->has_arg == 1) {
100                         if (!(optarg = argv[optind]))
101                                 return '?';
102                         optind++;
103                 }
104
105                 if (lo->flag) {
106                         *lo->flag = lo->val;
107                         return 0;
108                 } else {
109                         return lo->val;
110                 }
111         }
112
113         if ((uintptr_t) (pvt.optptr - carg) > (uintptr_t) strlen(carg)) {
114                 /* Someone frobbed optind, change to new opt. */
115                 pvt.optptr = carg + 1;
116         }
117
118         opt = *pvt.optptr++;
119
120         if (opt != ':' && (osptr = strchr(optstring, opt))) {
121                 if (osptr[1] == ':') {
122                         if (*pvt.optptr) {
123                                 /* Argument-taking option with attached
124                                    argument */
125                                 optarg = (char *)pvt.optptr;
126                                 optind++;
127                         } else {
128                                 /* Argument-taking option with non-attached
129                                    argument */
130                                 if (osptr[2] == ':') {
131                                         if (argv[optind + 1]) {
132                                                 optarg = (char *)argv[optind+1];
133                                                 optind += 2;
134                                         } else {
135                                                 optarg = NULL;
136                                                 optind++;
137                                         }
138                                         return opt;
139                                 } else if (argv[optind + 1]) {
140                                         optarg = (char *)argv[optind+1];
141                                         optind += 2;
142                                 } else {
143                                         /* Missing argument */
144                                         optind++;
145                                         return (optstring[0] == ':')
146                                                 ? ':' : '?';
147                                 }
148                         }
149                         return opt;
150                 } else {
151                         /* Non-argument-taking option */
152                         /* pvt.optptr will remember the exact position to
153                            resume at */
154                         if (!*pvt.optptr)
155                                 optind++;
156                         return opt;
157                 }
158         } else {
159                 /* Unknown option */
160                 optopt = opt;
161                 if (!*pvt.optptr)
162                         optind++;
163                 return '?';
164         }
165 }