sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit b88ed2ab2de089ad7902b1bd360ce5d5696fcab7
parent 438d2542e4bb731c65d10f908cd0519cf502750c
Author: Quentin Rameau <quinq@quinq.eu.org>
Date:   Fri, 12 Jun 2015 15:09:32 +0200

ls: fix -q

Move qflag handling to output() instead of modifying the actual entity
name which broke recursivity.

Diffstat:
Mls.c | 50+++++++++++++++++++++++++++++---------------------
1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/ls.c b/ls.c @@ -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] = '-'; @@ -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 @@ -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:"); @@ -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); } }