35 #if defined(WIN32) || defined(_WIN32)
36 #define HAVE_STRING_H 1
39 #if !defined __STDC__ || !__STDC__
57 #define GETOPT_INTERFACE_VERSION 2
58 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
59 # include <gnu-versions.h>
60 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
70 #ifdef __GNU_LIBRARY__
79 # if HAVE_STRING_H - 0
86 # if defined HAVE_LIBINTL_H || defined _LIBC
89 # define _(msgid) gettext (msgid)
92 # define _(msgid) (msgid)
198 #ifdef __GNU_LIBRARY__
204 # define my_index strchr
210 # include <strings.h>
239 # if (!defined __STDC__ || !__STDC__) && !defined strlen
242 extern int strlen (
const char *);
261 extern int __libc_argc;
262 extern char **__libc_argv;
267 # ifdef USE_NONOPTION_FLAGS
269 extern char *__getopt_nonoption_flags;
271 static int nonoption_flags_max_len;
272 static int nonoption_flags_len;
275 # ifdef USE_NONOPTION_FLAGS
276 # define SWAP_FLAGS(ch1, ch2) \
277 if (nonoption_flags_len > 0) \
279 char __tmp = __getopt_nonoption_flags[ch1]; \
280 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
281 __getopt_nonoption_flags[ch2] = __tmp; \
284 # define SWAP_FLAGS(ch1, ch2)
287 # define SWAP_FLAGS(ch1, ch2)
299 #if defined __STDC__ && __STDC__
317 #if defined _LIBC && defined USE_NONOPTION_FLAGS
321 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
325 char *new_str = malloc (top + 1);
327 nonoption_flags_len = nonoption_flags_max_len = 0;
330 memset (__mempcpy (new_str, __getopt_nonoption_flags,
331 nonoption_flags_max_len),
332 '\0', top + 1 - nonoption_flags_max_len);
333 nonoption_flags_max_len = top + 1;
334 __getopt_nonoption_flags = new_str;
339 while (top > middle && middle > bottom)
341 if (top - middle > middle - bottom)
344 int len = middle - bottom;
348 for (i = 0; i < len; i++)
350 tem = argv[bottom + i];
351 argv[bottom + i] = argv[top - (middle - bottom) + i];
352 argv[top - (middle - bottom) + i] = tem;
353 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
361 int len = top - middle;
365 for (i = 0; i < len; i++)
367 tem = argv[bottom + i];
368 argv[bottom + i] = argv[middle + i];
369 argv[middle + i] = tem;
385 #if defined __STDC__ && __STDC__
392 const char *optstring;
406 if (optstring[0] ==
'-')
411 else if (optstring[0] ==
'+')
421 #if defined _LIBC && defined USE_NONOPTION_FLAGS
423 && argc == __libc_argc && argv == __libc_argv)
425 if (nonoption_flags_max_len == 0)
427 if (__getopt_nonoption_flags ==
NULL
428 || __getopt_nonoption_flags[0] ==
'\0')
429 nonoption_flags_max_len = -1;
432 const char *orig_str = __getopt_nonoption_flags;
433 int len = nonoption_flags_max_len = strlen (orig_str);
434 if (nonoption_flags_max_len < argc)
435 nonoption_flags_max_len = argc;
436 __getopt_nonoption_flags =
437 (
char *) malloc (nonoption_flags_max_len);
438 if (__getopt_nonoption_flags ==
NULL)
439 nonoption_flags_max_len = -1;
441 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
442 '\0', nonoption_flags_max_len - len);
445 nonoption_flags_len = nonoption_flags_max_len;
448 nonoption_flags_len = 0;
514 const char *optstring;
515 const struct option *longopts;
519 int print_errors =
opterr;
520 if (optstring[0] ==
':')
540 #if defined _LIBC && defined USE_NONOPTION_FLAGS
541 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
542 || (optind < nonoption_flags_len \
543 && __getopt_nonoption_flags[optind] == '1'))
545 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
622 + (longopts !=
NULL && argv[
optind][1] ==
'-'));
641 && (argv[
optind][1] ==
'-'
652 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
657 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
660 if ((
unsigned int) (nameend -
nextchar)
661 == (
unsigned int) strlen (p->
name))
665 indfound = option_index;
669 else if (pfound ==
NULL)
673 indfound = option_index;
686 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
696 option_index = indfound;
708 if (argv[
optind - 1][1] ==
'-')
711 _(
"%s: option `--%s' doesn't allow an argument\n"),
712 argv[0], pfound->
name);
716 _(
"%s: option `%c%s' doesn't allow an argument\n"),
734 _(
"%s: option `%s' requires an argument\n"),
735 argv[0], argv[
optind - 1]);
738 return optstring[0] ==
':' ?
':' :
'?';
743 *longind = option_index;
756 if (!long_only || argv[
optind][1] ==
'-'
761 if (argv[
optind][1] ==
'-')
763 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
767 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
793 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
796 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
803 if (
temp[0] ==
'W' &&
temp[1] ==
';')
826 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
830 if (optstring[0] ==
':')
844 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
849 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
852 if ((
unsigned int) (nameend -
nextchar) == strlen (p->
name))
856 indfound = option_index;
860 else if (pfound ==
NULL)
864 indfound = option_index;
873 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
881 option_index = indfound;
891 fprintf (stderr,
_(
"\
892 %s: option `-W %s' doesn't allow an argument\n"),
893 argv[0], pfound->
name);
907 _(
"%s: option `%s' requires an argument\n"),
908 argv[0], argv[
optind - 1]);
910 return optstring[0] ==
':' ?
':' :
'?';
915 *longind = option_index;
956 _(
"%s: option requires an argument -- %c\n"),
960 if (optstring[0] ==
':')
980 const char *optstring;
983 (
const struct option *) 0,
1001 int digit_optind = 0;
1007 c =
getopt (argc, argv,
"abc:d:0123456789");
1023 if (digit_optind != 0 && digit_optind != this_option_optind)
1024 printf (
"digits occur in two different argv-elements.\n");
1025 digit_optind = this_option_optind;
1026 printf (
"option %c\n", c);
1030 printf (
"option a\n");
1034 printf (
"option b\n");
1038 printf (
"option c with value `%s'\n",
optarg);
1045 printf (
"?? getopt returned character code 0%o ??\n", c);
1051 printf (
"non-option ARGV-elements: ");
1053 printf (
"%s ", argv[
optind++]);