ls: Simplify some logic

- After first iteration, set first to 0 instead of !first.
- If Hflag || Lflag, then mkent used stat instead of lstat, so the
  entity cannot be a symlink.
- Print path prefix along with directory name.
- In the 'if (Rflag)' block, just use 1 instead of Rflag.
This commit is contained in:
Michael Forney 2016-05-14 18:56:53 -07:00 committed by sin
parent ea03da0bda
commit d827fcace2

15
ls.c
View File

@ -279,7 +279,7 @@ lsdir(const char *path, const struct entry *dir)
qsort(ents, n, sizeof(*ents), entcmp); qsort(ents, n, sizeof(*ents), entcmp);
if (path[0] || showdirs) if (path[0] || showdirs)
printf("%s:\n", dir->name); printf("%s%s:\n", path, dir->name);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
output(&ents[i]); output(&ents[i]);
@ -296,7 +296,7 @@ lsdir(const char *path, const struct entry *dir)
if (S_ISLNK(ent->mode) && S_ISDIR(ent->tmode) && !Lflag) if (S_ISLNK(ent->mode) && S_ISDIR(ent->tmode) && !Lflag)
continue; continue;
ls(prefix, ent, Rflag); ls(prefix, ent, 1);
} }
} }
@ -346,11 +346,10 @@ ls(const char *path, const struct entry *ent, int listdir)
eprintf("getcwd:"); eprintf("getcwd:");
if (first) if (first)
first = !first; first = 0;
else else
putchar('\n'); putchar('\n');
fputs(path, stdout);
lsdir(path, ent); lsdir(path, ent);
tree[treeind].ino = 0; tree[treeind].ino = 0;
@ -451,8 +450,8 @@ main(int argc, char *argv[])
case 1: case 1:
mkent(&ent, argv[0], 1, Hflag || Lflag); mkent(&ent, argv[0], 1, Hflag || Lflag);
ls("", &ent, (!dflag && S_ISDIR(ent.mode)) || ls("", &ent, (!dflag && S_ISDIR(ent.mode)) ||
((S_ISLNK(ent.mode) && S_ISDIR(ent.tmode)) && (S_ISLNK(ent.mode) && S_ISDIR(ent.tmode) &&
((Hflag || Lflag) || !(dflag || Fflag || lflag)))); !(dflag || Fflag || lflag)));
break; break;
default: default:
@ -460,8 +459,8 @@ main(int argc, char *argv[])
mkent(&ent, argv[i], 1, Hflag || Lflag); mkent(&ent, argv[i], 1, Hflag || Lflag);
if ((!dflag && S_ISDIR(ent.mode)) || if ((!dflag && S_ISDIR(ent.mode)) ||
((S_ISLNK(ent.mode) && S_ISDIR(ent.tmode)) && (S_ISLNK(ent.mode) && S_ISDIR(ent.tmode) &&
((Hflag || Lflag) || !(dflag || Fflag || lflag)))) { !(dflag || Fflag || lflag))) {
dents = ereallocarray(dents, ++ds, sizeof(*dents)); dents = ereallocarray(dents, ++ds, sizeof(*dents));
memcpy(&dents[ds - 1], &ent, sizeof(ent)); memcpy(&dents[ds - 1], &ent, sizeof(ent));
} else { } else {