summaryrefslogtreecommitdiffstats
path: root/common/env.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2013-09-23 12:49:04 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2013-09-30 10:28:52 +0200
commit5ee4ad2229b676ff521a1da9b8b32f474450f56c (patch)
tree2d5526505b47363b78c1b4fb301d9ef08ebc8067 /common/env.c
parent28141f9e5a465571cf79505b2fd4b9ae207b9ce3 (diff)
downloadbarebox-5ee4ad2229b676ff521a1da9b8b32f474450f56c.tar.gz
environment variables: introduce new helpers
This introduces some new environment variable helpers and updates the existing ones. Newly introduced are: getenv_bool: read a bool variable getenv_ul: read an unsigned long variable getenev_uint: read an unsigned int variable getenv_nonempty: like normal getenv, but does return NULL instead of an empty string All new helpers take a pointer to the value. This value is only modified when the variable exists. This allows the following programming scheme: unsigned int myvalue = sanedefault; getenv_uint("myvalue", &myvalue); So without checking the return value myvalue contains the best possible value. getenv_ull is updated to this scheme. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/env.c')
-rw-r--r--common/env.c71
1 files changed, 66 insertions, 5 deletions
diff --git a/common/env.c b/common/env.c
index 33a871f..748b655 100644
--- a/common/env.c
+++ b/common/env.c
@@ -267,13 +267,74 @@ void export_env_ull(const char *name, unsigned long long val)
}
EXPORT_SYMBOL(export_env_ull);
-unsigned long long getenv_ull(const char *name)
+/*
+ * Like regular getenv, but never returns an empty string.
+ * If the string is empty, NULL is returned instead
+ */
+const char *getenv_nonempty(const char *var)
{
- const char *valstr = getenv(name);
+ const char *val = getenv(var);
- if (!valstr)
- return 0;
+ if (val && *val)
+ return val;
- return simple_strtoull(valstr, NULL, 0);
+ return NULL;
+}
+EXPORT_SYMBOL(getenv_nonempty);
+
+int getenv_ull(const char *var , unsigned long long *val)
+{
+ const char *valstr = getenv(var);
+
+ if (!valstr || !*valstr)
+ return -EINVAL;
+
+ *val = simple_strtoull(valstr, NULL, 0);
+
+ return 0;
}
EXPORT_SYMBOL(getenv_ull);
+
+int getenv_ul(const char *var , unsigned long *val)
+{
+ const char *valstr = getenv(var);
+
+ if (!valstr || !*valstr)
+ return -EINVAL;
+
+ *val = simple_strtoul(valstr, NULL, 0);
+
+ return 0;
+}
+EXPORT_SYMBOL(getenv_ul);
+
+int getenv_uint(const char *var , unsigned int *val)
+{
+ const char *valstr = getenv(var);
+
+ if (!valstr || !*valstr)
+ return -EINVAL;
+
+ *val = simple_strtoul(valstr, NULL, 0);
+
+ return 0;
+}
+EXPORT_SYMBOL(getenv_uint);
+
+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;
+}
+EXPORT_SYMBOL(getenv_bool);