[PATCH 1/5] ls: fix -q

From: Quentin Rameau <quinq_AT_quinq.eu.org>
Date: Fri, 12 Jun 2015 15:09:32 +0200

Move qflag handling to output() instead of modifying the actual entity
name which broke recursivity.
---
 ls.c | 50 +++++++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 21 deletions(-)
diff --git a/ls.c b/ls.c
index cdfce4d..3034e6b 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -102,15 +102,34 @@ output(const struct entry *ent)
 	struct group *gr;
 	struct passwd *pw;
 	ssize_t len;
-	char buf[BUFSIZ], *fmt,
+	size_t l;
+	char *name, *c, *u, *fmt, buf[BUFSIZ],
 	     pwname[_SC_LOGIN_NAME_MAX], grname[_SC_LOGIN_NAME_MAX],
 	     mode[] = "----------";
+	Rune r;
+
+	if (qflag) {
+		name = emalloc(strlen(ent->name) + 1);
+ 
+		for (c = name, u = ent->name; *u; u += l) {
+			l = chartorune(&r, u);
+			if (isprintrune(r)) {
+				memcpy(c, u, l);
+				c += l;
+			} else {
+				*c++ = '?';
+			}
+		}
+		*c = '\0';
+	} else {
+		name = ent->name;
+	}
 
 	if (iflag)
 		printf("%lu ", (unsigned long)ent->ino);
 	if (!lflag) {
-		printf("%s%s\n", ent->name, indicator(ent->mode));
-		return;
+		printf("%s%s\n", name, indicator(ent->mode));
+		goto cleanup;
 	}
 	if (S_ISREG(ent->mode))
 		mode[0] = '-';
_AT_@ -173,6 +192,10 @@ output(const struct entry *ent)
 		printf(" -> %s%s", buf, indicator(ent->tmode));
 	}
 	putchar('\n');
+
+cleanup:
+	if (qflag)
+		free(name);
 }
 
 static int
_AT_@ -193,11 +216,10 @@ static void
 lsdir(const char *path)
 {
 	DIR *dp;
-	Rune r;
 	struct entry ent, *ents = NULL;
 	struct dirent *d;
-	size_t i, n = 0, len;
-	char cwd[PATH_MAX], *p, *q, *name;
+	size_t i, n = 0;
+	char cwd[PATH_MAX], *name;
 
 	if (!getcwd(cwd, sizeof(cwd)))
 		eprintf("getcwd:");
_AT_@ -224,21 +246,7 @@ lsdir(const char *path)
 			ls(&ent, Rflag);
 		} else {
 			ents = ereallocarray(ents, ++n, sizeof(*ents));
-			name = p = estrdup(d->d_name);
-			if (qflag) {
-				q = d->d_name;
-				while (*q) {
-					len = chartorune(&r, q);
-					if (isprintrune(r)) {
-						memcpy(p, q, len);
-						p += len, q += len;
-					} else {
-						*p++ = '?';
-						q += len;
-					}
-				}
-				*p = '\0';
-			}
+			name = estrdup(d->d_name);
 			mkent(&ents[n - 1], name, tflag || Sflag || Fflag || iflag || lflag || pflag || Rflag, Lflag);
 		}
 	}
-- 
2.4.3
--FcDMekJBvgbQ11GR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="0002-ls-list-operands-in-correct-order.patch"
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Thu Jun 18 2015 - 17:34:31 CEST