changeset 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 c6b3bd689d0a
files toys/printenv.c
diffstat 1 files changed, 19 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/toys/printenv.c	Sat Mar 03 23:55:27 2012 -0600
+++ b/toys/printenv.c	Sun Mar 04 00:50:44 2012 -0600
@@ -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 @@
 
 void printenv_main(void)
 {
-	char **env;
+	char **env, **var = toys.optargs;
 	char delim = '\n';
 
-	if (toys.optflags)
-		delim = '\0';
+	if (toys.optflags) delim = 0;
+
+	do {
+		int catch = 0, len = *var ? strlen(*var) : 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);
+		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));
 }