#include "bsdunzip_platform.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "bsdunzip.h"
#include "err.h"
static const char *short_options
= "aCcd:fI:jLlnO:opP:qtuvx:yZ:";
static const struct bsdunzip_option {
const char *name;
int required;
int equivalent;
} bsdunzip_longopts[] = {
{ "version", 0, OPTION_VERSION },
{ NULL, 0, 0 }
};
int
bsdunzip_getopt(struct bsdunzip *bsdunzip)
{
enum { state_start = 0, state_next_word, state_short, state_long };
const struct bsdunzip_option *popt, *match, *match2;
const char *p, *long_prefix;
size_t optlength;
int opt;
int required;
again:
match = NULL;
match2 = NULL;
long_prefix = "--";
opt = OPTION_NONE;
required = 0;
bsdunzip->argument = NULL;
if (bsdunzip->getopt_state == state_start) {
++bsdunzip->argv;
--bsdunzip->argc;
if (*bsdunzip->argv == NULL)
return (-1);
bsdunzip->getopt_state = state_next_word;
}
if (bsdunzip->getopt_state == state_next_word) {
if (bsdunzip->argv[0] == NULL)
return (-1);
if (bsdunzip->argv[0][0] != '-')
return (-1);
if (strcmp(bsdunzip->argv[0], "--") == 0) {
++bsdunzip->argv;
--bsdunzip->argc;
bsdunzip_optind++;
return (-1);
}
bsdunzip->getopt_word = *bsdunzip->argv++;
--bsdunzip->argc;
bsdunzip_optind++;
if (bsdunzip->getopt_word[1] == '-') {
bsdunzip->getopt_state = state_long;
bsdunzip->getopt_word += 2;
} else {
bsdunzip->getopt_state = state_short;
++bsdunzip->getopt_word;
}
}
if (bsdunzip->getopt_state == state_short) {
opt = *bsdunzip->getopt_word++;
if (opt == '\0') {
bsdunzip->getopt_state = state_next_word;
goto again;
}
p = strchr(short_options, opt);
if (p == NULL)
return ('?');
if (p[1] == ':')
required = 1;
if (required) {
if (bsdunzip->getopt_word[0] == '\0') {
bsdunzip->getopt_word = *bsdunzip->argv;
if (bsdunzip->getopt_word == NULL) {
lafe_warnc(0,
"Option -%c requires an argument",
opt);
return ('?');
}
++bsdunzip->argv;
--bsdunzip->argc;
bsdunzip_optind++;
}
bsdunzip->getopt_state = state_next_word;
bsdunzip->argument = bsdunzip->getopt_word;
}
}
if (bsdunzip->getopt_state == state_long) {
bsdunzip->getopt_state = state_next_word;
p = strchr(bsdunzip->getopt_word, '=');
if (p != NULL) {
optlength = (size_t)(p - bsdunzip->getopt_word);
bsdunzip->argument = (char *)(uintptr_t)(p + 1);
} else {
optlength = strlen(bsdunzip->getopt_word);
}
for (popt = bsdunzip_longopts; popt->name != NULL; popt++) {
if (popt->name[0] != bsdunzip->getopt_word[0])
continue;
if (strncmp(bsdunzip->getopt_word, popt->name, optlength) == 0) {
match2 = match;
match = popt;
if (strlen(popt->name) == optlength) {
match2 = NULL;
break;
}
}
}
if (match == NULL) {
lafe_warnc(0,
"Option %s%s is not supported",
long_prefix, bsdunzip->getopt_word);
return ('?');
}
if (match2 != NULL) {
lafe_warnc(0,
"Ambiguous option %s%s (matches --%s and --%s)",
long_prefix, bsdunzip->getopt_word, match->name, match2->name);
return ('?');
}
if (match->required) {
if (bsdunzip->argument == NULL) {
bsdunzip->argument = *bsdunzip->argv;
if (bsdunzip->argument == NULL) {
lafe_warnc(0,
"Option %s%s requires an argument",
long_prefix, match->name);
return ('?');
}
++bsdunzip->argv;
--bsdunzip->argc;
bsdunzip_optind++;
}
} else {
if (bsdunzip->argument != NULL) {
lafe_warnc(0,
"Option %s%s does not allow an argument",
long_prefix, match->name);
return ('?');
}
}
return (match->equivalent);
}
return (opt);
}