Skip to content

Commit

Permalink
implement env -u
Browse files Browse the repository at this point in the history
  • Loading branch information
izabera authored and landley committed Feb 10, 2016
1 parent 2f3f26e commit 1e77f70
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions toys/posix/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,45 @@
*
* http:https://opengroup.org/onlinepubs/9699919799/utilities/env.html
USE_ENV(NEWTOY(env, "^i", TOYFLAG_USR|TOYFLAG_BIN))
USE_ENV(NEWTOY(env, "^iu*", TOYFLAG_USR|TOYFLAG_BIN))
config ENV
bool "env"
default y
help
usage: env [-i] [NAME=VALUE...] [command [option...]]
usage: env [-i] [-u NAME] [NAME=VALUE...] [command [option...]]
Set the environment for command invocation.
-i Clear existing environment.
-u NAME Remove NAME from the environment
*/

#define FOR_env
#include "toys.h"

GLOBALS(
struct arg_list *u;
);

extern char **environ;

void env_main(void)
{
char **ev;

if (toys.optflags) clearenv();
if (toys.optflags & FLAG_i) clearenv();
while (TT.u) {
unsetenv(TT.u->arg);
TT.u = TT.u->next;
}

for (ev = toys.optargs; *ev; ev++) {
char *name = *ev, *val = strchr(name, '=');

if (val) {
*(val++) = 0;
if (*val) setenv(name, val, 1);
else unsetenv(name);
setenv(name, val, 1);
} else xexec(ev);
}

Expand Down

0 comments on commit 1e77f70

Please sign in to comment.