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
65 #ifdef __GNU_LIBRARY__
74 # if HAVE_STRING_H - 0
81 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
84 # define _(msgid) gettext (msgid)
87 # define _(msgid) (msgid)
89 # if defined _LIBC && defined USE_IN_LIBIO
196 #ifdef __GNU_LIBRARY__
202 # define my_index strchr
205 # if HAVE_STRING_H || WIN32
208 # include <strings.h>
237 # if (!defined __STDC__ || !__STDC__) && !defined strlen
240 extern int strlen (
const char *);
259 extern int __libc_argc;
260 extern char **__libc_argv;
265 # ifdef USE_NONOPTION_FLAGS
267 extern char *__getopt_nonoption_flags;
269 static int nonoption_flags_max_len;
270 static int nonoption_flags_len;
273 # ifdef USE_NONOPTION_FLAGS
274 # define SWAP_FLAGS(ch1, ch2) \
275 if (nonoption_flags_len > 0) \
277 char __tmp = __getopt_nonoption_flags[ch1]; \
278 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
279 __getopt_nonoption_flags[ch2] = __tmp; \
282 # define SWAP_FLAGS(ch1, ch2)
285 # define SWAP_FLAGS(ch1, ch2)
297 #if defined __STDC__ && __STDC__
315 #if defined _LIBC && defined USE_NONOPTION_FLAGS
319 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
323 char *new_str = malloc (top + 1);
325 nonoption_flags_len = nonoption_flags_max_len = 0;
328 memset (__mempcpy (new_str, __getopt_nonoption_flags,
329 nonoption_flags_max_len),
330 '\0', top + 1 - nonoption_flags_max_len);
331 nonoption_flags_max_len = top + 1;
332 __getopt_nonoption_flags = new_str;
337 while (top > middle && middle > bottom)
339 if (top - middle > middle - bottom)
342 int len = middle - bottom;
346 for (i = 0; i < len; i++)
348 tem = argv[bottom + i];
349 argv[bottom + i] = argv[top - (middle - bottom) + i];
350 argv[top - (middle - bottom) + i] = tem;
351 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
359 int len = top - middle;
363 for (i = 0; i < len; i++)
365 tem = argv[bottom + i];
366 argv[bottom + i] = argv[middle + i];
367 argv[middle + i] = tem;
383 #if defined __STDC__ && __STDC__
390 const char *optstring;
404 if (optstring[0] ==
'-')
409 else if (optstring[0] ==
'+')
419 #if defined _LIBC && defined USE_NONOPTION_FLAGS
421 && argc == __libc_argc && argv == __libc_argv)
423 if (nonoption_flags_max_len == 0)
425 if (__getopt_nonoption_flags ==
NULL
426 || __getopt_nonoption_flags[0] ==
'\0')
427 nonoption_flags_max_len = -1;
430 const char *orig_str = __getopt_nonoption_flags;
431 int len = nonoption_flags_max_len = strlen (orig_str);
432 if (nonoption_flags_max_len < argc)
433 nonoption_flags_max_len = argc;
434 __getopt_nonoption_flags =
435 (
char *) malloc (nonoption_flags_max_len);
436 if (__getopt_nonoption_flags ==
NULL)
437 nonoption_flags_max_len = -1;
439 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
440 '\0', nonoption_flags_max_len - len);
443 nonoption_flags_len = nonoption_flags_max_len;
446 nonoption_flags_len = 0;
512 const char *optstring;
513 const struct option *longopts;
517 int print_errors =
opterr;
518 if (optstring[0] ==
':')
538 #if defined _LIBC && defined USE_NONOPTION_FLAGS
539 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
540 || (optind < nonoption_flags_len \
541 && __getopt_nonoption_flags[optind] == '1'))
543 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
620 + (longopts !=
NULL && argv[
optind][1] ==
'-'));
639 && (argv[
optind][1] ==
'-'
650 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
655 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
658 if ((
unsigned int) (nameend -
nextchar)
659 == (
unsigned int) strlen (p->
name))
663 indfound = option_index;
667 else if (pfound ==
NULL)
671 indfound = option_index;
685 #if defined _LIBC && defined USE_IN_LIBIO
688 __asprintf (&buf,
_(
"%s: option `%s' is ambiguous\n"),
691 if (_IO_fwide (
stderr, 0) > 0)
692 __fwprintf (
stderr, L
"%s", buf);
698 fprintf (
stderr,
_(
"%s: option `%s' is ambiguous\n"),
710 option_index = indfound;
722 #if defined _LIBC && defined USE_IN_LIBIO
726 if (argv[
optind - 1][1] ==
'-')
729 #if defined _LIBC && defined USE_IN_LIBIO
730 __asprintf (&buf,
_(
"\
731 %s: option `--%s' doesn't allow an argument\n"),
732 argv[0], pfound->
name);
735 %s: option `--%s' doesn't allow an argument\n"),
736 argv[0], pfound->
name);
742 #if defined _LIBC && defined USE_IN_LIBIO
743 __asprintf (&buf,
_(
"\
744 %s: option `%c%s' doesn't allow an argument\n"),
745 argv[0], argv[
optind - 1][0],
749 %s: option `%c%s' doesn't allow an argument\n"),
754 #if defined _LIBC && defined USE_IN_LIBIO
755 if (_IO_fwide (
stderr, 0) > 0)
756 __fwprintf (
stderr, L
"%s", buf);
778 #if defined _LIBC && defined USE_IN_LIBIO
782 _(
"%s: option `%s' requires an argument\n"),
783 argv[0], argv[
optind - 1]);
785 if (_IO_fwide (
stderr, 0) > 0)
786 __fwprintf (
stderr, L
"%s", buf);
793 _(
"%s: option `%s' requires an argument\n"),
794 argv[0], argv[
optind - 1]);
799 return optstring[0] ==
':' ?
':' :
'?';
804 *longind = option_index;
817 if (!long_only || argv[
optind][1] ==
'-'
822 #if defined _LIBC && defined USE_IN_LIBIO
826 if (argv[
optind][1] ==
'-')
829 #if defined _LIBC && defined USE_IN_LIBIO
830 __asprintf (&buf,
_(
"%s: unrecognized option `--%s'\n"),
833 fprintf (
stderr,
_(
"%s: unrecognized option `--%s'\n"),
840 #if defined _LIBC && defined USE_IN_LIBIO
841 __asprintf (&buf,
_(
"%s: unrecognized option `%c%s'\n"),
844 fprintf (
stderr,
_(
"%s: unrecognized option `%c%s'\n"),
849 #if defined _LIBC && defined USE_IN_LIBIO
850 if (_IO_fwide (
stderr, 0) > 0)
851 __fwprintf (
stderr, L
"%s", buf);
875 if (temp ==
NULL ||
c ==
':')
879 #if defined _LIBC && defined USE_IN_LIBIO
886 #if defined _LIBC && defined USE_IN_LIBIO
887 __asprintf (&buf,
_(
"%s: illegal option -- %c\n"),
890 fprintf (
stderr,
_(
"%s: illegal option -- %c\n"), argv[0],
c);
895 #if defined _LIBC && defined USE_IN_LIBIO
896 __asprintf (&buf,
_(
"%s: invalid option -- %c\n"),
899 fprintf (
stderr,
_(
"%s: invalid option -- %c\n"), argv[0],
c);
903 #if defined _LIBC && defined USE_IN_LIBIO
904 if (_IO_fwide (
stderr, 0) > 0)
905 __fwprintf (
stderr, L
"%s", buf);
916 if (temp[0] ==
'W' && temp[1] ==
';')
939 #if defined _LIBC && defined USE_IN_LIBIO
942 __asprintf (&buf,
_(
"%s: option requires an argument -- %c\n"),
945 if (_IO_fwide (
stderr, 0) > 0)
946 __fwprintf (
stderr, L
"%s", buf);
952 fprintf (
stderr,
_(
"%s: option requires an argument -- %c\n"),
957 if (optstring[0] ==
':')
971 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
976 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
979 if ((
unsigned int) (nameend -
nextchar) == strlen (p->
name))
983 indfound = option_index;
987 else if (pfound ==
NULL)
991 indfound = option_index;
1001 #if defined _LIBC && defined USE_IN_LIBIO
1004 __asprintf (&buf,
_(
"%s: option `-W %s' is ambiguous\n"),
1007 if (_IO_fwide (
stderr, 0) > 0)
1008 __fwprintf (
stderr, L
"%s", buf);
1014 fprintf (
stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
1024 option_index = indfound;
1035 #if defined _LIBC && defined USE_IN_LIBIO
1038 __asprintf (&buf,
_(
"\
1039 %s: option `-W %s' doesn't allow an argument\n"),
1040 argv[0], pfound->
name);
1042 if (_IO_fwide (
stderr, 0) > 0)
1043 __fwprintf (
stderr, L
"%s", buf);
1050 %s: option `-W %s' doesn't allow an argument\n"),
1051 argv[0], pfound->
name);
1059 else if (pfound->
has_arg == 1)
1067 #if defined _LIBC && defined USE_IN_LIBIO
1070 __asprintf (&buf,
_(
"\
1071 %s: option `%s' requires an argument\n"),
1072 argv[0], argv[
optind - 1]);
1074 if (_IO_fwide (
stderr, 0) > 0)
1075 __fwprintf (
stderr, L
"%s", buf);
1082 _(
"%s: option `%s' requires an argument\n"),
1083 argv[0], argv[
optind - 1]);
1087 return optstring[0] ==
':' ?
':' :
'?';
1091 if (longind !=
NULL)
1092 *longind = option_index;
1095 *(pfound->
flag) = pfound->
val;
1132 #if defined _LIBC && defined USE_IN_LIBIO
1136 _(
"%s: option requires an argument -- %c\n"),
1139 if (_IO_fwide (
stderr, 0) > 0)
1140 __fwprintf (
stderr, L
"%s", buf);
1147 _(
"%s: option requires an argument -- %c\n"),
1152 if (optstring[0] ==
':')
1172 const char *optstring;
1175 (
const struct option *) 0,
1199 int digit_optind = 0;
1205 c =
getopt (argc, argv,
"abc:d:0123456789");
1221 if (digit_optind != 0 && digit_optind != this_option_optind)
1222 printf (
"digits occur in two different argv-elements.\n");
1223 digit_optind = this_option_optind;
1224 printf (
"option %c\n",
c);
1228 printf (
"option a\n");
1232 printf (
"option b\n");
1236 printf (
"option c with value `%s'\n",
optarg);
1243 printf (
"?? getopt returned character code 0%o ??\n",
c);
1249 printf (
"non-option ARGV-elements: ");
1251 printf (
"%s ", argv[
optind++]);