changeset 409:55598a9b8f21

'env' and 'basename' refactored
author Tryn Mirell <tryn@mirell.org>
date Fri, 20 Jan 2012 00:02:37 -0600
parents 8506c538f26a
children 3d87f15f4c60
files toys/basename.c toys/env.c
diffstat 2 files changed, 24 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/toys/basename.c	Wed Jan 18 20:32:52 2012 -0600
+++ b/toys/basename.c	Fri Jan 20 00:02:37 2012 -0600
@@ -7,7 +7,7 @@
  * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html
 
 
-USE_BASENAME(NEWTOY(basename, NULL, TOYFLAG_USR|TOYFLAG_BIN))
+USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))
 
 config BASENAME
 	bool "basename"
@@ -22,40 +22,23 @@
 
 void basename_main(void)
 {
-    char *arg, *suffix, *base;
-    int arglen;
-
-    arg = toys.optargs[0];
-    suffix = toys.optargs[1];
+    char *arg = toys.optargs[0], *suffix = toys.optargs[1], *base;
 
-    // return null string if nothing provided
-    if (!arg) return; 
-
-    arglen = strlen(arg);
- 
-    // handle the case where we only have single slash
-    if (arglen == 1 && arg[0] == '/') {
-        puts("/");
-        return;
+    while ((base = strrchr(arg, '/'))) {
+        if (base == arg) break;
+        if (!base[1]) *base = 0;
+        else {
+            base++;
+            break;
+        }
     }
 
-    // remove trailing slash
-    if (arg[arglen - 1] == '/') {
-        arg[arglen - 1] = 0;
-    }
-
-    // get everything past the last /
-    base = strrchr(arg, '/');
-
     if (!base) base = arg;
-    else base++;
-   
-    // handle the case where we have all slashes
-    if (base[0] == 0) base = "/";  
     
     // chop off the suffix if provided
     if (suffix) {
-        strstr(base, suffix)[0] = 0;
+        char *s = strstr(base, suffix);
+        if (s && s != base) *s = 0;
     }
 
     puts(base);
--- a/toys/env.c	Wed Jan 18 20:32:52 2012 -0600
+++ b/toys/env.c	Fri Jan 20 00:02:37 2012 -0600
@@ -1,13 +1,14 @@
 /* vi: set sw=4 ts=4:
  * env.c
 
-USE_ENV(NEWTOY(env, "^?i", TOYFLAG_USR|TOYFLAG_BIN))
+USE_ENV(NEWTOY(env, "^i", TOYFLAG_USR|TOYFLAG_BIN))
 
 config ENV
 	bool "env"
 	default n
 	help
-        
+        usage: env [-i] [FOO=BAR...] [command [option...]]
+
         Set the environment for command invocation
 */
 
@@ -19,20 +20,19 @@
 {
     char **ev;
     char **command = NULL;
-
+    char *del = "=";
+    
     if (toys.optflags & 1) clearenv();
     
     for (ev = toys.optargs; *ev != NULL; ev++) {
-        char *env = NULL, *val = NULL;
-        char *del = "=";
+        char *env, *val = NULL;
         
         env = strtok(*ev, del);
         
-        if (env != NULL) val = strtok(NULL, del);
+        if (env) val = strtok(NULL, del);
         
-        if (val != NULL) {
-            setenv(env, val, 1);
-        } else {
+        if (val) setenv(env, val, 1);
+        else {
             command = ev;
             break;
         }
@@ -40,13 +40,8 @@
     
     if (!command) {
         char **ep;
-        if (environ) {
-            for (ep = environ; *ep != NULL; ep++)
-                xputs(*ep);
-            return;
-        }
-    } else {
-        execvp(*command, command);
-    }
-
+        for (ep = environ; *ep; ep++)
+            xputs(*ep);
+        return;
+    } else execvp(*command, command);
 }