aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-04-20 02:51:51 -0500
committerRob Landley <rob@landley.net>2019-04-20 02:51:51 -0500
commit71c3f623c6011c923788f0770ad40fe2756893a7 (patch)
tree6bbde27b783bb54b0042935c5e750eac5c12364f /toys
parentd8eeedddd0bdcf71d19f2372109c2c897ce8d697 (diff)
downloadtoybox-71c3f623c6011c923788f0770ad40fe2756893a7.tar.gz
New xsetenv() plumbing (repeatedly set same environment variables without
leaking memory), and mod env command to test it.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/env.c19
1 files changed, 8 insertions, 11 deletions
diff --git a/toys/posix/env.c b/toys/posix/env.c
index 5c7bb789..d1bb580d 100644
--- a/toys/posix/env.c
+++ b/toys/posix/env.c
@@ -28,11 +28,10 @@ GLOBALS(
struct arg_list *u;
);
-extern char **environ;
-
void env_main(void)
{
char **ev = toys.optargs;
+ struct arg_list *u;
// If first nonoption argument is "-" treat it as -i
if (*ev && **ev == '-' && !(*ev)[1]) {
@@ -40,21 +39,19 @@ void env_main(void)
ev++;
}
- if (toys.optflags & FLAG_i) clearenv();
- while (TT.u) {
- unsetenv(TT.u->arg);
- TT.u = TT.u->next;
- }
+ 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);
for (; *ev; ev++) {
- char *name = *ev, *val = strchr(name, '=');
+ char *val = strchr(*ev, '=');
if (val) {
*(val++) = 0;
- setenv(name, val, 1);
+ xsetenv(*ev, val);
} else xexec(ev);
}
- if (environ) for (ev = environ; *ev; ev++)
- xprintf("%s%c", *ev, '\n'*!(toys.optflags&FLAG_0));
+ for (ev = environ; *ev; ev++) xprintf("%s%c", *ev, '\n'*!FLAG(0));
}