summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2022-06-17 10:01:51 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2022-06-21 12:33:30 +0200
commitecd082ad21a1d07973f42adbd5f2a20a07fdc71f (patch)
treef5e7ff25051d712cfbcb6325550129ae50b92f95 /common
parent825d95150d88a9204ac37d86b238c3a5112cb2e8 (diff)
downloadbarebox-ecd082ad21a1d07973f42adbd5f2a20a07fdc71f.tar.gz
barebox-ecd082ad21a1d07973f42adbd5f2a20a07fdc71f.tar.xz
env: Introduce pr_setenv()
It's a common pattern to (ba)sprintf to a string and then call setenv() with this string. Introduce pr_setenv() as a shortcut to simplify this pattern. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common')
-rw-r--r--common/env.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/common/env.c b/common/env.c
index 05add63f62..5ac1e62b8c 100644
--- a/common/env.c
+++ b/common/env.c
@@ -244,13 +244,12 @@ static int dev_setenv(const char *name, const char *val)
/**
* setenv - set environment variables
- * @_name - Variable name
+ * @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);
@@ -277,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);