From: Masatake YAMATO Date: Thu, 14 Oct 2021 13:51:18 +0000 (+0900) Subject: lsfd: (helper) accept an integer argument for a parameter X-Git-Tag: v2.38-rc1~144^2~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cb9c4cabedc5b01b656629bff91b66d0c293545b;p=thirdparty%2Futil-linux.git lsfd: (helper) accept an integer argument for a parameter Signed-off-by: Masatake YAMATO --- diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index ad89626b8d..d318aff2b8 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -55,10 +55,12 @@ static void __attribute__((__noreturn__)) usage(FILE *out, int status) union value { const char *string; + long integer; }; enum ptype { PTYPE_STRING, + PTYPE_INTEGER, }; struct ptype_class { @@ -78,6 +80,7 @@ struct ptype_class { }; #define ARG_STRING(A) (A.v.string) +#define ARG_INTEGER(A) (A.v.integer) struct arg { union value v; void (*free)(union value value); @@ -105,6 +108,36 @@ static void string_free(union value value) free((void *)value.string); } +static char *integer_sprint(const union value *value) +{ + char *str = NULL; + xasprintf(&str, "%ld", value->integer); + return str; +} + +static union value integer_read(const char *arg, const union value *defv) +{ + char *ep; + union value r; + + if (!arg) + return *defv; + + errno = 0; + r.integer = strtol(arg, &ep, 10); + if (errno) + err(EXIT_FAILURE, _("fail to make a number from %s"), arg); + else if (*ep != '\0') + errx(EXIT_FAILURE, _("garbage at the end of number: %s"), arg); + return r; +} + +static void integer_free(union value value _U_) +{ + /* Do nothing */ +} + + struct ptype_class ptype_classes [] = { [PTYPE_STRING] = { .name = "string", @@ -112,6 +145,12 @@ struct ptype_class ptype_classes [] = { .read = string_read, .free = string_free, }, + [PTYPE_INTEGER] = { + .name = "integer", + .sprint = integer_sprint, + .read = integer_read, + .free = integer_free, + }, }; static struct arg decode_arg(const char *pname,