From a66b7f8afd9a58d918f4a5748722f0e64754db7f Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Fri, 29 Apr 2016 11:09:43 +0200 Subject: getenv_bool: use strtobool We now have a library function to convert a string to a boolean type. Use it. Signed-off-by: Sascha Hauer --- common/env.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'common/env.c') diff --git a/common/env.c b/common/env.c index c98ed73f9b..d6cab52a4b 100644 --- a/common/env.c +++ b/common/env.c @@ -28,6 +28,7 @@ #include #include #include +#include #include static struct env_context root = { @@ -323,20 +324,25 @@ int getenv_uint(const char *var , unsigned int *val) } EXPORT_SYMBOL(getenv_uint); +/** + * getenv_bool - get a boolean value from an environment variable + * @var - Variable name + * @val - The boolean value returned. + * + * This function treats + * - any positive (nonzero) number as true + * - "0" as false + * - "true" (case insensitive) as true + * - "false" (case insensitive) as false + * + * Returns 0 for success or negative error code if the variable does + * not exist or contains something this function does not recognize + * as true or false. + */ int getenv_bool(const char *var, int *val) { const char *valstr = getenv(var); - if (!valstr || !*valstr) - return -EINVAL; - - if (!*valstr) - *val = false; - else if (*valstr == '0') - *val = false; - else - *val = true; - - return 0; + return strtobool(valstr, val); } EXPORT_SYMBOL(getenv_bool); -- cgit v1.2.3