60#pragma warning(disable : 4996)
84#define optreset __mingw_optreset
89#define PRINT_ERROR ((opterr) && (*options != ':'))
91#define FLAG_PERMUTE 0x01
92#define FLAG_ALLARGS 0x02
93#define FLAG_LONGONLY 0x04
97#define BADARG ((*options == ':') ? (int)':' : (int)'?')
101#define __progname __argv[0]
103extern char __declspec(dllimport) *
__progname;
107static char EMSG[] =
"";
113 const struct option *,
int *,
int);
115 const struct option *,
int *,
int);
116static int gcd(
int,
int);
126static const char recargchar[] =
"option requires an argument -- %c";
128static const char ambig[] =
"ambiguous option -- %.*s";
129static const char noarg[] =
"option doesn't take an argument -- %.*s";
133static void _vwarnx(
const char *fmt, va_list ap) {
136 (void)vfprintf(stderr, fmt, ap);
137 (void)fprintf(stderr,
"\n");
140static void warnx(
const char *fmt, ...) {
150static int gcd(
int a,
int b) {
168static void permute_args(
int panonopt_start,
int panonopt_end,
int opt_end,
169 char *
const *nargv) {
170 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
176 nnonopts = panonopt_end - panonopt_start;
177 nopts = opt_end - panonopt_end;
178 ncycle =
gcd(nnonopts, nopts);
179 cyclelen = (opt_end - panonopt_start) / ncycle;
181 for (i = 0; i < ncycle; i++) {
182 cstart = panonopt_end + i;
184 for (j = 0; j < cyclelen; j++) {
185 if (pos >= panonopt_end)
191 ((
char **)nargv)[pos] = nargv[cstart];
193 ((
char **)nargv)[cstart] = swap;
205int getopt(
int nargc,
char *
const *nargv,
const char *options) {
228#define optreset __mingw_optreset
245#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
246#define __GETOPT_LONG_H__
273 const struct option *long_options,
int *idx,
275 char *current_argv, *has_equal;
276 size_t current_argv_len;
277 int i, ambiguous, match;
279#define IDENTICAL_INTERPRETATION(_x, _y) \
280 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
281 long_options[(_x)].flag == long_options[(_y)].flag && \
282 long_options[(_x)].val == long_options[(_y)].val)
284 current_argv =
place;
290 if ((has_equal = strchr(current_argv,
'=')) != NULL) {
292 current_argv_len = has_equal - current_argv;
295 current_argv_len = strlen(current_argv);
297 for (i = 0; long_options[i].
name; i++) {
299 if (strncmp(current_argv, long_options[i].name, current_argv_len))
302 if (strlen(long_options[i].name) == current_argv_len) {
312 if (short_too && current_argv_len == 1)
323 warnx(
ambig, (
int)current_argv_len, current_argv);
328 if (long_options[match].has_arg ==
no_argument && has_equal) {
330 warnx(
noarg, (
int)current_argv_len, current_argv);
334 if (long_options[match].flag == NULL)
362 if (long_options[match].flag == NULL)
381 if (long_options[match].flag) {
382 *long_options[match].
flag = long_options[match].
val;
385 return (long_options[match].val);
386#undef IDENTICAL_INTERPRETATION
394 const struct option *long_options,
int *idx,
397 int optchar, short_too;
398 static int posixly_correct = -1;
417 if (posixly_correct == -1 ||
optreset != 0)
418 posixly_correct = (getenv(
"POSIXLY_CORRECT") != NULL);
421 else if (posixly_correct || *options ==
'+')
423 if (*options ==
'+' || *options ==
'-')
449 (
place[1] ==
'\0' && strchr(options,
'-') == NULL)) {
506 if (long_options != NULL &&
place != nargv[
optind] &&
511 else if (*
place !=
':' && strchr(options, *
place) != NULL)
521 if ((optchar = (
int)*
place++) == (
int)
':' ||
522 (optchar == (
int)
'-' && *
place !=
'\0') ||
523 (oli = (
char *)strchr(options, optchar)) == NULL) {
529 if (optchar == (
int)
'-' && *
place ==
'\0')
538 if (long_options != NULL && optchar ==
'W' && oli[1] ==
';') {
542 else if (++
optind >= nargc) {
561 else if (oli[1] !=
':') {
582int getopt_long(
int nargc,
char *
const *nargv,
const char *options,
583 const struct option *long_options,
int *idx) {
594 const struct option *long_options,
int *idx) {
607#ifndef HAVE_DECL_GETOPT
611#define HAVE_DECL_GETOPT 1
int getopt_long_only(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
static const char noarg[]
static void _vwarnx(const char *fmt, va_list ap)
static const char recargstring[]
static const char illoptstring[]
int getopt(int nargc, char *const *nargv, const char *options)
int getopt_long(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
static const char illoptchar[]
static int getopt_internal(int, char *const *, const char *, const struct option *, int *, int)
static void permute_args(int, int, int, char *const *)
#define IDENTICAL_INTERPRETATION(_x, _y)
static void warnx(const char *fmt,...)
static int parse_long_options(char *const *, const char *, const struct option *, int *, int)
static const char recargchar[]
static const char ambig[]