From: Martin Pool Date: Mon, 25 Mar 2002 03:51:17 +0000 (+0000) Subject: Add a test case for trim_trailing_slashes, and make it handle other cases. X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/commitdiff_plain/c127e8aaec2b0ea408a3cd3a36fd910520249332 Add a test case for trim_trailing_slashes, and make it handle other cases. --- diff --git a/Makefile.in b/Makefile.in index 7d2763c1..2c563958 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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* diff --git a/syscall.c b/syscall.c index b4b581b9..b198dbf4 100644 --- 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. * */ - 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 index 00000000..cf118407 --- /dev/null +++ b/testsuite/trimslash.test @@ -0,0 +1,29 @@ +#! /bin/sh + +# Copyright (C) 2002 by Martin Pool + +# 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" - <