]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: Add str_to_bool helper function
authorPetr Oros <poros@redhat.com>
Tue, 18 Nov 2025 14:10:30 +0000 (15:10 +0100)
committerDavid Ahern <dsahern@kernel.org>
Fri, 21 Nov 2025 16:10:22 +0000 (09:10 -0700)
Add str_to_bool() helper function to lib/utils.c that uses
parse_one_of() to parse boolean values. Update devlink to
use this common implementation.

Signed-off-by: Petr Oros <poros@redhat.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
devlink/devlink.c
include/utils.h
lib/utils.c

index fd9fac216e45f0d3cdb61cb606560d7433c9d2c4..b95fd348007c44ebc6ef304b0e95885b7aa93fab 100644 (file)
@@ -1042,22 +1042,6 @@ static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
        return -ENOENT;
 }
 
-static int strtobool(const char *str, bool *p_val)
-{
-       bool val;
-
-       if (!strcmp(str, "true") || !strcmp(str, "1") ||
-           !strcmp(str, "enable"))
-               val = true;
-       else if (!strcmp(str, "false") || !strcmp(str, "0") ||
-                !strcmp(str, "disable"))
-               val = false;
-       else
-               return -EINVAL;
-       *p_val = val;
-       return 0;
-}
-
 static int ident_str_validate(char *str, unsigned int expected)
 {
        if (!str)
@@ -1360,7 +1344,7 @@ static int dl_argv_bool(struct dl *dl, bool *p_val)
                return -EINVAL;
        }
 
-       err = strtobool(str, p_val);
+       err = str_to_bool(str, p_val);
        if (err) {
                pr_err("\"%s\" is not a valid boolean value\n", str);
                return err;
@@ -3862,7 +3846,7 @@ static int cmd_dev_param_set(struct dl *dl)
                mnl_attr_put_u64(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u64);
                break;
        case MNL_TYPE_FLAG:
-               err = strtobool(dl->opts.param_value, &val_bool);
+               err = str_to_bool(dl->opts.param_value, &val_bool);
                if (err)
                        goto err_param_value_parse;
                if (val_bool == ctx.value.vbool)
@@ -5454,7 +5438,7 @@ static int cmd_port_param_set(struct dl *dl)
                mnl_attr_put_u64(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u64);
                break;
        case MNL_TYPE_FLAG:
-               err = strtobool(dl->opts.param_value, &val_bool);
+               err = str_to_bool(dl->opts.param_value, &val_bool);
                if (err)
                        goto err_param_value_parse;
                if (val_bool == ctx.value.vbool)
index 7d9b3cfb35a66572e051a1fd5a90d0c78cb2475c..e0a9c780cb9eb16223905374916813d1b1e7711f 100644 (file)
@@ -351,6 +351,7 @@ int parse_one_of_deprecated(const char *msg, const char *realval,
                            const char * const *list,
                            size_t len, int *p_err);
 bool parse_on_off(const char *msg, const char *realval, int *p_err);
+int str_to_bool(const char *str, bool *p_val);
 
 int parse_mapping_num_all(__u32 *keyp, const char *key);
 int parse_mapping_gen(int *argcp, char ***argvp,
index dd242d4d672e47a42c7060e258860ceb1c264af8..0719281a059d14499222292dd0394a7d3a88b9e8 100644 (file)
@@ -1820,6 +1820,23 @@ bool parse_on_off(const char *msg, const char *realval, int *p_err)
                              ARRAY_SIZE(values_on_off), p_err, strcmp);
 }
 
+int str_to_bool(const char *str, bool *p_val)
+{
+       static const char * const values[] = {
+               "false", "true",
+               "0", "1",
+               "disable", "enable"
+       };
+       int err, index;
+
+       index = parse_one_of(NULL, str, values, ARRAY_SIZE(values), &err);
+       if (err)
+               return err;
+
+       *p_val = index & 1;
+       return 0;
+}
+
 int parse_mapping_gen(int *argcp, char ***argvp,
                      int (*key_cb)(__u32 *keyp, const char *key),
                      int (*mapping_cb)(__u32 key, char *value, void *data),