comparison toys/posix/env.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children e3ebec880fe6
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* env.c - Set the environment for command invocation.
2 *
3 * env.c - Set the environment for command invocation.
4 * 2 *
5 * Copyright 2012 Tryn Mirell <tryn@mirell.org> 3 * Copyright 2012 Tryn Mirell <tryn@mirell.org>
6 * 4 *
7 * http://opengroup.org/onlinepubs/9699919799/utilities/env.html 5 * http://opengroup.org/onlinepubs/9699919799/utilities/env.html
8 6
9 USE_ENV(NEWTOY(env, "^i", TOYFLAG_USR|TOYFLAG_BIN)) 7 USE_ENV(NEWTOY(env, "^i", TOYFLAG_USR|TOYFLAG_BIN))
10 8
11 config ENV 9 config ENV
12 bool "env" 10 bool "env"
13 default y 11 default y
14 help 12 help
15 usage: env [-i] [NAME=VALUE...] [command [option...]] 13 usage: env [-i] [NAME=VALUE...] [command [option...]]
16 14
17 Set the environment for command invocation. 15 Set the environment for command invocation.
18 16
19 -i Clear existing environment. 17 -i Clear existing environment.
20 */ 18 */
21 19
22 #include "toys.h" 20 #include "toys.h"
23 21
24 extern char **environ; 22 extern char **environ;
25 23
26 void env_main(void) 24 void env_main(void)
27 { 25 {
28 char **ev; 26 char **ev;
29 char **command = NULL; 27 char **command = NULL;
30 char *del = "="; 28 char *del = "=";
31 29
32 if (toys.optflags) clearenv(); 30 if (toys.optflags) clearenv();
33 31
34 for (ev = toys.optargs; *ev != NULL; ev++) { 32 for (ev = toys.optargs; *ev != NULL; ev++) {
35 char *env, *val = NULL; 33 char *env, *val = NULL;
36 34
37 env = strtok(*ev, del); 35 env = strtok(*ev, del);
38 36
39 if (env) val = strtok(NULL, del); 37 if (env) val = strtok(NULL, del);
40 38
41 if (val) setenv(env, val, 1); 39 if (val) setenv(env, val, 1);
42 else { 40 else {
43 command = ev; 41 command = ev;
44 break; 42 break;
45 }
46 } 43 }
47 44 }
48 if (!command) { 45
49 char **ep; 46 if (!command) {
50 for (ep = environ; *ep; ep++) xputs(*ep); 47 char **ep;
51 return; 48 for (ep = environ; *ep; ep++) xputs(*ep);
52 } else xexec(command); 49 return;
50 } else xexec(command);
53 } 51 }