comparison toys/printenv.c @ 518:e0eed40f4ab1

Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
author Rob Landley <rob@landley.net>
date Sun, 04 Mar 2012 00:50:44 -0600
parents a191ea9bc5df
children
comparison
equal deleted inserted replaced
517:a191ea9bc5df 518:e0eed40f4ab1
3 * printenv.c - Print environment variables. 3 * printenv.c - Print environment variables.
4 * 4 *
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> 5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
6 * 6 *
7 7
8 USE_PRINTENV(NEWTOY(printenv, "0", TOYFLAG_USR|TOYFLAG_BIN)) 8 USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_USR|TOYFLAG_BIN))
9 9
10 config PRINTENV 10 config PRINTENV
11 bool "printenv" 11 bool "printenv"
12 default y 12 default y
13 help 13 help
14 usage: printenv [-0] [env_var...] 14 usage: printenv [-0] [env_var...]
15 Print enviroment variables.
16 15
17 -0 Use \0 as environment delimiter instead of \n 16 Print environment variables.
17
18 -0 Use \0 as delimiter instead of \n
18 */ 19 */
19 20
20 #include "toys.h" 21 #include "toys.h"
21 22
22 extern char **environ; 23 extern char **environ;
23 24
24 void printenv_main(void) 25 void printenv_main(void)
25 { 26 {
26 char **env; 27 char **env, **var = toys.optargs;
27 char delim = '\n'; 28 char delim = '\n';
28 29
29 if (toys.optflags) 30 if (toys.optflags) delim = 0;
30 delim = '\0';
31 31
32 if (!toys.optargs[0]) { 32 do {
33 for (env = environ; *env; env++) 33 int catch = 0, len = *var ? strlen(*var) : 0;
34 xprintf("%s%c", *env, delim); 34
35 } else { 35 for (env = environ; *env; env++) {
36 char **var = toys.optargs; 36 char *out = *env;
37 for (var = toys.optargs; *var; var++) { 37 if (*var) {
38 int len = strlen(*var); 38 if (!strncmp(out, *var, len) && out[len] == '=')
39 for (env = environ; *env; env++) { 39 out += len +1;
40 if (strncmp(*env, *var, len) == 0) 40 else continue;
41 xprintf("%s%c", *env + len + 1, delim);
42 } 41 }
42 xprintf("%s%c", out, delim);
43 catch++;
43 } 44 }
44 } 45 if (*var && !catch) toys.exitval = 1;
46 } while (*var && *(++var));
45 } 47 }