summaryrefslogtreecommitdiffstats
path: root/environ.c
diff options
context:
space:
mode:
authornicm <nicm>2015-11-24 23:46:15 +0000
committernicm <nicm>2015-11-24 23:46:15 +0000
commit62d3af17f9e6aec244ab1d91c1c46fdbbf6f8a86 (patch)
treee1479657534e995ea2d25785a08d3f0238b66338 /environ.c
parent3ff46b2e43d48376f74c0fd74b5616925d4063f0 (diff)
Make environ_set va_args and use it to tidy up some calls. Also add a
missing word in manpage (from jmc).
Diffstat (limited to 'environ.c')
-rw-r--r--environ.c46
1 files changed, 32 insertions, 14 deletions
diff --git a/environ.c b/environ.c
index 43a9ce01..de560896 100644
--- a/environ.c
+++ b/environ.c
@@ -83,8 +83,12 @@ environ_copy(struct environ *srcenv, struct environ *dstenv)
{
struct environ_entry *envent;
- RB_FOREACH(envent, environ, srcenv)
- environ_set(dstenv, envent->name, envent->value);
+ RB_FOREACH(envent, environ, srcenv) {
+ if (envent->value == NULL)
+ environ_clear(dstenv, envent->name);
+ else
+ environ_set(dstenv, envent->name, "%s", envent->value);
+ }
}
/* Find an environment variable. */
@@ -99,23 +103,37 @@ environ_find(struct environ *env, const char *name)
/* Set an environment variable. */
void
-environ_set(struct environ *env, const char *name, const char *value)
+environ_set(struct environ *env, const char *name, const char *fmt, ...)
{
struct environ_entry *envent;
+ va_list ap;
+ va_start(ap, fmt);
if ((envent = environ_find(env, name)) != NULL) {
free(envent->value);
- if (value != NULL)
- envent->value = xstrdup(value);
- else
- envent->value = NULL;
+ xvasprintf(&envent->value, fmt, ap);
} else {
envent = xmalloc(sizeof *envent);
envent->name = xstrdup(name);
- if (value != NULL)
- envent->value = xstrdup(value);
- else
- envent->value = NULL;
+ xvasprintf(&envent->value, fmt, ap);
+ RB_INSERT(environ, env, envent);
+ }
+ va_end(ap);
+}
+
+/* Clear an environment variable. */
+void
+environ_clear(struct environ *env, const char *name)
+{
+ struct environ_entry *envent;
+
+ if ((envent = environ_find(env, name)) != NULL) {
+ free(envent->value);
+ envent->value = NULL;
+ } else {
+ envent = xmalloc(sizeof *envent);
+ envent->name = xstrdup(name);
+ envent->value = NULL;
RB_INSERT(environ, env, envent);
}
}
@@ -134,7 +152,7 @@ environ_put(struct environ *env, const char *var)
name = xstrdup(var);
name[strcspn(name, "=")] = '\0';
- environ_set(env, name, value);
+ environ_set(env, name, "%s", value);
free(name);
}
@@ -166,9 +184,9 @@ environ_update(const char *vars, struct environ *srcenv,
copyvars = next = xstrdup(vars);
while ((var = strsep(&next, " ")) != NULL) {
if ((envent = environ_find(srcenv, var)) == NULL)
- environ_set(dstenv, var, NULL);
+ environ_clear(dstenv, var);
else
- environ_set(dstenv, envent->name, envent->value);
+ environ_set(dstenv, envent->name, "%s", envent->value);
}
free(copyvars);
}