From 42f2f219c618f0c1bf07e536f5cdcc27cb0d6a4b Mon Sep 17 00:00:00 2001 From: Petr Oros Date: Tue, 18 Nov 2025 15:10:30 +0100 Subject: [PATCH] lib: Add str_to_bool helper function 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 Signed-off-by: David Ahern --- devlink/devlink.c | 22 +++------------------- include/utils.h | 1 + lib/utils.c | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index fd9fac21..b95fd348 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -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) diff --git a/include/utils.h b/include/utils.h index 7d9b3cfb..e0a9c780 100644 --- a/include/utils.h +++ b/include/utils.h @@ -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, diff --git a/lib/utils.c b/lib/utils.c index dd242d4d..0719281a 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -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), -- 2.47.3