34#if !defined (__STDC__) || !__STDC__
52#define GETOPT_INTERFACE_VERSION 2
53#if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
54#include <gnu-versions.h>
55#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
92# define _(msgid) gettext (msgid)
94# define _(msgid) (msgid)
150static char *nextchar;
198static char *posixly_correct;
200#ifdef __GNU_LIBRARY__
206#define my_index strchr
233#if !defined (__STDC__) || !__STDC__
236extern int strlen (
const char *);
248static int first_nonopt;
249static int last_nonopt;
256extern char *__getopt_nonoption_flags;
258static int nonoption_flags_max_len;
259static int nonoption_flags_len;
261static int original_argc;
262static char *
const *original_argv;
269store_args_and_env (
int argc,
char *
const *argv)
273 original_argc = argc;
274 original_argv = argv;
276# ifdef text_set_element
277text_set_element (__libc_subinit, store_args_and_env);
280# define SWAP_FLAGS(ch1, ch2) \
281 if (nonoption_flags_len > 0) \
283 char __tmp = __getopt_nonoption_flags[ch1]; \
284 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
285 __getopt_nonoption_flags[ch2] = __tmp; \
288# define SWAP_FLAGS(ch1, ch2)
300#if defined (__STDC__) && __STDC__
301static void exchange (
char **);
308 int bottom = first_nonopt;
309 int middle = last_nonopt;
322 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
326 char *new_str = malloc (top + 1);
328 nonoption_flags_len = nonoption_flags_max_len = 0;
331 memset (__mempcpy (new_str, __getopt_nonoption_flags,
332 nonoption_flags_max_len),
333 '\0', top + 1 - nonoption_flags_max_len);
334 nonoption_flags_max_len = top + 1;
335 __getopt_nonoption_flags = new_str;
340 while (top > middle && middle > bottom)
342 if (top - middle > middle - bottom)
345 int len = middle - bottom;
349 for (i = 0; i < len; i++)
351 tem = argv[bottom + i];
352 argv[bottom + i] = argv[top - (middle - bottom) + i];
353 argv[top - (middle - bottom) + i] = tem;
354 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
362 int len = top - middle;
366 for (i = 0; i < len; i++)
368 tem = argv[bottom + i];
369 argv[bottom + i] = argv[middle + i];
370 argv[middle + i] = tem;
380 first_nonopt += (
optind - last_nonopt);
386#if defined (__STDC__) && __STDC__
387static const char *_getopt_initialize (
int,
char *
const *,
const char *);
390_getopt_initialize (argc, argv, optstring)
393 const char *optstring;
399 first_nonopt = last_nonopt =
optind;
403 posixly_correct =
getenv (
"POSIXLY_CORRECT");
407 if (optstring[0] ==
'-')
412 else if (optstring[0] ==
'+')
417 else if (posixly_correct !=
NULL)
423 if (posixly_correct ==
NULL
424 && argc == original_argc && argv == original_argv)
426 if (nonoption_flags_max_len == 0)
428 if (__getopt_nonoption_flags ==
NULL
429 || __getopt_nonoption_flags[0] ==
'\0')
430 nonoption_flags_max_len = -1;
433 const char *orig_str = __getopt_nonoption_flags;
434 int len = nonoption_flags_max_len = strlen (orig_str);
435 if (nonoption_flags_max_len < argc)
436 nonoption_flags_max_len = argc;
437 __getopt_nonoption_flags =
438 (
char *) malloc (nonoption_flags_max_len);
439 if (__getopt_nonoption_flags ==
NULL)
440 nonoption_flags_max_len = -1;
442 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
443 '\0', nonoption_flags_max_len - len);
446 nonoption_flags_len = nonoption_flags_max_len;
449 nonoption_flags_len = 0;
515 const char *optstring;
516 const struct option *longopts;
526 optstring = _getopt_initialize (argc, argv, optstring);
535#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
536 || (optind < nonoption_flags_len \
537 && __getopt_nonoption_flags[optind] == '1'))
539#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
542 if (nextchar ==
NULL || *nextchar ==
'\0')
550 if (first_nonopt >
optind)
558 if (first_nonopt != last_nonopt && last_nonopt !=
optind)
559 exchange ((
char **) argv);
560 else if (last_nonopt !=
optind)
580 if (first_nonopt != last_nonopt && last_nonopt !=
optind)
581 exchange ((
char **) argv);
582 else if (first_nonopt == last_nonopt)
596 if (first_nonopt != last_nonopt)
615 nextchar = (argv[
optind] + 1
616 + (longopts !=
NULL && argv[
optind][1] ==
'-'));
635 && (argv[
optind][1] ==
'-'
636 || (long_only && (argv[
optind][2] || !my_index (optstring, argv[
optind][1])))))
646 for (nameend = nextchar; *nameend && *nameend !=
'='; nameend++)
651 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
652 if (!strncmp (p->
name, nextchar, nameend - nextchar))
654 if ((
unsigned int) (nameend - nextchar)
655 == (
unsigned int) strlen (p->
name))
659 indfound = option_index;
663 else if (pfound ==
NULL)
667 indfound = option_index;
677 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
679 nextchar += strlen (nextchar);
687 option_index = indfound;
700 if (argv[
optind - 1][1] ==
'-')
703 _(
"%s: option `--%s' doesn't allow an argument\n"),
704 argv[0], pfound->
name);
708 _(
"%s: option `%c%s' doesn't allow an argument\n"),
711 nextchar += strlen (nextchar);
725 _(
"%s: option `%s' requires an argument\n"),
726 argv[0], argv[
optind - 1]);
727 nextchar += strlen (nextchar);
729 return optstring[0] ==
':' ?
':' :
'?';
732 nextchar += strlen (nextchar);
734 *longind = option_index;
747 if (!long_only || argv[
optind][1] ==
'-'
748 || my_index (optstring, *nextchar) ==
NULL)
752 if (argv[
optind][1] ==
'-')
754 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
758 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
759 argv[0], argv[
optind][0], nextchar);
761 nextchar = (
char *)
"";
771 char c = *nextchar++;
772 char *temp = my_index (optstring, c);
775 if (*nextchar ==
'\0')
778 if (temp ==
NULL || c ==
':')
784 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
787 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
794 if (temp[0] ==
'W' && temp[1] ==
';')
805 if (*nextchar !=
'\0')
817 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
821 if (optstring[0] ==
':')
835 for (nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
840 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
841 if (!strncmp (p->
name, nextchar, nameend - nextchar))
843 if ((
unsigned int) (nameend - nextchar) == strlen (p->
name))
847 indfound = option_index;
851 else if (pfound ==
NULL)
855 indfound = option_index;
864 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
866 nextchar += strlen (nextchar);
872 option_index = indfound;
882 fprintf (stderr,
_(
"\
883%s: option `-W %s' doesn't allow an argument\n"),
884 argv[0], pfound->
name);
886 nextchar += strlen (nextchar);
898 _(
"%s: option `%s' requires an argument\n"),
899 argv[0], argv[
optind - 1]);
900 nextchar += strlen (nextchar);
901 return optstring[0] ==
':' ?
':' :
'?';
904 nextchar += strlen (nextchar);
906 *longind = option_index;
922 if (*nextchar !=
'\0')
934 if (*nextchar !=
'\0')
947 _(
"%s: option requires an argument -- %c\n"),
951 if (optstring[0] ==
':')
971 const char *optstring;
974 (
const struct option *) 0,
983 const char *optstring;
984 const struct option *longopts;
998 const char *optstring;
999 const struct option *longopts;
1021 int digit_optind = 0;
1027 c =
getopt (argc, argv,
"abc:d:0123456789");
1043 if (digit_optind != 0 && digit_optind != this_option_optind)
1044 printf (
"digits occur in two different argv-elements.\n");
1045 digit_optind = this_option_optind;
1046 printf (
"option %c\n", c);
1050 printf (
"option a\n");
1054 printf (
"option b\n");
1058 printf (
"option c with value `%s'\n",
optarg);
1065 printf (
"?? getopt returned character code 0%o ??\n", c);
1071 printf (
"non-option ARGV-elements: ");
1073 printf (
"%s ", argv[
optind++]);
#define SWAP_FLAGS(ch1, ch2)
int main(int argc, char *argv[])