33 #ifndef HAVE_GETOPT_LONG 41 #define REPLACE_GETOPT 44 #define _DIAGASSERT(e) 49 __weak_alias (
getopt, _getopt)
61 #define IGNORE_FIRST (*options == '-' || *options == '+') 62 #define PRINT_ERROR ((opterr) && ((*options != ':') \ 63 || (IGNORE_FIRST && options[1] != ':'))) 64 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL) 65 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST) 67 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-') 69 #define BADCH (int)'?' 70 #define BADARG (int)':' 71 #define INORDER (int)1 74 static int gcd (
int,
int);
76 static void xwarnx (
const char *, ...);
85 static const char recargchar[] =
"option requires an argument -- %c";
86 static const char recargstring[] =
"option requires an argument -- %s";
87 static const char ambig[] =
"ambiguous option -- %.*s";
88 static const char noarg[] =
"option doesn't take an argument -- %.*s";
96 static void xwarnx (
const char *fmt, ...)
102 (void) fprintf (stderr,
"%s: ", progname);
104 (void) vfprintf (stderr, fmt, ap);
105 (void) fprintf (stderr,
"\n");
134 permute_args (
int nonopt_start,
int nonopt_end,
int opt_end,
char *
const *nargv)
136 int cstart, cyclelen,
i, j, ncycle, nnonopts, nopts, pos;
144 ncycle =
gcd (nnonopts, nopts);
147 for (i = 0; i < ncycle; i++)
149 cstart = nonopt_end +
i;
151 for (j = 0; j < cyclelen; j++)
153 if (pos >= nonopt_end)
159 ((
char **) nargv)[pos] = nargv[cstart];
161 ((
char **) nargv)[cstart] = swap;
191 nonopt_start = nonopt_end = -1;
193 if (optreset || !*place)
199 if (nonopt_end != -1)
205 else if (nonopt_start != -1)
213 nonopt_start = nonopt_end = -1;
216 if (*(place = nargv[optind]) !=
'-')
225 optarg = nargv[optind++];
237 if (nonopt_start == -1)
239 else if (nonopt_end != -1)
249 if (nonopt_start != -1 && nonopt_end == -1)
251 if (place[1] && *++place ==
'-')
257 if ((optchar = (
int) *place++) == (int)
':' || (oli = (
char *) strchr (options + (
IGNORE_FIRST ? 1 : 0), optchar))
264 xwarnx (illoptchar, optchar);
268 if (optchar ==
'W' && oli[1] ==
';')
274 if (++optind >= nargc)
278 xwarnx (recargchar, optchar);
302 else if (oli[1] !=
':')
304 if (++optind >= nargc)
308 xwarnx (recargchar, optchar);
323 #ifdef REPLACE_GETOPT 331 getopt (
int nargc,
char *
const *nargv,
const char *options)
344 if (nonopt_end != -1)
349 nonopt_start = nonopt_end = -1;
361 getopt_long (
int nargc,
char *
const *nargv,
const char *options,
const struct option *long_options,
int *idx)
374 char *current_argv, *has_equal;
375 size_t current_argv_len;
378 current_argv =
place;
384 if (*current_argv ==
'\0')
390 if (nonopt_end != -1)
395 nonopt_start = nonopt_end = -1;
398 if ((has_equal = strchr (current_argv,
'=')) !=
NULL)
401 current_argv_len = has_equal - current_argv;
405 current_argv_len =
strlen (current_argv);
407 for (i = 0; long_options[
i].
name; i++)
410 if (strncmp (current_argv, long_options[i].
name, current_argv_len))
413 if (
strlen (long_options[i].name) == (
unsigned) current_argv_len)
425 xwarnx (ambig, (
int) current_argv_len, current_argv);
435 xwarnx (noarg, (
int) current_argv_len, current_argv);
440 if (long_options[match].
flag ==
NULL)
441 optopt = long_options[match].
val;
457 optarg = nargv[optind++];
467 xwarnx (recargstring, current_argv);
472 if (long_options[match].
flag ==
NULL)
473 optopt = long_options[match].
val;
484 xwarnx (illoptstring, current_argv);
488 if (long_options[match].
flag)
490 *long_options[match].
flag = long_options[match].
val;
494 retval = long_options[match].
val;
static const char recargchar[]
int getopt_long(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
static void xwarnx(const char *,...)
static const char illoptchar[]
#define required_argument
static const char * progname
static const char illoptstring[]
#define optional_argument
static const char recargstring[]
static int getopt_internal(int, char *const *, const char *)
static const char ambig[]
static void permute_args(int, int, int, char *const *)
static const char noarg[]
int getopt(int nargc, char *const *nargv, const char *options)