115 reverse_argv_elements (
char **argv,
int num)
120 for (i = 0; i < (num >> 1); i++)
123 argv[i] = argv[num - i - 1];
124 argv[num - i - 1] = tmp;
130 permute (
char **argv,
int len1,
int len2)
132 reverse_argv_elements (argv, len1);
133 reverse_argv_elements (argv, len1 + len2);
134 reverse_argv_elements (argv, len2);
139 is_option (
char *argv_element,
int only)
141 return ((argv_element == NULL)
142 || (argv_element[0] ==
'-') || (only && argv_element[0] ==
'+'));
147 getopt_internal (
int argc,
char **argv,
const char * shortopts,
148 const struct option * longopts,
int *longind,
int only)
151 static size_t optwhere = 0;
152 size_t permute_from = 0;
155 size_t match_chars = 0;
156 char *possible_arg = NULL;
157 int longopt_match = -1;
163 if (argc == 0 || argv == NULL || (shortopts == NULL && longopts == NULL))
167 if (strcmp (argv[
optind],
"--") == 0)
174 optind = optwhere = 1;
177 if (shortopts != NULL && (*shortopts ==
'-' || *shortopts ==
'+'))
196 while (!is_option (argv[optind], only))
201 if (argv[optind] == NULL)
204 optind = permute_from;
207 else if (strcmp (argv[optind],
"--") == 0)
210 permute (argv + permute_from, num_nonopts, 1);
211 optind = permute_from + 1;
216 if (!is_option (argv[optind], only))
223 if (!is_option (argv[optind], only))
232 && (memcmp (argv[optind],
"--", 2) == 0
233 || (only && argv[optind][0] ==
'+')) && optwhere == 1)
236 if (memcmp (argv[optind],
"--", 2) == 0)
239 possible_arg = strchr (argv[optind] + optwhere,
'=');
240 if (possible_arg == NULL)
243 match_chars = strlen (argv[optind]);
244 possible_arg = argv[
optind] + match_chars;
245 match_chars = match_chars - optwhere;
248 match_chars = (possible_arg - argv[
optind]) - optwhere;
249 for (optindex = 0; longopts[optindex].
name != NULL; optindex++)
251 if (memcmp (argv[optind] + optwhere,
252 longopts[optindex].name, match_chars) == 0)
255 if (match_chars == (
int) (strlen (longopts[optindex].name)))
257 longopt_match = optindex;
263 if (longopt_match < 0)
264 longopt_match = optindex;
269 fprintf (stderr,
"%s: option `%s' is ambiguous "
270 "(could be `--%s' or `--%s')\n",
273 longopts[longopt_match].name,
274 longopts[optindex].name);
280 if (longopt_match >= 0)
281 has_arg = longopts[longopt_match].
has_arg;
284 if (longopt_match < 0 && shortopts != NULL)
286 cp = strchr (shortopts, argv[optind][optwhere]);
292 "%s: invalid option -- `-%c'\n",
293 argv[0], argv[optind][optwhere]);
295 if (argv[optind][optwhere] ==
'\0')
302 has_arg = ((cp[1] ==
':')
304 possible_arg = argv[
optind] + optwhere + 1;
312 if (*possible_arg ==
'=')
314 if (*possible_arg !=
'\0')
323 if (*possible_arg ==
'=')
325 if (*possible_arg !=
'\0')
330 else if (optind + 1 >= argc)
334 fprintf (stderr,
"%s: argument required for option `", argv[0]);
335 if (longopt_match >= 0)
336 fprintf (stderr,
"--%s'\n", longopts[longopt_match].name);
338 fprintf (stderr,
"-%c'\n", *cp);
345 optarg = argv[optind + 1];
351 if (longopt_match < 0)
354 if (argv[optind][optwhere] ==
'\0')
364 if (ordering ==
PERMUTE && optwhere == 1 && num_nonopts != 0)
366 permute (argv + permute_from, num_nonopts, 1 + arg_next);
367 optind = permute_from + 1 + arg_next;
369 else if (optwhere == 1)
370 optind = optind + 1 + arg_next;
373 if (longopt_match >= 0)
376 *longind = longopt_match;
377 if (longopts[longopt_match].flag != NULL)
379 *(longopts[longopt_match].
flag) = longopts[longopt_match].val;
383 return longopts[longopt_match].
val;
391 getopt (
int argc,
char **argv,
const char * optstring)
393 return getopt_internal (argc, argv, optstring, NULL, NULL, 0);
400 const struct option * longopts,
int *longind)
402 return getopt_internal (argc, argv, shortopts, longopts, longind, 0);
407 const struct option * longopts,
int *longind)
409 return getopt_internal (argc, argv, shortopts, longopts, longind, 1);