From: Karel Zak Date: Wed, 18 Mar 2026 10:17:31 +0000 (+0100) Subject: lib/strutils: add ul_strtobool() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5022295152686f103fe4347cae2167bcedd5b8c3;p=thirdparty%2Futil-linux.git lib/strutils: add ul_strtobool() Add a locale-independent string to boolean parsing function. Supported true values: 1, y, t, yes, true, on, enable. Supported false values: 0, no, not, false, off, disable. Signed-off-by: Karel Zak --- diff --git a/include/strutils.h b/include/strutils.h index d034f6b95..f6256efa1 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -71,6 +71,7 @@ extern int isxdigit_strend(const char *str, const char **end); #define isxdigit_string(_s) isxdigit_strend(_s, NULL) +extern int ul_strtobool(const char *str, bool *result); extern int ul_parse_switch(const char *arg, ...); #ifndef HAVE_MEMPCPY diff --git a/lib/strutils.c b/lib/strutils.c index dd67fc554..0fce3c7c6 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -16,6 +16,7 @@ #include #include "c.h" +#include "cctype.h" #include "nls.h" #include "strutils.h" #include "bitops.h" @@ -250,6 +251,34 @@ int isxdigit_strend(const char *str, const char **end) /* * For example: ul_parse_switch(argv[i], "on", "off", "yes", "no", NULL); */ +int ul_strtobool(const char *str, bool *result) +{ + static const char *bool_true[] = { "1", "y", "t", "yes", "true", "on", "enable" }; + static const char *bool_false[] = { "0", "no", "not", "false", "off", "disable" }; + size_t i; + + if (!str || !result) + return -(errno = EINVAL); + + errno = 0; + + for (i = 0; i < ARRAY_SIZE(bool_true); i++) { + if (c_strcasecmp(str, bool_true[i]) == 0) { + *result = true; + return 0; + } + } + + for (i = 0; i < ARRAY_SIZE(bool_false); i++) { + if (c_strcasecmp(str, bool_false[i]) == 0) { + *result = false; + return 0; + } + } + + return -(errno = EINVAL); +} + int ul_parse_switch(const char *arg, ...) { const char *a, *b; @@ -1326,8 +1355,6 @@ char *ul_optstr_get_value(const char *optstr, const char *key) #ifdef TEST_PROGRAM_STRUTILS -#include "cctype.h" - struct testS { char *name; char *value; @@ -1461,6 +1488,13 @@ int main(int argc, char *argv[]) "1", "0", NULL)); return EXIT_SUCCESS; + } else if (argc == 3 && strcmp(argv[1], "--strtobool") == 0) { + bool val; + if (ul_strtobool(argv[2], &val)) + err(EXIT_FAILURE, "could not parse '%s'", argv[2]); + printf("'%s'-->%s\n", argv[2], val ? "true" : "false"); + return EXIT_SUCCESS; + } else if (argc == 4 && strcmp(argv[1], "--cmp-paths") == 0) { return test_strutils_cmp_paths(argc - 1, argv + 1); @@ -1530,6 +1564,7 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } else { fprintf(stderr, "usage: %1$s --size [suffix]\n" + " %1$s --strtobool \n" " %1$s --parse-switch \n" " %1$s --cmp-paths \n" " %1$s --strdup-member \n"