49 #define GETOPT_INTERFACE_VERSION 2
50 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
51 #include <gnu-versions.h>
52 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
62 #ifdef __GNU_LIBRARY__
76 #if defined (WIN32_NATIVE) && !defined (CYGWIN)
80 #define getpid() GetCurrentProcessId()
88 # define _(msgid) gettext (msgid)
90 # define _(msgid) (msgid)
196 #ifdef __GNU_LIBRARY__
202 #define my_index strchr
222 #if !defined (__STDC__) || !__STDC__
225 extern int strlen (
const char *);
245 extern char *__getopt_nonoption_flags;
247 static int nonoption_flags_max_len;
248 static int nonoption_flags_len;
250 static int original_argc;
251 static char *
const *original_argv;
253 extern pid_t __libc_pid;
259 __attribute__ ((unused))
260 store_args_and_env (
int argc,
char *
const *argv)
264 original_argc = argc;
265 original_argv = argv;
267 text_set_element (__libc_subinit, store_args_and_env);
269 # define SWAP_FLAGS(ch1, ch2) \
270 if (nonoption_flags_len > 0) \
272 char __tmp = __getopt_nonoption_flags[ch1]; \
273 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
274 __getopt_nonoption_flags[ch2] = __tmp; \
277 # define SWAP_FLAGS(ch1, ch2)
289 #if defined (__STDC__) && __STDC__
310 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
314 char *new_str = malloc (top + 1);
316 nonoption_flags_len = nonoption_flags_max_len = 0;
319 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
320 memset (&new_str[nonoption_flags_max_len],
'\0',
321 top + 1 - nonoption_flags_max_len);
322 nonoption_flags_max_len = top + 1;
323 __getopt_nonoption_flags = new_str;
328 while (top > middle && middle > bottom)
330 if (top - middle > middle - bottom)
333 int len = middle - bottom;
337 for (i = 0; i < len; i++)
339 tem = argv[bottom + i];
340 argv[bottom + i] = argv[top - (middle - bottom) + i];
341 argv[top - (middle - bottom) + i] = tem;
342 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
350 int len = top - middle;
354 for (i = 0; i < len; i++)
356 tem = argv[bottom + i];
357 argv[bottom + i] = argv[middle + i];
358 argv[middle + i] = tem;
374 #if defined (__STDC__) && __STDC__
392 if (optstring[0] ==
'-')
397 else if (optstring[0] ==
'+')
409 && argc == original_argc && argv == original_argv)
411 if (nonoption_flags_max_len == 0)
413 if (__getopt_nonoption_flags ==
NULL
414 || __getopt_nonoption_flags[0] ==
'\0')
415 nonoption_flags_max_len = -1;
418 const char *orig_str = __getopt_nonoption_flags;
419 int len = nonoption_flags_max_len = strlen (orig_str);
420 if (nonoption_flags_max_len < argc)
421 nonoption_flags_max_len = argc;
422 __getopt_nonoption_flags =
423 (
char *) malloc (nonoption_flags_max_len);
424 if (__getopt_nonoption_flags ==
NULL)
425 nonoption_flags_max_len = -1;
428 memcpy (__getopt_nonoption_flags, orig_str, len);
429 memset (&__getopt_nonoption_flags[len],
'\0',
430 nonoption_flags_max_len - len);
434 nonoption_flags_len = nonoption_flags_max_len;
437 nonoption_flags_len = 0;
501 const struct option *longopts,
int *longind,
int long_only)
518 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
519 || (optind < nonoption_flags_len \
520 && __getopt_nonoption_flags[optind] == '1'))
522 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
599 + (longopts !=
NULL && argv[
optind][1] ==
'-'));
618 && (argv[
optind][1] ==
'-'
629 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
634 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
637 if ((
unsigned int) (nameend -
nextchar)
638 == (
unsigned int) strlen (p->
name))
642 indfound = option_index;
646 else if (pfound ==
NULL)
650 indfound = option_index;
660 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
670 option_index = indfound;
681 if (argv[
optind - 1][1] ==
'-')
684 _(
"%s: option `--%s' doesn't allow an argument\n"),
685 argv[0], pfound->
name);
689 _(
"%s: option `%c%s' doesn't allow an argument\n"),
706 _(
"%s: option `%s' requires an argument\n"),
707 argv[0], argv[
optind - 1]);
710 return optstring[0] ==
':' ?
':' :
'?';
715 *longind = option_index;
728 if (!long_only || argv[
optind][1] ==
'-'
733 if (argv[
optind][1] ==
'-')
735 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
739 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
753 char *temp =
my_index (optstring, c);
759 if (temp ==
NULL || c ==
':')
765 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
768 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
775 if (temp[0] ==
'W' && temp[1] ==
';')
798 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
802 if (optstring[0] ==
':')
816 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
821 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
824 if ((
unsigned int) (nameend -
nextchar) == strlen (p->
name))
828 indfound = option_index;
832 else if (pfound ==
NULL)
836 indfound = option_index;
845 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
853 option_index = indfound;
863 fprintf (stderr,
_(
"\
864 %s: option `-W %s' doesn't allow an argument\n"),
865 argv[0], pfound->
name);
879 _(
"%s: option `%s' requires an argument\n"),
880 argv[0], argv[
optind - 1]);
882 return optstring[0] ==
':' ?
':' :
'?';
887 *longind = option_index;
928 _(
"%s: option requires an argument -- %c\n"),
932 if (optstring[0] ==
':')
949 getopt (
int argc,
char *
const *argv,
const char *optstring)
952 (
const struct option *) 0,
965 main (
int argc,
char **argv)
968 int digit_optind = 0;
974 c =
getopt (argc, argv,
"abc:d:0123456789");
990 if (digit_optind != 0 && digit_optind != this_option_optind)
991 printf (
"digits occur in two different argv-elements.\n");
992 digit_optind = this_option_optind;
993 printf (
"option %c\n", c);
997 printf (
"option a\n");
1001 printf (
"option b\n");
1005 printf (
"option c with value `%s'\n",
optarg);
1012 printf (
"?? getopt returned character code 0%o ??\n", c);
1018 printf (
"non-option ARGV-elements: ");
1020 printf (
"%s ", argv[
optind++]);