comparison toys/env.c @ 406:c8a3d740c229

'env' implementation - Initial
author Tryn Mirell <tryn@mirell.org>
date Mon, 16 Jan 2012 01:48:51 -0600
parents
children 55598a9b8f21
comparison
equal deleted inserted replaced
405:a8b14410e784 406:c8a3d740c229
1 /* vi: set sw=4 ts=4:
2 * env.c
3
4 USE_ENV(NEWTOY(env, "^?i", TOYFLAG_USR|TOYFLAG_BIN))
5
6 config ENV
7 bool "env"
8 default n
9 help
10
11 Set the environment for command invocation
12 */
13
14 #include "toys.h"
15
16 extern char **environ;
17
18 void env_main(void)
19 {
20 char **ev;
21 char **command = NULL;
22
23 if (toys.optflags & 1) clearenv();
24
25 for (ev = toys.optargs; *ev != NULL; ev++) {
26 char *env = NULL, *val = NULL;
27 char *del = "=";
28
29 env = strtok(*ev, del);
30
31 if (env != NULL) val = strtok(NULL, del);
32
33 if (val != NULL) {
34 setenv(env, val, 1);
35 } else {
36 command = ev;
37 break;
38 }
39 }
40
41 if (!command) {
42 char **ep;
43 if (environ) {
44 for (ep = environ; *ep != NULL; ep++)
45 xputs(*ep);
46 return;
47 }
48 } else {
49 execvp(*command, command);
50 }
51
52 }