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

45
ls.c
View File

@ -113,23 +113,14 @@ indicator(mode_t mode)
return ""; return "";
} }
static void static char *
output(const struct entry *ent) makeprint(char *name)
{ {
struct group *gr; char *c, *u, *print = emalloc(strlen(name) + 1);
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; Rune r;
size_t l;
if (qflag) { for (c = print, u = name; *u; u += l) {
name = emalloc(strlen(ent->name) + 1);
for (c = name, u = ent->name; *u; u += l) {
l = chartorune(&r, u); l = chartorune(&r, u);
if (isprintrune(r)) { if (isprintrune(r)) {
memcpy(c, u, l); memcpy(c, u, l);
@ -139,10 +130,21 @@ output(const struct entry *ent)
} }
} }
*c = '\0'; *c = '\0';
} else {
name = ent->name; return print;
} }
static void
output(const struct entry *ent)
{
struct group *gr;
struct passwd *pw;
struct tm *tm;
ssize_t len;
char *fmt, buf[BUFSIZ], pwname[_SC_LOGIN_NAME_MAX],
grname[_SC_LOGIN_NAME_MAX], mode[] = "----------",
*name = qflag ? makeprint(ent->name) : ent->name;
if (iflag) if (iflag)
printf("%lu ", (unsigned long)ent->ino); printf("%lu ", (unsigned long)ent->ino);
if (!lflag) { if (!lflag) {
@ -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]);