summaryrefslogtreecommitdiffstats
path: root/environ.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2010-04-04 19:02:09 +0000
committerNicholas Marriott <nicm@openbsd.org>2010-04-04 19:02:09 +0000
commitb02cd353543cb47ddaf079bd42f94696703f2667 (patch)
treea7f93d71da25c2bae91d2f3a97b60ccf9d63a9ee /environ.c
parent7425122c1cd2aff909d72b33ef07b6a71d0c6922 (diff)
Run job commands explicitly in the global enviroment (which can be
modified with setenv -g) rather than with the environment tmux started with.
Diffstat (limited to 'environ.c')
-rw-r--r--environ.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/environ.c b/environ.c
index 353b84fa..4c4af98f 100644
--- a/environ.c
+++ b/environ.c
@@ -35,12 +35,14 @@ environ_cmp(struct environ_entry *envent1, struct environ_entry *envent2)
return (strcmp(envent1->name, envent2->name));
}
+/* Initialise the environment. */
void
environ_init(struct environ *env)
{
RB_INIT(env);
}
+/* Free an environment. */
void
environ_free(struct environ *env)
{
@@ -56,6 +58,7 @@ environ_free(struct environ *env)
}
}
+/* Copy one environment into another. */
void
environ_copy(struct environ *srcenv, struct environ *dstenv)
{
@@ -65,6 +68,7 @@ environ_copy(struct environ *srcenv, struct environ *dstenv)
environ_set(dstenv, envent->name, envent->value);
}
+/* Find an environment variable. */
struct environ_entry *
environ_find(struct environ *env, const char *name)
{
@@ -74,6 +78,7 @@ environ_find(struct environ *env, const char *name)
return (RB_FIND(environ, env, &envent));
}
+/* Set an environment variable. */
void
environ_set(struct environ *env, const char *name, const char *value)
{
@@ -97,10 +102,11 @@ environ_set(struct environ *env, const char *name, const char *value)
}
}
+/* Set an environment variable from a NAME=VALUE string. */
void
environ_put(struct environ *env, const char *var)
{
- char *name, *value;
+ char *name, *value;
value = strchr(var, '=');
if (value == NULL)
@@ -114,6 +120,7 @@ environ_put(struct environ *env, const char *var)
xfree(name);
}
+/* Unset an environment variable. */
void
environ_unset(struct environ *env, const char *name)
{
@@ -128,6 +135,10 @@ environ_unset(struct environ *env, const char *name)
xfree(envent);
}
+/*
+ * Copy a space-separated list of variables from a destination into a source
+ * environment.
+ */
void
environ_update(const char *vars, struct environ *srcenv, struct environ *dstenv)
{
@@ -143,3 +154,28 @@ environ_update(const char *vars, struct environ *srcenv, struct environ *dstenv)
}
xfree(copyvars);
}
+
+/* Push environment into the real environment - use after fork(). */
+void
+environ_push(struct environ *env)
+{
+ ARRAY_DECL(, char *) varlist;
+ struct environ_entry *envent;
+ char **varp, *var;
+ u_int i;
+
+ ARRAY_INIT(&varlist);
+ for (varp = environ; *varp != NULL; varp++) {
+ var = xstrdup(*varp);
+ var[strcspn(var, "=")] = '\0';
+ ARRAY_ADD(&varlist, var);
+ }
+ for (i = 0; i < ARRAY_LENGTH(&varlist); i++)
+ unsetenv(ARRAY_ITEM(&varlist, i));
+ ARRAY_FREE(&varlist);
+
+ RB_FOREACH(envent, environ, env) {
+ if (envent->value != NULL)
+ setenv(envent->name, envent->value, 1);
+ }
+}