CHANGES: document is address local fix
[tftp-hpa.git] / lib / getopt_long.c
blob49d12742afded6e6836a1496e544787707301133
1 /*
2 * getopt_long.c
4 * getopt_long(), or at least a common subset thereof:
6 * - Option reordering is not supported
7 * - -W foo is not supported
8 * - First optstring character "-" not supported.
9 */
11 #include "config.h"
13 char *optarg;
14 int optind, opterr, optopt;
16 static struct getopt_private_state {
17 const char *optptr;
18 const char *last_optstring;
19 char *const *last_argv;
20 } pvt;
22 static inline const char *option_matches(const char *arg_str,
23 const char *opt_name)
25 while (*arg_str != '\0' && *arg_str != '=') {
26 if (*arg_str++ != *opt_name++)
27 return NULL;
30 if (*opt_name)
31 return NULL;
33 return arg_str;
36 int getopt_long(int argc, char *const *argv, const char *optstring,
37 const struct option *longopts, int *longindex)
39 const char *carg;
40 const char *osptr;
41 int opt;
43 /* getopt() relies on a number of different global state
44 variables, which can make this really confusing if there is
45 more than one use of getopt() in the same program. This
46 attempts to detect that situation by detecting if the
47 "optstring" or "argv" argument have changed since last time
48 we were called; if so, reinitialize the query state. */
50 if (optstring != pvt.last_optstring || argv != pvt.last_argv ||
51 optind < 1 || optind > argc) {
52 /* optind doesn't match the current query */
53 pvt.last_optstring = optstring;
54 pvt.last_argv = argv;
55 optind = 1;
56 pvt.optptr = NULL;
59 carg = argv[optind];
61 /* First, eliminate all non-option cases */
63 if (!carg || carg[0] != '-' || !carg[1])
64 return -1;
66 if (carg[1] == '-') {
67 const struct option *lo;
68 const char *opt_end = NULL;
70 optind++;
72 /* Either it's a long option, or it's -- */
73 if (!carg[2]) {
74 /* It's -- */
75 return -1;
78 for (lo = longopts; lo->name; lo++) {
79 if ((opt_end = option_matches(carg+2, lo->name)))
80 break;
82 if (!opt_end)
83 return '?';
85 if (longindex)
86 *longindex = lo-longopts;
88 if (*opt_end == '=') {
89 if (lo->has_arg)
90 optarg = (char *)opt_end+1;
91 else
92 return '?';
93 } else if (lo->has_arg == 1) {
94 if (!(optarg = argv[optind]))
95 return '?';
96 optind++;
99 if (lo->flag) {
100 *lo->flag = lo->val;
101 return 0;
102 } else {
103 return lo->val;
107 if ((uintptr_t) (pvt.optptr - carg) > (uintptr_t) strlen(carg)) {
108 /* Someone frobbed optind, change to new opt. */
109 pvt.optptr = carg + 1;
112 opt = *pvt.optptr++;
114 if (opt != ':' && (osptr = strchr(optstring, opt))) {
115 if (osptr[1] == ':') {
116 if (*pvt.optptr) {
117 /* Argument-taking option with attached
118 argument */
119 optarg = (char *)pvt.optptr;
120 optind++;
121 } else {
122 /* Argument-taking option with non-attached
123 argument */
124 if (argv[optind + 1]) {
125 optarg = (char *)argv[optind+1];
126 optind += 2;
127 } else {
128 /* Missing argument */
129 optind++;
130 return (optstring[0] == ':')
131 ? ':' : '?';
134 return opt;
135 } else {
136 /* Non-argument-taking option */
137 /* pvt.optptr will remember the exact position to
138 resume at */
139 if (!*pvt.optptr)
140 optind++;
141 return opt;
143 } else {
144 /* Unknown option */
145 optopt = opt;
146 if (!*pvt.optptr)
147 optind++;
148 return '?';