aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/env.c6
-rw-r--r--lib/lib.h1
-rw-r--r--toys/posix/env.c15
3 files changed, 11 insertions, 11 deletions
diff --git a/lib/env.c b/lib/env.c
index 94ea0a4e..35ef688c 100644
--- a/lib/env.c
+++ b/lib/env.c
@@ -80,6 +80,12 @@ void xsetenv(char *name, char *val)
environ[i] = new;
}
+void xunsetenv(char *name)
+{
+ if (strchr(name, '=')) error_exit("xunsetenv %s name has =", name);
+ xsetenv(name, 0);
+}
+
// reset environment for a user, optionally clearing most of it
void reset_env(struct passwd *p, int clear)
{
diff --git a/lib/lib.h b/lib/lib.h
index 82c4c16e..7572a8a3 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -275,6 +275,7 @@ int human_readable(char *buf, unsigned long long num, int style);
long environ_bytes();
void xsetenv(char *name, char *val);
+void xunsetenv(char *name);
void xclearenv(void);
// linestack.c
diff --git a/toys/posix/env.c b/toys/posix/env.c
index d1bb580d..3a251334 100644
--- a/toys/posix/env.c
+++ b/toys/posix/env.c
@@ -40,18 +40,11 @@ void env_main(void)
}
if (FLAG(i)) xclearenv();
- else for (u = TT.u; u; u = u->next)
- if (strchr(u->arg, '=')) error_msg("bad -u %s", u->arg);
- else xsetenv(u->arg, 0);
+ else for (u = TT.u; u; u = u->next) xunsetenv(u->arg);
- for (; *ev; ev++) {
- char *val = strchr(*ev, '=');
-
- if (val) {
- *(val++) = 0;
- xsetenv(*ev, val);
- } else xexec(ev);
- }
+ for (; *ev; ev++)
+ if (strchr(*ev, '=')) xsetenv(xstrdup(*ev), 0);
+ else xexec(ev);
for (ev = environ; *ev; ev++) xprintf("%s%c", *ev, '\n'*!FLAG(0));
}