Add a test case for trim_trailing_slashes, and make it handle other cases.
authorMartin Pool <mbp@samba.org>
Mon, 25 Mar 2002 03:51:17 +0000 (03:51 +0000)
committerMartin Pool <mbp@samba.org>
Mon, 25 Mar 2002 03:51:17 +0000 (03:51 +0000)
Makefile.in
syscall.c
testsuite/trimslash.test [new file with mode: 0644]
trimslash.c [new file with mode: 0644]

index 7d2763c..2c56395 100644 (file)
@@ -40,7 +40,7 @@ OBJS=$(OBJS1) $(OBJS2) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
 TLS_OBJ = tls.o syscall.o lib/permstring.o 
 
 # Programs we must have to run the test cases
-CHECK_PROGS = rsync tls getgroups
+CHECK_PROGS = rsync tls getgroups trimslash
 
 # note that the -I. is needed to handle config.h when using VPATH
 .c.o:
@@ -75,6 +75,10 @@ tls: $(TLS_OBJ)
 getgroups: getgroups.o
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS)
 
+TRIMSLASH_OBJ = trimslash.o syscall.o
+trimslash: $(TRIMSLASH_OBJ)
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(TRIMSLASH_OBJ) $(LIBS)
+
 Makefile: Makefile.in configure config.status
        echo "WARNING: You need to run ./config.status --recheck"
 
@@ -92,7 +96,7 @@ proto:
        cat $(srcdir)/*.c $(srcdir)/lib/compat.c | awk -f $(srcdir)/mkproto.awk > $(srcdir)/proto.h
 
 clean: cleantests
-       rm -f *~ $(OBJS) rsync $(TLS_OBJ) tls getgroups
+       rm -f *~ $(OBJS) rsync $(TLS_OBJ) $(CHECK_PROGS)
 
 cleantests:
        rm -rf ./testtmp*
index b4b581b..b198dbf 100644 (file)
--- a/syscall.c
+++ b/syscall.c
@@ -113,15 +113,19 @@ int do_rename(char *fname1, char *fname2)
 
 void trim_trailing_slashes(char *name)
 {
-       char *p;
+       int l;
        /* Some BSD systems cannot make a directory if the name
         * contains a trailing slash.
         * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
-       if (!*name)
-               return;         /* empty string */
-       p = strchr(name, '\0') - 1;
-       while (p == '/') {
-               p-- = '\0';
+       
+       /* Don't change empty string; and also we can't improve on
+        * "/" */
+       
+       l = strlen(name);
+       while (l > 1) {
+               if (name[--l] != '/')
+                       break;
+               name[l] = '\0';
        }
 }
 
diff --git a/testsuite/trimslash.test b/testsuite/trimslash.test
new file mode 100644 (file)
index 0000000..cf11840
--- /dev/null
@@ -0,0 +1,29 @@
+#! /bin/sh
+
+# Copyright (C) 2002 by Martin Pool <mbp@samba.org>
+
+# This program is distributable under the terms of the GNU GPL (see
+# COPYING).
+
+# Test tiny function to trim trailing slashes.
+
+. $srcdir/testsuite/rsync.fns
+
+set -x
+
+"$TOOLDIR/trimslash" "/usr/local/bin" "/usr/local/bin/" "/usr/local/bin///" \
+       "//a//" "////" \
+        "/Users/Wierd Macintosh Name/// Ooh, translucent plastic/" \
+       > "$scratchdir/slash.out"
+diff -c "$scratchdir/slash.out" - <<EOF
+/usr/local/bin
+/usr/local/bin
+/usr/local/bin
+//a
+/
+/Users/Wierd Macintosh Name/// Ooh, translucent plastic
+EOF
+
+exit 0
+# last [] may have failed but if we get here then we've won
+
diff --git a/trimslash.c b/trimslash.c
new file mode 100644 (file)
index 0000000..7188274
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2002 by Martin Pool
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "rsync.h"
+
+/* These are to make syscall.o shut up. */
+int dry_run = 0;
+int read_only = 1;
+int list_only = 0;
+
+/**
+ * @file trimslash.c
+ *
+ * Test harness; not linked into release.
+ **/
+int main(int argc, char **argv)
+{
+       int i;
+       
+       if (argc <= 1) {
+               fprintf(stderr, "trimslash: needs at least one argument\n");
+               return 1;
+       }
+
+       for (i = 1; i < argc; i++) {
+               trim_trailing_slashes(argv[i]); /* modify in place */
+               printf("%s\n", argv[i]);
+       }
+       return 0;
+}