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
82 # ifdef HAVE_LIBINTL_H
84 # define _(msgid) gettext (msgid)
86 # define _(msgid) (msgid)
192 #ifdef __GNU_LIBRARY__
198 # define my_index strchr
203 # include <strings.h>
232 # if (!defined __STDC__ || !__STDC__) && !defined strlen
235 extern int strlen (
const char *);
255 extern char *__getopt_nonoption_flags;
257 static int nonoption_flags_max_len;
258 static int nonoption_flags_len;
260 static int original_argc;
261 static char *
const *original_argv;
268 store_args_and_env (
int argc,
char *
const *
argv)
272 original_argc = argc;
273 original_argv =
argv;
275 # ifdef text_set_element
276 text_set_element (__libc_subinit, store_args_and_env);
279 # define SWAP_FLAGS(ch1, ch2) \
280 if (nonoption_flags_len > 0) \
282 char __tmp = __getopt_nonoption_flags[ch1]; \
283 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
284 __getopt_nonoption_flags[ch2] = __tmp; \
287 # define SWAP_FLAGS(ch1, ch2)
299 #if defined __STDC__ && __STDC__
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 memcpy (new_str, __getopt_nonoption_flags,
331 nonoption_flags_max_len);
332 memset (new_str + nonoption_flags_max_len,
333 '\0', top + 1 - nonoption_flags_max_len);
334 nonoption_flags_max_len = top + 1;
335 __getopt_nonoption_flags = new_str;
340 while (top > middle && middle > bottom)
342 if (top - middle > middle - bottom)
345 int len = middle - bottom;
351 tem =
argv[bottom +
i];
352 argv[bottom +
i] =
argv[top - (middle - bottom) +
i];
353 argv[top - (middle - bottom) +
i] = tem;
362 int len = top - middle;
368 tem =
argv[bottom +
i];
370 argv[middle +
i] = tem;
386 #if defined __STDC__ && __STDC__
393 const char *optstring;
407 if (optstring[0] ==
'-')
412 else if (optstring[0] ==
'+')
424 && argc == original_argc &&
argv == original_argv)
426 if (nonoption_flags_max_len == 0)
428 if (__getopt_nonoption_flags ==
NULL
429 || __getopt_nonoption_flags[0] ==
'\0')
430 nonoption_flags_max_len = -1;
433 const char *orig_str = __getopt_nonoption_flags;
434 int len = nonoption_flags_max_len = strlen (orig_str);
435 if (nonoption_flags_max_len < argc)
436 nonoption_flags_max_len = argc;
437 __getopt_nonoption_flags =
438 (
char *)
malloc (nonoption_flags_max_len);
439 if (__getopt_nonoption_flags ==
NULL)
440 nonoption_flags_max_len = -1;
442 memcpy (__getopt_nonoption_flags, orig_str,
len);
444 '\0', nonoption_flags_max_len -
len);
448 nonoption_flags_len = nonoption_flags_max_len;
451 nonoption_flags_len = 0;
517 const char *optstring;
518 const struct option *longopts;
522 int print_errors =
opterr;
523 if (optstring[0] ==
':')
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')
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;
683 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
693 option_index = indfound;
708 _(
"%s: option `--%s' doesn't allow an argument\n"),
713 _(
"%s: option `%c%s' doesn't allow an argument\n"),
731 _(
"%s: option `%s' requires an argument\n"),
735 return optstring[0] ==
':' ?
':' :
'?';
740 *longind = option_index;
760 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
764 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
784 if (temp ==
NULL ||
c ==
':')
790 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
793 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
800 if (temp[0] ==
'W' && temp[1] ==
';')
823 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
827 if (optstring[0] ==
':')
841 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
846 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
849 if ((
unsigned int) (nameend -
nextchar) == strlen (
p->name))
853 indfound = option_index;
857 else if (pfound ==
NULL)
861 indfound = option_index;
870 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
878 option_index = indfound;
888 fprintf (stderr,
_(
"%s: option `-W %s' doesn't allow an argument\n"),
903 _(
"%s: option `%s' requires an argument\n"),
906 return optstring[0] ==
':' ?
':' :
'?';
911 *longind = option_index;
952 _(
"%s: option requires an argument -- %c\n"),
956 if (optstring[0] ==
':')
976 const char *optstring;
979 (
const struct option *) 0,
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
static static fork const void static count static fd const char const char static newpath char char argv