49 #define _(msgid) (msgid)
51 #if defined _LIBC && defined USE_IN_LIBIO
55 #ifndef attribute_hidden
56 # define attribute_hidden
114 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
122 extern int __libc_argc;
123 extern char **__libc_argv;
128 # ifdef USE_NONOPTION_FLAGS
130 extern char *__getopt_nonoption_flags;
133 # ifdef USE_NONOPTION_FLAGS
134 # define SWAP_FLAGS(ch1, ch2) \
135 if (d->__nonoption_flags_len > 0) \
137 char __tmp = __getopt_nonoption_flags[ch1]; \
138 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
139 __getopt_nonoption_flags[ch2] = __tmp; \
142 # define SWAP_FLAGS(ch1, ch2)
145 # define SWAP_FLAGS(ch1, ch2)
160 int bottom =
d->__first_nonopt;
161 int middle =
d->__last_nonopt;
170 #if defined _LIBC && defined USE_NONOPTION_FLAGS
174 if (
d->__nonoption_flags_len > 0 && top >=
d->__nonoption_flags_max_len)
178 char *new_str =
malloc (top + 1);
180 d->__nonoption_flags_len =
d->__nonoption_flags_max_len = 0;
183 memset (__mempcpy (new_str, __getopt_nonoption_flags,
184 d->__nonoption_flags_max_len),
185 '\0', top + 1 -
d->__nonoption_flags_max_len);
186 d->__nonoption_flags_max_len = top + 1;
187 __getopt_nonoption_flags = new_str;
192 while (top > middle && middle > bottom)
194 if (top - middle > middle - bottom)
197 int len = middle - bottom;
203 tem =
argv[bottom +
i];
204 argv[bottom +
i] =
argv[top - (middle - bottom) +
i];
205 argv[top - (middle - bottom) +
i] = tem;
214 int len = top - middle;
220 tem =
argv[bottom +
i];
222 argv[middle +
i] = tem;
232 d->__first_nonopt += (
d->optind -
d->__last_nonopt);
233 d->__last_nonopt =
d->optind;
246 d->__first_nonopt =
d->__last_nonopt =
d->optind;
248 d->__nextchar =
NULL;
254 if (optstring[0] ==
'-')
259 else if (optstring[0] ==
'+')
264 else if (
d->__posixly_correct)
269 #if defined _LIBC && defined USE_NONOPTION_FLAGS
270 if (!
d->__posixly_correct
271 && argc == __libc_argc &&
argv == __libc_argv)
273 if (
d->__nonoption_flags_max_len == 0)
275 if (__getopt_nonoption_flags ==
NULL
276 || __getopt_nonoption_flags[0] ==
'\0')
277 d->__nonoption_flags_max_len = -1;
280 const char *orig_str = __getopt_nonoption_flags;
281 int len =
d->__nonoption_flags_max_len = strlen (orig_str);
282 if (
d->__nonoption_flags_max_len < argc)
283 d->__nonoption_flags_max_len = argc;
284 __getopt_nonoption_flags =
285 (
char *)
malloc (
d->__nonoption_flags_max_len);
286 if (__getopt_nonoption_flags ==
NULL)
287 d->__nonoption_flags_max_len = -1;
289 memset (__mempcpy (__getopt_nonoption_flags, orig_str,
len),
290 '\0',
d->__nonoption_flags_max_len -
len);
293 d->__nonoption_flags_len =
d->__nonoption_flags_max_len;
296 d->__nonoption_flags_len = 0;
359 const struct option *longopts,
int *longind,
362 int print_errors =
d->opterr;
363 if (optstring[0] ==
':')
371 if (
d->optind == 0 || !
d->__initialized)
377 d->__initialized = 1;
384 #if defined _LIBC && defined USE_NONOPTION_FLAGS
385 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
386 || (d->optind < d->__nonoption_flags_len \
387 && __getopt_nonoption_flags[d->optind] == '1'))
389 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
392 if (
d->__nextchar ==
NULL || *
d->__nextchar ==
'\0')
398 if (
d->__last_nonopt >
d->optind)
399 d->__last_nonopt =
d->optind;
400 if (
d->__first_nonopt >
d->optind)
401 d->__first_nonopt =
d->optind;
408 if (
d->__first_nonopt !=
d->__last_nonopt
409 &&
d->__last_nonopt !=
d->optind)
411 else if (
d->__last_nonopt !=
d->optind)
412 d->__first_nonopt =
d->optind;
419 d->__last_nonopt =
d->optind;
427 if (
d->optind != argc && !strcmp (
argv[
d->optind],
"--"))
431 if (
d->__first_nonopt !=
d->__last_nonopt
432 &&
d->__last_nonopt !=
d->optind)
434 else if (
d->__first_nonopt ==
d->__last_nonopt)
435 d->__first_nonopt =
d->optind;
436 d->__last_nonopt = argc;
444 if (
d->optind == argc)
448 if (
d->__first_nonopt !=
d->__last_nonopt)
449 d->optind =
d->__first_nonopt;
460 d->optarg =
argv[
d->optind++];
467 d->__nextchar = (
argv[
d->optind] + 1
468 + (longopts !=
NULL &&
argv[
d->optind][1] ==
'-'));
487 && (
argv[
d->optind][1] ==
'-'
488 || (long_only && (
argv[
d->optind][2]
489 || !strchr (optstring,
argv[
d->optind][1])))))
499 for (nameend =
d->__nextchar; *nameend && *nameend !=
'='; nameend++)
504 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
505 if (!strncmp (
p->name,
d->__nextchar, nameend -
d->__nextchar))
507 if ((
unsigned int) (nameend -
d->__nextchar)
508 == (
unsigned int) strlen (
p->name))
512 indfound = option_index;
516 else if (pfound ==
NULL)
520 indfound = option_index;
524 || pfound->
flag !=
p->flag
525 || pfound->
val !=
p->val)
534 #if defined _LIBC && defined USE_IN_LIBIO
537 if (__asprintf (&
buf,
_(
"%s: option `%s' is ambiguous\n"),
540 _IO_flockfile (stderr);
542 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
543 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
547 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
548 _IO_funlockfile (stderr);
553 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
557 d->__nextchar += strlen (
d->__nextchar);
565 option_index = indfound;
572 d->optarg = nameend + 1;
577 #if defined _LIBC && defined USE_IN_LIBIO
582 if (
argv[
d->optind - 1][1] ==
'-')
585 #if defined _LIBC && defined USE_IN_LIBIO
586 n = __asprintf (&
buf,
_(
"\
587 %s: option `--%s' doesn't allow an argument\n"),
590 fprintf (stderr,
_(
"\
591 %s: option `--%s' doesn't allow an argument\n"),
598 #if defined _LIBC && defined USE_IN_LIBIO
599 n = __asprintf (&
buf,
_(
"\
600 %s: option `%c%s' doesn't allow an argument\n"),
604 fprintf (stderr,
_(
"\
605 %s: option `%c%s' doesn't allow an argument\n"),
611 #if defined _LIBC && defined USE_IN_LIBIO
614 _IO_flockfile (stderr);
616 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
617 ((_IO_FILE *) stderr)->_flags2
618 |= _IO_FLAGS2_NOTCANCEL;
622 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
623 _IO_funlockfile (stderr);
630 d->__nextchar += strlen (
d->__nextchar);
632 d->optopt = pfound->
val;
638 if (
d->optind < argc)
639 d->optarg =
argv[
d->optind++];
644 #if defined _LIBC && defined USE_IN_LIBIO
647 if (__asprintf (&
buf,
_(
"\
648 %s: option `%s' requires an argument\n"),
651 _IO_flockfile (stderr);
653 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
654 ((_IO_FILE *) stderr)->_flags2
655 |= _IO_FLAGS2_NOTCANCEL;
659 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
660 _IO_funlockfile (stderr);
666 _(
"%s: option `%s' requires an argument\n"),
670 d->__nextchar += strlen (
d->__nextchar);
671 d->optopt = pfound->
val;
672 return optstring[0] ==
':' ?
':' :
'?';
675 d->__nextchar += strlen (
d->__nextchar);
677 *longind = option_index;
690 if (!long_only ||
argv[
d->optind][1] ==
'-'
691 || strchr (optstring, *
d->__nextchar) ==
NULL)
695 #if defined _LIBC && defined USE_IN_LIBIO
700 if (
argv[
d->optind][1] ==
'-')
703 #if defined _LIBC && defined USE_IN_LIBIO
704 n = __asprintf (&
buf,
_(
"%s: unrecognized option `--%s'\n"),
705 argv[0],
d->__nextchar);
707 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
708 argv[0],
d->__nextchar);
714 #if defined _LIBC && defined USE_IN_LIBIO
715 n = __asprintf (&
buf,
_(
"%s: unrecognized option `%c%s'\n"),
718 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
723 #if defined _LIBC && defined USE_IN_LIBIO
726 _IO_flockfile (stderr);
728 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
729 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
733 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
734 _IO_funlockfile (stderr);
740 d->__nextchar = (
char *)
"";
750 char c = *
d->__nextchar++;
751 char *temp = strchr (optstring,
c);
754 if (*
d->__nextchar ==
'\0')
757 if (temp ==
NULL ||
c ==
':')
761 #if defined _LIBC && defined USE_IN_LIBIO
766 if (
d->__posixly_correct)
769 #if defined _LIBC && defined USE_IN_LIBIO
770 n = __asprintf (&
buf,
_(
"%s: illegal option -- %c\n"),
773 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
argv[0],
c);
778 #if defined _LIBC && defined USE_IN_LIBIO
779 n = __asprintf (&
buf,
_(
"%s: invalid option -- %c\n"),
782 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
argv[0],
c);
786 #if defined _LIBC && defined USE_IN_LIBIO
789 _IO_flockfile (stderr);
791 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
792 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
796 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
797 _IO_funlockfile (stderr);
807 if (temp[0] ==
'W' && temp[1] ==
';')
818 if (*
d->__nextchar !=
'\0')
820 d->optarg =
d->__nextchar;
825 else if (
d->optind == argc)
830 #if defined _LIBC && defined USE_IN_LIBIO
833 if (__asprintf (&
buf,
834 _(
"%s: option requires an argument -- %c\n"),
837 _IO_flockfile (stderr);
839 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
840 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
844 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
845 _IO_funlockfile (stderr);
850 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
855 if (optstring[0] ==
':')
864 d->optarg =
argv[
d->optind++];
869 for (
d->__nextchar = nameend =
d->optarg; *nameend && *nameend !=
'=';
875 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
876 if (!strncmp (
p->name,
d->__nextchar, nameend -
d->__nextchar))
878 if ((
unsigned int) (nameend -
d->__nextchar) == strlen (
p->name))
882 indfound = option_index;
886 else if (pfound ==
NULL)
890 indfound = option_index;
900 #if defined _LIBC && defined USE_IN_LIBIO
903 if (__asprintf (&
buf,
_(
"%s: option `-W %s' is ambiguous\n"),
906 _IO_flockfile (stderr);
908 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
909 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
913 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
914 _IO_funlockfile (stderr);
919 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
923 d->__nextchar += strlen (
d->__nextchar);
929 option_index = indfound;
935 d->optarg = nameend + 1;
940 #if defined _LIBC && defined USE_IN_LIBIO
943 if (__asprintf (&
buf,
_(
"\
944 %s: option `-W %s' doesn't allow an argument\n"),
947 _IO_flockfile (stderr);
949 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
950 ((_IO_FILE *) stderr)->_flags2
951 |= _IO_FLAGS2_NOTCANCEL;
955 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
956 _IO_funlockfile (stderr);
961 fprintf (stderr,
_(
"\
962 %s: option `-W %s' doesn't allow an argument\n"),
967 d->__nextchar += strlen (
d->__nextchar);
973 if (
d->optind < argc)
974 d->optarg =
argv[
d->optind++];
979 #if defined _LIBC && defined USE_IN_LIBIO
982 if (__asprintf (&
buf,
_(
"\
983 %s: option `%s' requires an argument\n"),
986 _IO_flockfile (stderr);
988 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
989 ((_IO_FILE *) stderr)->_flags2
990 |= _IO_FLAGS2_NOTCANCEL;
994 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
995 _IO_funlockfile (stderr);
1001 _(
"%s: option `%s' requires an argument\n"),
1005 d->__nextchar += strlen (
d->__nextchar);
1006 return optstring[0] ==
':' ?
':' :
'?';
1009 d->__nextchar += strlen (
d->__nextchar);
1010 if (longind !=
NULL)
1011 *longind = option_index;
1014 *(pfound->
flag) = pfound->
val;
1019 d->__nextchar =
NULL;
1027 if (*
d->__nextchar !=
'\0')
1029 d->optarg =
d->__nextchar;
1034 d->__nextchar =
NULL;
1039 if (*
d->__nextchar !=
'\0')
1041 d->optarg =
d->__nextchar;
1046 else if (
d->optind == argc)
1051 #if defined _LIBC && defined USE_IN_LIBIO
1054 if (__asprintf (&
buf,
_(
"\
1055 %s: option requires an argument -- %c\n"),
1058 _IO_flockfile (stderr);
1060 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1061 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1065 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1066 _IO_funlockfile (stderr);
1072 _(
"%s: option requires an argument -- %c\n"),
1077 if (optstring[0] ==
':')
1085 d->optarg =
argv[
d->optind++];
1086 d->__nextchar =
NULL;
1095 const struct option *longopts,
int *longind,
1138 int digit_optind = 0;
1160 if (digit_optind != 0 && digit_optind != this_option_optind)
1161 printf (
"digits occur in two different argv-elements.\n");
1162 digit_optind = this_option_optind;
1182 printf (
"?? getopt returned character code 0%o ??\n",
c);
1188 printf (
"non-option ARGV-elements: ");
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
RZ_API void Ht_() free(HtName_(Ht) *ht)
return memset(p, 0, total)
void * malloc(size_t size)
static static fork const void static count static fd const char const char static newpath char char argv
int main(int argc, char **argv)
#define SWAP_FLAGS(ch1, ch2)
static const char * _getopt_initialize(int argc, char **argv, const char *optstring, int posixly_correct, struct _getopt_data *d)
int _getopt_internal_r(int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct, struct _getopt_data *d)
static struct _getopt_data getopt_data
static void exchange(char **argv, struct _getopt_data *d)