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()
This commit is contained in:
Evan Gates 2016-10-05 14:43:30 -07:00 committed by Laslo Hunhold
parent 071dcc4d6b
commit d24ef864cb

57
ls.c
View File

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