summaryrefslogtreecommitdiffstats
path: root/common/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/env.c')
-rw-r--r--common/env.c87
1 files changed, 60 insertions, 27 deletions
diff --git a/common/env.c b/common/env.c
index 323a82223e..c231bc8b1d 100644
--- a/common/env.c
+++ b/common/env.c
@@ -1,20 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-only
/*
* env.c - environment variables storage
*
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
*/
/**
@@ -92,7 +80,7 @@ int env_push_context(void)
*/
int env_pop_context(void)
{
- struct env_context *c = context;
+ struct env_context *c;
if (context->parent) {
c = context->parent;
@@ -139,7 +127,7 @@ static const char *dev_getenv(const char *name)
{
const char *pos, *val, *dot, *varname;
char *devname;
- struct device_d *dev;
+ struct device *dev;
pos = name;
@@ -227,7 +215,7 @@ static int dev_setenv(const char *name, const char *val)
{
const char *pos, *dot, *varname;
char *devname;
- struct device_d *dev;
+ struct device *dev;
pos = name;
@@ -254,15 +242,20 @@ static int dev_setenv(const char *name, const char *val)
return -ENODEV;
}
+/**
+ * setenv - set environment variables
+ * @name - Variable name
+ * @value - the value to set, empty string not handled specially
+ *
+ * Returns 0 for success and a negative error code otherwise
+ * Use unsetenv() to unset.
+ */
int setenv(const char *_name, const char *value)
{
char *name = strdup(_name);
int ret = 0;
struct list_head *list;
- if (value && !*value)
- value = NULL;
-
if (strchr(name, '.')) {
ret = dev_setenv(name, value);
if (ret)
@@ -283,6 +276,35 @@ out:
}
EXPORT_SYMBOL(setenv);
+/**
+ * pr_setenv - set environment variables
+ * @name - Variable name
+ * @fmt - the format string to use
+ *
+ * Returns 0 for success and a negative error code otherwise
+ * Use unsetenv() to unset.
+ */
+int pr_setenv(const char *name, const char *fmt, ...)
+{
+ va_list ap;
+ int ret = 0;
+ char *value;
+ int len;
+
+ va_start(ap, fmt);
+ len = vasprintf(&value, fmt, ap);
+ va_end(ap);
+
+ if (len < 0)
+ return -ENOMEM;
+
+ ret = setenv(name, value);
+ free(value);
+
+ return ret;
+}
+EXPORT_SYMBOL(pr_setenv);
+
int export(const char *varname)
{
const char *val = getenv_raw(&context->local, varname);
@@ -314,31 +336,42 @@ const char *getenv_nonempty(const char *var)
{
const char *val = getenv(var);
- if (val && *val)
- return val;
+ if (isempty(val))
+ return NULL;
- return NULL;
+ return val;
}
EXPORT_SYMBOL(getenv_nonempty);
-int getenv_ull(const char *var , unsigned long long *val)
+static int getenv_ull_base(const char *var, int base, unsigned long long *val)
{
const char *valstr = getenv(var);
- if (!valstr || !*valstr)
+ if (isempty(valstr))
return -EINVAL;
- *val = simple_strtoull(valstr, NULL, 0);
+ *val = simple_strtoull(valstr, NULL, base);
return 0;
}
+
+int getenv_ull(const char *var , unsigned long long *val)
+{
+ return getenv_ull_base(var, 0, val);
+}
EXPORT_SYMBOL(getenv_ull);
+int getenv_ullx(const char *var , unsigned long long *val)
+{
+ return getenv_ull_base(var, 16, val);
+}
+EXPORT_SYMBOL(getenv_ullx);
+
int getenv_ul(const char *var , unsigned long *val)
{
const char *valstr = getenv(var);
- if (!valstr || !*valstr)
+ if (isempty(valstr))
return -EINVAL;
*val = simple_strtoul(valstr, NULL, 0);
@@ -351,7 +384,7 @@ int getenv_uint(const char *var , unsigned int *val)
{
const char *valstr = getenv(var);
- if (!valstr || !*valstr)
+ if (isempty(valstr))
return -EINVAL;
*val = simple_strtoul(valstr, NULL, 0);