annotate 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
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
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
4 USE_ENV(NEWTOY(env, "^?i", TOYFLAG_USR|TOYFLAG_BIN))
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
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
10
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
11 Set the environment for command invocation
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
12 */
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 #include "toys.h"
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
15
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
16 extern char **environ;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
17
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
18 void env_main(void)
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
19 {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
20 char **ev;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
21 char **command = NULL;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
22
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
23 if (toys.optflags & 1) clearenv();
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
24
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
25 for (ev = toys.optargs; *ev != NULL; ev++) {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
26 char *env = NULL, *val = NULL;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
27 char *del = "=";
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
28
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
29 env = strtok(*ev, del);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
30
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
31 if (env != NULL) val = strtok(NULL, del);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
32
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
33 if (val != NULL) {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
34 setenv(env, val, 1);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
35 } else {
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;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
43 if (environ) {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
44 for (ep = environ; *ep != NULL; ep++)
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
45 xputs(*ep);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
46 return;
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
47 }
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
48 } else {
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
49 execvp(*command, command);
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
50 }
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
51
c8a3d740c229 'env' implementation - Initial
Tryn Mirell <tryn@mirell.org>
parents:
diff changeset
52 }