changeset 782:3d7526f6115b

Use basename() where appropriate.
author Rob Landley <rob@landley.net>
date Sat, 05 Jan 2013 00:44:24 -0600
parents b300eb824c70
children 7bbb49149bb6
files main.c scripts/test/chgrp.test scripts/test/rmdir.test scripts/test/testing.sh toys/other/rmmod.c toys/posix/ln.c toys/posix/rmdir.c
diffstat 7 files changed, 19 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Fri Jan 04 21:10:49 2013 -0600
+++ b/main.c	Sat Jan 05 00:44:24 2013 -0600
@@ -146,16 +146,8 @@
 {
   if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "");
 
-  // Artificial scope to eat less stack for things we call
-  {
-    char *name;
-
-    // Trim path off of command name
-    name = strrchr(argv[0], '/');
-    if (!name) name=argv[0];
-    else name++;
-    argv[0] = name;
-  }
+  // Trim path off of command name
+  *argv = basename(*argv);
 
   // Call the multiplexer, adjusting this argv[] to be its' argv[1].
   // (It will adjust it back before calling toy_exec().)
--- a/scripts/test/chgrp.test	Fri Jan 04 21:10:49 2013 -0600
+++ b/scripts/test/chgrp.test	Sat Jan 05 00:44:24 2013 -0600
@@ -2,8 +2,6 @@
 
 [ -f testing.sh ] && . testing.sh
 
-VERBOSE=1
-
 if [ "$(id -u)" -ne 0 ]
 then
   echo "SKIPPED: chgrp (not root)"
--- a/scripts/test/rmdir.test	Fri Jan 04 21:10:49 2013 -0600
+++ b/scripts/test/rmdir.test	Sat Jan 05 00:44:24 2013 -0600
@@ -45,6 +45,10 @@
 testing "rmdir -p one/two/three" \
 	"rmdir -p one/two/three && [ ! -e one ] && echo yes" "yes\n" "" ""
 
+mkdir -p one/two/three
+testing "rmdir -p one/two/three/" \
+	"rmdir -p one/two/three/ && [ ! -e one ] && echo yes" "yes\n" "" ""
+
 #mkdir -p one/two/three
 #chmod 000 one/two/three one/two one
 #testing "rmdir -p one/two/three" \
--- a/scripts/test/testing.sh	Fri Jan 04 21:10:49 2013 -0600
+++ b/scripts/test/testing.sh	Sat Jan 05 00:44:24 2013 -0600
@@ -74,7 +74,7 @@
   echo -ne "$5" | eval "$2" > actual
   RETVAL=$?
 
-  cmp expected actual > /dev/null
+  cmp expected actual > /dev/null 2>&1
   if [ $? -ne 0 ]
   then
     FAILCOUNT=$[$FAILCOUNT+1]
--- a/toys/other/rmmod.c	Fri Jan 04 21:10:49 2013 -0600
+++ b/toys/other/rmmod.c	Sat Jan 05 00:44:24 2013 -0600
@@ -29,9 +29,7 @@
   int len;
 
   // Basename
-  mod_name = strrchr(toys.optargs[0],'/');
-  if (mod_name) mod_name++;
-  else mod_name = toys.optargs[0];
+  mod_name = basename(*toys.optargs);
 
   // Remove .ko if present
   len = strlen(mod_name);
--- a/toys/posix/ln.c	Fri Jan 04 21:10:49 2013 -0600
+++ b/toys/posix/ln.c	Sat Jan 05 00:44:24 2013 -0600
@@ -47,11 +47,8 @@
     int rc;
     char *try = toys.optargs[i];
 
-    if (S_ISDIR(buf.st_mode)) {
-      new = strrchr(try, '/');
-      if (!new) new = try;
-      new = xmsprintf("%s/%s", dest, new);
-    } else new = dest;
+    if (S_ISDIR(buf.st_mode)) new = xmsprintf("%s/%s", dest, basename(try));
+    else new = dest;
     /* Silently unlink the existing target. If it doesn't exist,
      * then we just move on */
     if (toys.optflags & FLAG_f) unlink(new);
--- a/toys/posix/rmdir.c	Fri Jan 04 21:10:49 2013 -0600
+++ b/toys/posix/rmdir.c	Sat Jan 05 00:44:24 2013 -0600
@@ -20,16 +20,21 @@
 
 static void do_rmdir(char *name)
 {
+  char *temp;
+
   for (;;) {
-    char *temp;
-
     if (rmdir(name)) {
       perror_msg("%s",name);
       return;
     }
+
+    // Each -p cycle back up one slash, ignoring trailing and repeated /.
+
     if (!toys.optflags) return;
-    if (!(temp=strrchr(name,'/'))) return;
-    *temp=0;
+    do {
+      if (!(temp = strrchr(name, '/'))) return;
+      *temp = 0;
+    } while (!temp[1]);
   }
 }