diff options
-rw-r--r-- | toys/printenv.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/toys/printenv.c b/toys/printenv.c index da75acdf..305b3e7d 100644 --- a/toys/printenv.c +++ b/toys/printenv.c @@ -5,16 +5,17 @@ * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> * -USE_PRINTENV(NEWTOY(printenv, "0", TOYFLAG_USR|TOYFLAG_BIN)) +USE_PRINTENV(NEWTOY(printenv, "0(null)", TOYFLAG_USR|TOYFLAG_BIN)) config PRINTENV bool "printenv" default y help usage: printenv [-0] [env_var...] - Print enviroment variables. - -0 Use \0 as environment delimiter instead of \n + Print environment variables. + + -0 Use \0 as delimiter instead of \n */ #include "toys.h" @@ -23,23 +24,24 @@ extern char **environ; void printenv_main(void) { - char **env; + char **env, **var = toys.optargs; char delim = '\n'; - if (toys.optflags) - delim = '\0'; - - if (!toys.optargs[0]) { - for (env = environ; *env; env++) - xprintf("%s%c", *env, delim); - } else { - char **var = toys.optargs; - for (var = toys.optargs; *var; var++) { - int len = strlen(*var); - for (env = environ; *env; env++) { - if (strncmp(*env, *var, len) == 0) - xprintf("%s%c", *env + len + 1, delim); + if (toys.optflags) delim = 0; + + do { + int catch = 0, len = *var ? strlen(*var) : 0; + + for (env = environ; *env; env++) { + char *out = *env; + if (*var) { + if (!strncmp(out, *var, len) && out[len] == '=') + out += len +1; + else continue; } + xprintf("%s%c", out, delim); + catch++; } - } + if (*var && !catch) toys.exitval = 1; + } while (*var && *(++var)); } |