1 /* printenv.c - Print environment variables.
2  *
3  * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
4 
5 USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config PRINTENV
8   bool "printenv"
9   default y
10   help
11     usage: printenv [-0] [env_var...]
12 
13     Print environment variables.
14 
15     -0	Use \0 as delimiter instead of \n
16 */
17 
18 #include "toys.h"
19 
20 extern char **environ;
21 
printenv_main(void)22 void printenv_main(void)
23 {
24   char **env, **var = toys.optargs;
25   char delim = '\n';
26 
27   if (toys.optflags) delim = 0;
28 
29   do {
30     int catch = 0, len = *var ? strlen(*var) : 0;
31 
32     for (env = environ; *env; env++) {
33       char *out = *env;
34       if (*var) {
35         if (!strncmp(out, *var, len) && out[len] == '=') out += len +1;
36         else continue;
37       }
38       xprintf("%s%c", out, delim);
39       catch++;
40     }
41     if (*var && !catch) toys.exitval = 1;
42   } while (*var && *(++var));
43 }
44