summaryrefslogtreecommitdiffstats
path: root/common/env.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-04-29 11:09:43 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-04-29 11:29:25 +0200
commita66b7f8afd9a58d918f4a5748722f0e64754db7f (patch)
tree179452d1907cd156101607405410447b8e975ecc /common/env.c
parent22b8e9415d29c1e11cef0efb0aafd5ebd78cb51b (diff)
downloadbarebox-a66b7f8afd9a58d918f4a5748722f0e64754db7f.tar.gz
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 <s.hauer@pengutronix.de>
Diffstat (limited to 'common/env.c')
-rw-r--r--common/env.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/common/env.c b/common/env.c
index c98ed73..d6cab52 100644
--- a/common/env.c
+++ b/common/env.c
@@ -28,6 +28,7 @@
#include <xfuncs.h>
#include <errno.h>
#include <init.h>
+#include <string.h>
#include <environment.h>
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);