aboutsummaryrefslogtreecommitdiff
path: root/toys/printenv.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-03-04 00:50:44 -0600
committerRob Landley <rob@landley.net>2012-03-04 00:50:44 -0600
commit405e73ce65cf048c49c6e57a62f60c3f9c83faf8 (patch)
treea448e66a00fdf700ea992191dd8200caf475aeb2 /toys/printenv.c
parent2790105914ea20aaa26d71899be1f12422270349 (diff)
downloadtoybox-405e73ce65cf048c49c6e57a62f60c3f9c83faf8.tar.gz
Add longopt, refactor so only one instance of each loop, requre = as part of match, update exit code.
Diffstat (limited to 'toys/printenv.c')
-rw-r--r--toys/printenv.c38
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));
}