annotate toys/env.c @ 409:55598a9b8f21

'env' and 'basename' refactored
author Tryn Mirell <tryn@mirell.org>
date Fri, 20 Jan 2012 00:02:37 -0600
parents c8a3d740c229
children eda61bcf575a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
2 * env.c
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
3
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
4 USE_ENV(NEWTOY(env, "^i", TOYFLAG_USR|TOYFLAG_BIN))
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
5
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
6 config ENV
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
7 bool "env"
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
8 default n
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
9 help
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
10 usage: env [-i] [FOO=BAR...] [command [option...]]
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
11
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
12 Set the environment for command invocation
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
13 */
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
14
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
15 #include "toys.h"
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
16
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
17 extern char **environ;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
18
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
19 void env_main(void)
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
20 {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
21 char **ev;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
22 char **command = NULL;
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
23 char *del = "=";
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
24
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
25 if (toys.optflags & 1) clearenv();
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
26
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
27 for (ev = toys.optargs; *ev != NULL; ev++) {
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
28 char *env, *val = NULL;
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
29
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
30 env = strtok(*ev, del);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
31
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
32 if (env) val = strtok(NULL, del);
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
33
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
34 if (val) setenv(env, val, 1);
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
35 else {
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
36 command = ev;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
37 break;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
38 }
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
39 }
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
40
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
41 if (!command) {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
42 char **ep;
409
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
43 for (ep = environ; *ep; ep++)
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
44 xputs(*ep);
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
45 return;
55598a9b8f21 'env' and 'basename' refactored
Tryn Mirell <tryn@mirell.org>
parents: 406
diff changeset
46 } else execvp(*command, command);
406
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
47 }