mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/2] env: let setenv() take printf arguments
Date: Fri, 17 Jun 2022 10:05:32 +0200	[thread overview]
Message-ID: <20220617080533.1194160-1-s.hauer@pengutronix.de> (raw)

It's a common pattern to (ba)sprintf to a string and then call setenv()
with this string. Let setenv() take printf arguments to make that
easier.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/env.c          | 10 +++++++++-
 include/environment.h |  5 +++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/common/env.c b/common/env.c
index 05add63f62..d69c86feab 100644
--- a/common/env.c
+++ b/common/env.c
@@ -251,11 +251,18 @@ static int dev_setenv(const char *name, const char *val)
  * Use unsetenv() to unset.
  */
 
-int setenv(const char *_name, const char *value)
+int setenv(const char *_name, const char *fmt, ...)
 {
+	va_list ap;
 	char *name = strdup(_name);
 	int ret = 0;
 	struct list_head *list;
+	char *value;
+	int len;
+
+	va_start(ap, fmt);
+	len = vasprintf(&value, fmt, ap);
+	va_end(ap);
 
 	if (strchr(name, '.')) {
 		ret = dev_setenv(name, value);
@@ -271,6 +278,7 @@ int setenv(const char *_name, const char *value)
 
 	ret = setenv_raw(list, name, value);
 out:
+	free(value);
 	free(name);
 
 	return ret;
diff --git a/include/environment.h b/include/environment.h
index 19e522cfb6..9e1cb5a929 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -31,7 +31,7 @@ char *var_name(struct variable_d *);
 
 #ifdef CONFIG_ENVIRONMENT_VARIABLES
 const char *getenv(const char *);
-int setenv(const char *, const char *);
+int setenv(const char *, const char *fmt, ...)  __attribute__ ((format(__printf__, 2, 3)));
 void export_env_ull(const char *name, unsigned long long val);
 int getenv_ull(const char *name, unsigned long long *val);
 int getenv_ul(const char *name, unsigned long *val);
@@ -44,7 +44,8 @@ static inline char *getenv(const char *var)
 	return NULL;
 }
 
-static inline int setenv(const char *var, const char *val)
+static inline __attribute__ ((format(__printf__, 2, 3))) int setenv(
+	const char *var, const char *fmt, ...)
 {
 	return 0;
 }
-- 
2.30.2




             reply	other threads:[~2022-06-17  8:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-17  8:05 Sascha Hauer [this message]
2022-06-17  8:05 ` [PATCH 2/2] treewide: Simplify setenv() calls Sascha Hauer
2022-06-17 21:53   ` Daniel Brát
2022-06-20  7:21     ` [PATCH] env: let setenv() take printf arguments Ahmad Fatoum
2022-06-20  7:47       ` Sascha Hauer
2022-06-20  7:59         ` Ahmad Fatoum
2022-06-20  8:16           ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220617080533.1194160-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox