[hackers] [PATCH] Don't add excessive slashes to directories in ls(1) with the R flag

From: Pekka Jylhä-Ollila <pekka.jylha.ollila_AT_gmail.com>
Date: Wed, 17 Feb 2016 22:37:59 +0200

This patch fixes directory names when using the R flag, and the
directory name ends with '/'. Try out "ls -R ./" with the current
version, the output looks like this:

$ ls -R ./
dir1
dir2

.//dir1:
file1

.//dir2:
file2
---
 ls.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/ls.c b/ls.c
index 72d7434..4b51d17 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -276,8 +276,12 @@ lsdir(const char *path, const struct entry *dir)
 		output(&ents[i]);
 
 	if (Rflag) {
-		if (snprintf(prefix, PATH_MAX, "%s%s/", path, dir->name) >=
-		    PATH_MAX)
+		i = strlen(dir->name);
+		if (i && dir->name[i - 1] == '/')
+			i = snprintf(prefix, PATH_MAX, "%s%s", path, dir->name);
+		else
+			i = snprintf(prefix, PATH_MAX, "%s%s/", path, dir->name);
+		if (i >= PATH_MAX)
 			eprintf("path too long: %s%s\n", path, dir->name);
 
 		for (i = 0; i < n; i++) {
-- 
2.1.4
Received on Wed Feb 17 2016 - 21:37:59 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 17 2016 - 21:48:11 CET