From 652d3198036a97c9cb1d3af373d2a927de669599 Mon Sep 17 00:00:00 2001 From: German Service Network Date: Sun, 19 May 2024 21:22:03 +0200 Subject: [PATCH] Handling optparse long option --- src/fping.c | 10 ++++++++++ src/optparse.c | 2 ++ src/optparse.h | 1 + 3 files changed, 13 insertions(+) diff --git a/src/fping.c b/src/fping.c index c58e20c..1c65cbd 100644 --- a/src/fping.c +++ b/src/fping.c @@ -561,6 +561,16 @@ int main(int argc, char **argv) float opt_value_float; while ((c = optparse_long(&optparse_state, longopts, NULL)) != EOF) { switch (c) { + case '0': + /* + * Use long-option example + * Define "struct optparse_long longopts": { "long-option-name", '0', OPTPARSE_NONE } + * + * if(strstr(optparse_state.optlongname, "long-option-name") != NULL) { + * long_option_flag = 1; + * } + */ + break; case '4': #ifdef IPV6 if (hints_ai_family != AF_UNSPEC && hints_ai_family != AF_INET) { diff --git a/src/optparse.c b/src/optparse.c index 4242bff..63a9f40 100644 --- a/src/optparse.c +++ b/src/optparse.c @@ -238,12 +238,14 @@ optparse_long(struct optparse *options, /* Parse as long option. */ options->errmsg[0] = '\0'; options->optopt = 0; + options->optlongname = 0; options->optarg = 0; option += 2; /* skip "--" */ options->optind++; for (int i = 0; !longopts_end(longopts, i); i++) { const char *name = longopts[i].longname; if (longopts_match(name, option)) { + options->optlongname = option; if (longindex) *longindex = i; options->optopt = longopts[i].shortname; diff --git a/src/optparse.h b/src/optparse.h index f124b75..7a555ed 100644 --- a/src/optparse.h +++ b/src/optparse.h @@ -48,6 +48,7 @@ struct optparse { int permute; int optind; int optopt; + char *optlongname; char *optarg; char errmsg[64]; int subopt;