5 \rchar *
\r optarg = NULL;
7 \r\rint
\r optopt = '?';
9 \r\rstatic char *
\r nextchar;
10 \r\rstatic
\r int first_nonopt;
11 \r\rstatic int
\r last_nonopt;
12 \r\r\rstatic enum
\r {
\rREQUIRE_ORDER,
\rPERMUTE,
\rRETURN_IN_ORDER
\r
14 \r\r\rstatic const char *
\r __getopt_initialize(const char *optstring);
16 __getopt_internal(int argc, char *const *argv, const char *optstring,
17 const struct option *longopts, int *longind,
19 \r\rstatic void
\r __exchange(char **argv);
20 \r\rint
\r getopt(int argc, char *const argv[], const char *optstring)
\r
22 \rreturn __getopt_internal(argc, argv, optstring,
23 (const struct option *) 0, (int *) 0, 0);
24 \r}
\r\rstatic const char *
\r __getopt_initialize(const char *optstring)
\r
27 /* Start processing options with ARGV-element 1 (since ARGV-element 0
\r
28 is the program name); the sequence of previously skipped
\r
29 non-option ARGV-elements is empty. */
\r
30 \rfirst_nonopt = last_nonopt = optind = 1;
33 /* Determine how to handle the ordering of options and nonoptions. */
\r
34 \rif (optstring[0] == '-')
36 \rordering = RETURN_IN_ORDER;
40 /* si la chaîne d'options commence par un + alors la fonction getopt() s'arrête
\r
41 * dès qu'un argument de la ligne de commande n'est pas une option
\r
43 else if (optstring[0] == '+')
45 \rordering = REQUIRE_ORDER;
57 __getopt_internal(int argc, char *const *argv, const char *optstring,
58 const struct option *longopts, int *longind,
63 \roptstring = __getopt_initialize(optstring);
64 \r\rif (nextchar == NULL || *nextchar == '\0')
67 /* Advance to the next ARGV-element. */
\r
68 \rif (ordering == PERMUTE)
71 /* If we have just processed some options following some non-options,
\r
72 __exchange them so that the options come first. */
\r
73 \rif (first_nonopt != last_nonopt && last_nonopt != optind)
74 \r__exchange((char **) argv);
76 else if (last_nonopt != optind)
77 \rfirst_nonopt = optind;
79 /* Skip any additional non-options
\r
80 and extend the range of non-options previously skipped. */
\r
81 \rwhile (optind < argc
82 && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
84 \r\rlast_nonopt = optind;
87 /* The special ARGV-element `--' means premature end of options.
\r
88 Skip it like a null option,
\r
89 then __exchange with previous non-options as if it were an option,
\r
90 then skip everything else like a non-option. */
\r
91 \rif (optind != argc && !strcmp(argv[optind], "--"))
94 \r\rif (first_nonopt != last_nonopt && last_nonopt != optind)
95 \r__exchange((char **) argv);
97 else if (first_nonopt == last_nonopt)
98 \rfirst_nonopt = optind;
99 \r\rlast_nonopt = argc;
103 /* If we have done all the ARGV-elements, stop the scan
\r
104 and back over any non-options that we skipped and permuted. */
\r
105 \rif (optind == argc)
108 /* Set the next-arg-index to point at the non-options
\r
109 that we previously skipped, so the caller will digest them. */
\r
110 if (first_nonopt != last_nonopt)
111 \roptind = first_nonopt;
115 /* If we have come to a non-option and did not permute it,
\r
116 either stop the scan or describe it to the caller and pass it by. */
\r
117 \rif ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
119 \rif (ordering == REQUIRE_ORDER)
121 \roptarg = argv[optind++];
125 /* We have found another option-ARGV-element.
\r
126 Skip the initial punctuation. */
\r
128 (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));
131 /* Decode the current option-ARGV-element. */
\r
133 /* Check whether the ARGV-element is a long option.
\r
135 If long_only and the ARGV-element has the form "-f", where f is
\r
136 a valid short option, don't consider it an abbreviated form of
\r
137 a long option that starts with f. Otherwise there would be no
\r
138 way to give the -f short option.
\r
140 On the other hand, if there's a long option "fubar" and
\r
141 the ARGV-element is "-fu", do consider that an abbreviation of
\r
142 the long option, just like "--fu", and not "-f" with arg "u".
\r
144 This distinction seems to be the most useful approach. */
\r
145 \rif (longopts != NULL
146 && (argv[optind][1] == '-'
149 || !strchr(optstring, argv[optind][1])))))
152 \rconst struct option *p;
153 \rconst struct option *pfound = NULL;
158 \r\rfor (nameend = nextchar; *nameend != '\0' && *nameend != '=';
163 /* Test all long options for either exact match
\r
164 or abbreviated matches. */
\r
165 for (p = longopts, option_index = 0; p->name; p++, option_index++)
167 \rif (!strncmp(p->name, nextchar, nameend - nextchar))
169 \r\rif (nameend - nextchar == strlen(p->name))
172 /* Exact match found. */
\r
174 \rindfound = option_index;
179 else if (pfound == NULL)
182 /* First nonexact match found. */
\r
187 indfound = option_index;
\r
195 /* Second or later nonexact match found. */
\r
200 \r\rif (ambig && !exact)
203 \rfprintf(stderr, "error : %s: option `%s' is ambiguous\n",
204 argv[0], argv[optind]);
205 \r\rnextchar += strlen(nextchar);
209 \r\rif (pfound != NULL)
211 \roption_index = indfound;
216 /* Don't test has_arg with >, because some C compilers don't
\r
217 allow it to be used on enums. */
\r
219 \roptarg = nameend + 1;
225 \rif (argv[optind - 1][1] == '-')
229 "error : %s: option `--%s' doesn't allow an argument\n",
230 argv[0], pfound->name);
234 /* +option or -option */
\r
236 "error : %s: option `%c%s' doesn't allow an argument\n",
237 argv[0], argv[optind - 1][0], pfound->name);
239 \r\rnextchar += strlen(nextchar);
244 else if (pfound->has_arg == 1)
247 \roptarg = argv[optind++];
253 "error : %s: option `%s' requires an argument\n",
254 argv[0], argv[optind - 1]);
255 \r\rnextchar += strlen(nextchar);
256 \rreturn optstring[0] == ':' ? ':' : '?';
259 \r\rnextchar += strlen(nextchar);
260 \r\rif (longind != NULL)
261 \r*longind = option_index;
262 \r\rif (pfound->flag)
264 \r*(pfound->flag) = pfound->val;
267 \r\rreturn pfound->val;
270 /* Can't find it as a long option. If this is not getopt_long_only,
\r
271 or the option starts with '--' or is not a valid short
\r
272 option, then it's an error.
\r
273 Otherwise interpret it as a short option. */
\r
274 if (!long_only || argv[optind][1] == '-'
275 || strchr(optstring, *nextchar) == NULL)
279 \rif (argv[optind][1] == '-')
282 fprintf(stderr, "error : %s: unrecognized option `--%s'\n",
287 /* +option or -option */
\r
288 fprintf(stderr, "error : %s: unrecognized option `%c%s'\n",
289 argv[0], argv[optind][0], nextchar);
291 \r\rnextchar = (char *) "";
297 /* Look at and handle the next short option-character. */
\r
299 \rchar c = *nextchar++;
300 \rchar *temp = strchr(optstring, c);
302 /* Increment `optind' when we start to process its last character. */
\r
303 if (*nextchar == '\0')
305 \r\rif (temp == NULL || c == ':')
308 \rfprintf(stderr, "error : %s: invalid option -- %c\n", argv[0],
313 \r\rif (temp[1] == ':')
315 \rif (temp[2] == ':')
318 /* This is an option that accepts an argument optionally. */
\r
319 if (*nextchar != '\0')
333 /* This is an option that requires an argument. */
\r
334 if (*nextchar != '\0')
338 /* If we end this ARGV-element by taking the rest as an arg,
\r
339 we must advance to the next element now. */
\r
343 else if (optind == argc)
348 /* 1003.2 specifies the format of this message. */
\r
350 "error : %s: option requires an argument -- %c\n",
354 \r\rif (optstring[0] == ':')
363 /* We already incremented `optind' once;
\r
364 increment it again when taking next ARGV-elt as argument. */
\r
365 optarg = argv[optind++];
373 \r\r\rstatic void
\r __exchange(char **argv)
\r
375 \rint bottom = first_nonopt;
376 \rint middle = last_nonopt;
380 /* Exchange the shorter segment with the far end of the longer segment.
\r
381 That puts the shorter segment into the right place.
\r
382 It leaves the longer segment in the right place overall,
\r
383 but it consists of two parts that need to be swapped next. */
\r
384 \rwhile (top > middle && middle > bottom)
386 \rif (top - middle > middle - bottom)
389 /* Bottom segment is the short one. */
\r
390 int len = middle - bottom;
393 /* Swap it with the top part of the top segment. */
\r
394 for (i = 0; i < len; i++)
396 \rtem = argv[bottom + i];
397 \rargv[bottom + i] = argv[top - (middle - bottom) + i];
398 \rargv[top - (middle - bottom) + i] = tem;
401 /* Exclude the moved bottom segment from further swapping. */
\r
408 /* Top segment is the short one. */
\r
409 int len = top - middle;
412 /* Swap it with the bottom part of the bottom segment. */
\r
413 for (i = 0; i < len; i++)
415 \rtem = argv[bottom + i];
416 \rargv[bottom + i] = argv[middle + i];
417 \rargv[middle + i] = tem;
420 /* Exclude the moved top segment from further swapping. */
\r
425 /* Update records for the slots the non-options now occupy. */
\r
426 \rfirst_nonopt += (optind - last_nonopt);
427 \rlast_nonopt = optind;
431 getopt_long(int argc, char *const *argv, const char *options,
432 const struct option *long_options, int *opt_index)
\r
434 \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,
439 getopt_long_only(int argc, char *const *argv, const char *options,
440 const struct option *long_options, int *opt_index)
\r
442 \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,