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 defined HAVE_LIBINTL_H || defined _LIBC 84 # define _(msgid) gettext (msgid) 87 # define _(msgid) (msgid) 134 int __getopt_initialized;
143 static char *nextchar;
187 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
191 static char *posixly_correct;
193 #ifdef __GNU_LIBRARY__ 199 # define my_index strchr 205 # include <strings.h> 212 extern char *getenv ();
234 # if (!defined __STDC__ || !__STDC__) && !defined strlen 237 extern int strlen (
const char *);
249 static int first_nonopt;
250 static int last_nonopt;
256 extern int __libc_argc;
257 extern char **__libc_argv;
262 # ifdef USE_NONOPTION_FLAGS 264 extern char *__getopt_nonoption_flags;
266 static int nonoption_flags_max_len;
267 static int nonoption_flags_len;
270 # ifdef USE_NONOPTION_FLAGS 271 # define SWAP_FLAGS(ch1, ch2) \ 272 if (nonoption_flags_len > 0) \ 274 char __tmp = __getopt_nonoption_flags[ch1]; \ 275 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 276 __getopt_nonoption_flags[ch2] = __tmp; \ 279 # define SWAP_FLAGS(ch1, ch2) 282 # define SWAP_FLAGS(ch1, ch2) 294 #if defined __STDC__ && __STDC__ 295 static void exchange (
char **);
302 int bottom = first_nonopt;
303 int middle = last_nonopt;
312 #if defined _LIBC && defined USE_NONOPTION_FLAGS 316 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
320 char *new_str = malloc (top + 1);
322 nonoption_flags_len = nonoption_flags_max_len = 0;
325 memset (__mempcpy (new_str, __getopt_nonoption_flags,
326 nonoption_flags_max_len),
327 '\0', top + 1 - nonoption_flags_max_len);
328 nonoption_flags_max_len = top + 1;
329 __getopt_nonoption_flags = new_str;
334 while (top > middle && middle > bottom)
336 if (top - middle > middle - bottom)
339 int len = middle - bottom;
343 for (i = 0; i < len; i++)
345 tem = argv[bottom + i];
346 argv[bottom + i] = argv[top - (middle - bottom) + i];
347 argv[top - (middle - bottom) + i] = tem;
348 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
356 int len = top - middle;
360 for (i = 0; i < len; i++)
362 tem = argv[bottom + i];
363 argv[bottom + i] = argv[middle + i];
364 argv[middle + i] = tem;
365 SWAP_FLAGS (bottom + i, middle + i);
374 first_nonopt += (optind - last_nonopt);
375 last_nonopt = optind;
380 #if defined __STDC__ && __STDC__ 381 static const char *_getopt_initialize (
int,
char *
const *,
const char *);
384 _getopt_initialize (argc, argv, optstring)
387 const char *optstring;
393 first_nonopt = last_nonopt = optind;
397 posixly_correct = getenv (
"POSIXLY_CORRECT");
401 if (optstring[0] ==
'-')
403 ordering = RETURN_IN_ORDER;
406 else if (optstring[0] ==
'+')
408 ordering = REQUIRE_ORDER;
411 else if (posixly_correct != NULL)
412 ordering = REQUIRE_ORDER;
416 #if defined _LIBC && defined USE_NONOPTION_FLAGS 417 if (posixly_correct == NULL
418 && argc == __libc_argc && argv == __libc_argv)
420 if (nonoption_flags_max_len == 0)
422 if (__getopt_nonoption_flags == NULL
423 || __getopt_nonoption_flags[0] ==
'\0')
424 nonoption_flags_max_len = -1;
427 const char *orig_str = __getopt_nonoption_flags;
428 int len = nonoption_flags_max_len = strlen (orig_str);
429 if (nonoption_flags_max_len < argc)
430 nonoption_flags_max_len = argc;
431 __getopt_nonoption_flags =
432 (
char *) malloc (nonoption_flags_max_len);
433 if (__getopt_nonoption_flags == NULL)
434 nonoption_flags_max_len = -1;
436 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
437 '\0', nonoption_flags_max_len - len);
440 nonoption_flags_len = nonoption_flags_max_len;
443 nonoption_flags_len = 0;
506 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
509 const char *optstring;
510 const struct option *longopts;
514 int print_errors = opterr;
515 if (optstring[0] ==
':')
523 if (optind == 0 || !__getopt_initialized)
527 optstring = _getopt_initialize (argc, argv, optstring);
528 __getopt_initialized = 1;
535 #if defined _LIBC && defined USE_NONOPTION_FLAGS 536 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 537 || (optind < nonoption_flags_len \ 538 && __getopt_nonoption_flags[optind] == '1')) 540 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 543 if (nextchar == NULL || *nextchar ==
'\0')
549 if (last_nonopt > optind)
550 last_nonopt = optind;
551 if (first_nonopt > optind)
552 first_nonopt = optind;
554 if (ordering == PERMUTE)
559 if (first_nonopt != last_nonopt && last_nonopt != optind)
560 exchange ((
char **) argv);
561 else if (last_nonopt != optind)
562 first_nonopt = optind;
567 while (optind < argc && NONOPTION_P)
569 last_nonopt = optind;
577 if (optind != argc && !strcmp (argv[optind],
"--"))
581 if (first_nonopt != last_nonopt && last_nonopt != optind)
582 exchange ((
char **) argv);
583 else if (first_nonopt == last_nonopt)
584 first_nonopt = optind;
597 if (first_nonopt != last_nonopt)
598 optind = first_nonopt;
607 if (ordering == REQUIRE_ORDER)
609 optarg = argv[optind++];
616 nextchar = (argv[optind] + 1
617 + (longopts != NULL && argv[optind][1] ==
'-'));
636 && (argv[optind][1] ==
'-' 637 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
641 const struct option *pfound = NULL;
647 for (nameend = nextchar; *nameend && *nameend !=
'='; nameend++)
652 for (p = longopts, option_index = 0; p->name; p++, option_index++)
653 if (!strncmp (p->name, nextchar, nameend - nextchar))
655 if ((
unsigned int) (nameend - nextchar)
656 == (
unsigned int) strlen (p->name))
660 indfound = option_index;
664 else if (pfound == NULL)
668 indfound = option_index;
671 || pfound->has_arg != p->has_arg
672 || pfound->flag != p->flag
673 || pfound->val != p->val)
681 fprintf (stderr, _(
"%s: option `%s' is ambiguous\n"),
682 argv[0], argv[optind]);
683 nextchar += strlen (nextchar);
691 option_index = indfound;
698 optarg = nameend + 1;
703 if (argv[optind - 1][1] ==
'-')
706 _(
"%s: option `--%s' doesn't allow an argument\n"),
707 argv[0], pfound->name);
711 _(
"%s: option `%c%s' doesn't allow an argument\n"),
712 argv[0], argv[optind - 1][0], pfound->name);
715 nextchar += strlen (nextchar);
717 optopt = pfound->val;
721 else if (pfound->has_arg == 1)
724 optarg = argv[optind++];
729 _(
"%s: option `%s' requires an argument\n"),
730 argv[0], argv[optind - 1]);
731 nextchar += strlen (nextchar);
732 optopt = pfound->val;
733 return optstring[0] ==
':' ?
':' :
'?';
736 nextchar += strlen (nextchar);
738 *longind = option_index;
741 *(pfound->flag) = pfound->val;
751 if (!long_only || argv[optind][1] ==
'-' 752 || my_index (optstring, *nextchar) == NULL)
756 if (argv[optind][1] ==
'-')
758 fprintf (stderr, _(
"%s: unrecognized option `--%s'\n"),
762 fprintf (stderr, _(
"%s: unrecognized option `%c%s'\n"),
763 argv[0], argv[optind][0], nextchar);
765 nextchar = (
char *)
"";
775 char c = *nextchar++;
776 char *temp = my_index (optstring, c);
779 if (*nextchar ==
'\0')
782 if (temp == NULL || c ==
':')
788 fprintf (stderr, _(
"%s: illegal option -- %c\n"),
791 fprintf (stderr, _(
"%s: invalid option -- %c\n"),
798 if (temp[0] ==
'W' && temp[1] ==
';')
802 const struct option *pfound = NULL;
809 if (*nextchar !=
'\0')
816 else if (optind == argc)
821 fprintf (stderr, _(
"%s: option requires an argument -- %c\n"),
825 if (optstring[0] ==
':')
834 optarg = argv[optind++];
839 for (nextchar = nameend = optarg; *nameend && *nameend !=
'='; nameend++)
844 for (p = longopts, option_index = 0; p->name; p++, option_index++)
845 if (!strncmp (p->name, nextchar, nameend - nextchar))
847 if ((
unsigned int) (nameend - nextchar) == strlen (p->name))
851 indfound = option_index;
855 else if (pfound == NULL)
859 indfound = option_index;
868 fprintf (stderr, _(
"%s: option `-W %s' is ambiguous\n"),
869 argv[0], argv[optind]);
870 nextchar += strlen (nextchar);
876 option_index = indfound;
882 optarg = nameend + 1;
886 fprintf (stderr, _(
"\ 887 %s: option `-W %s' doesn't allow an argument\n"),
888 argv[0], pfound->name);
890 nextchar += strlen (nextchar);
894 else if (pfound->has_arg == 1)
897 optarg = argv[optind++];
902 _(
"%s: option `%s' requires an argument\n"),
903 argv[0], argv[optind - 1]);
904 nextchar += strlen (nextchar);
905 return optstring[0] ==
':' ?
':' :
'?';
908 nextchar += strlen (nextchar);
910 *longind = option_index;
913 *(pfound->flag) = pfound->val;
926 if (*nextchar !=
'\0')
938 if (*nextchar !=
'\0')
945 else if (optind == argc)
951 _(
"%s: option requires an argument -- %c\n"),
955 if (optstring[0] ==
':')
963 optarg = argv[optind++];
972 getopt (argc, argv, optstring)
975 const char *optstring;
977 return _getopt_internal (argc, argv, optstring,
978 (
const struct option *) 0,
996 int digit_optind = 0;
1000 int this_option_optind = optind ? optind : 1;
1002 c = getopt (argc, argv,
"abc:d:0123456789");
1018 if (digit_optind != 0 && digit_optind != this_option_optind)
1019 printf (
"digits occur in two different argv-elements.\n");
1020 digit_optind = this_option_optind;
1021 printf (
"option %c\n", c);
1025 printf (
"option a\n");
1029 printf (
"option b\n");
1033 printf (
"option c with value `%s'\n", optarg);
1040 printf (
"?? getopt returned character code 0%o ??\n", c);
1046 printf (
"non-option ARGV-elements: ");
1047 while (optind < argc)
1048 printf (
"%s ", argv[optind++]);
int main(int argc, char *argv[])