ls: handle character/block files in long format

Although major() and minor() are not POSIX, we don't want to have macros
there so we rely on their implementation by the target system.
This commit is contained in:
Quentin Rameau 2015-10-04 13:10:28 +02:00 committed by sin
parent f14a896891
commit 0e2f509883
2 changed files with 10 additions and 3 deletions

6
ls.1
View File

@ -55,10 +55,12 @@ List information about the targets of symbolic links instead of the links
themselves. themselves.
.It Fl l .It Fl l
List detailed information about each file, including their type, permissions, List detailed information about each file, including their type, permissions,
links, owner, group, size, and last file status/modification time. links, owner, group, size or major and minor numbers if the file is a
character/block device, and last file status/modification time.
.It Fl n .It Fl n
List detailed information about each file, including their type, permissions, List detailed information about each file, including their type, permissions,
links, owner, group, size, and last file status/modification time, but with links, owner, group, size or major and minor numbers if the file is a
character/block device, and last file status/modification time, but with
numeric IDs. numeric IDs.
.It Fl p .It Fl p
Append a file type indicator to directories. Append a file type indicator to directories.

7
ls.c
View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <grp.h> #include <grp.h>
@ -22,6 +23,7 @@ struct entry {
off_t size; off_t size;
time_t t; time_t t;
dev_t dev; dev_t dev;
dev_t rdev;
ino_t ino, tino; ino_t ino, tino;
}; };
@ -75,6 +77,7 @@ mkent(struct entry *ent, char *path, int dostat, int follow)
else else
ent->t = st.st_mtime; ent->t = st.st_mtime;
ent->dev = st.st_dev; ent->dev = st.st_dev;
ent->rdev = st.st_rdev;
ent->ino = st.st_ino; ent->ino = st.st_ino;
if (S_ISLNK(ent->mode)) { if (S_ISLNK(ent->mode)) {
if (stat(path, &st) == 0) { if (stat(path, &st) == 0) {
@ -192,7 +195,9 @@ output(const struct entry *ent)
strftime(buf, sizeof(buf), fmt, localtime(&ent->t)); strftime(buf, sizeof(buf), fmt, localtime(&ent->t));
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname); printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
if (hflag) if (S_ISBLK(ent->mode) || S_ISCHR(ent->mode))
printf("%4u, %4u ", major(ent->rdev), minor(ent->rdev));
else if (hflag)
printf("%10s ", humansize(ent->size)); printf("%10s ", humansize(ent->size));
else else
printf("%10lu ", (unsigned long)ent->size); printf("%10lu ", (unsigned long)ent->size);