Skip to content

Commit

Permalink
detect non-option command-line arg
Browse files Browse the repository at this point in the history
  • Loading branch information
zfl9 committed May 8, 2023
1 parent 1e607e5 commit 4deb726
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,13 @@ static void parse_noaaaa_rules(const char *rules) {

void opt_parse(int argc, char *argv[]) {
opterr = 0; /* disable default error msg */

int optindex = -1;
int shortopt = -1;
int shortopt;

const char *chinadns_optarg = "114.114.114.114";
const char *trustdns_optarg = "8.8.8.8";

char no_arg;

while ((shortopt = getopt_long(argc, argv, s_shortopts, s_options, &optindex)) != -1) {
while ((shortopt = getopt_long(argc, argv, s_shortopts, s_options, NULL)) != -1) {
switch (shortopt) {
case OPT_BIND_ADDR:
if (get_ipstr_family(optarg) == -1)
Expand Down Expand Up @@ -388,9 +385,16 @@ void opt_parse(int argc, char *argv[]) {
err_exit("unknown option: '%.*s'", len, longopt);
}
break;

default:
err_exit("unprocessed option: '%s'", argv[optind - 1]);
break;
}
}

for (int i = optind; i < argc; ++i)
err_exit("non-option argument: %s", argv[i]);

if ((uintptr_t)g_add_tagchn_ip == (uintptr_t)&no_arg) {
size_t len4 = strlen(g_ipset_name4) + 1;
size_t len6 = strlen(g_ipset_name6) + 1;
Expand Down

0 comments on commit 4deb726

Please sign in to comment.