sbase

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

commit d24ef864cbf9be010294418acb51c0d424c7bc99
parent 071dcc4d6b0b901591a8b591c746390f2c3e0f9c
Author: Evan Gates <evan.gates@gmail.com>
Date:   Wed,  5 Oct 2016 14:43:30 -0700

ls: respect -q when printing directory names with -R

break out the non printable character to ? code into a makeprint()
function so it can be used both in output() and lsdir()

Diffstat:
Mls.c | 57+++++++++++++++++++++++++++++++--------------------------
1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/ls.c b/ls.c @@ -113,6 +113,27 @@ indicator(mode_t mode) return ""; } +static char * +makeprint(char *name) +{ + char *c, *u, *print = emalloc(strlen(name) + 1); + Rune r; + size_t l; + + for (c = print, u = name; *u; u += l) { + l = chartorune(&r, u); + if (isprintrune(r)) { + memcpy(c, u, l); + c += l; + } else { + *c++ = '?'; + } + } + *c = '\0'; + + return print; +} + static void output(const struct entry *ent) { @@ -120,28 +141,9 @@ output(const struct entry *ent) struct passwd *pw; struct tm *tm; ssize_t len; - 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; - } + char *fmt, buf[BUFSIZ], pwname[_SC_LOGIN_NAME_MAX], + grname[_SC_LOGIN_NAME_MAX], mode[] = "----------", + *name = qflag ? makeprint(ent->name) : ent->name; if (iflag) printf("%lu ", (unsigned long)ent->ino); @@ -250,12 +252,11 @@ lsdir(const char *path, const struct entry *dir) struct entry *ent, *ents = NULL; struct dirent *d; size_t i, n = 0; - char prefix[PATH_MAX]; + char prefix[PATH_MAX], *name; if (!(dp = opendir(dir->name))) { ret = 1; weprintf("opendir %s%s:", path, dir->name); - return; } if (chdir(dir->name) < 0) eprintf("chdir %s:", dir->name); @@ -278,8 +279,12 @@ lsdir(const char *path, const struct entry *dir) if (!Uflag) qsort(ents, n, sizeof(*ents), entcmp); - if (path[0] || showdirs) - printf("%s%s:\n", path, dir->name); + if (path[0] || showdirs) { + name = qflag ? makeprint(dir->name) : dir->name; + printf("%s%s:\n", path, name); + if (qflag) + free(name); + } for (i = 0; i < n; i++) output(&ents[i]);