From 405e73ce65cf048c49c6e57a62f60c3f9c83faf8 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 4 Mar 2012 00:50:44 -0600 Subject: [PATCH] Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code. --- toys/printenv.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/toys/printenv.c b/toys/printenv.c index da75acd..305b3e7 100644 --- a/toys/printenv.c +++ b/toys/printenv.c @@ -5,16 +5,17 @@ * Copyright 2012 Georgi Chorbadzhiyski * -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)); } -- 2.7.4