sbase

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

commit 0e2f50988310356b2764b911980476ea514a312f
parent f14a8968913a4a86064b1688790880bb94fe5a53
Author: Quentin Rameau <quinq@fifth.space>
Date:   Sun,  4 Oct 2015 13:10:28 +0200

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.

Diffstat:
Mls.1 | 6++++--
Mls.c | 7++++++-
2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/ls.1 b/ls.1 @@ -55,10 +55,12 @@ List information about the targets of symbolic links instead of the links themselves. .It Fl l 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 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. .It Fl p Append a file type indicator to directories. diff --git a/ls.c b/ls.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <sys/stat.h> +#include <sys/types.h> #include <dirent.h> #include <grp.h> @@ -22,6 +23,7 @@ struct entry { off_t size; time_t t; dev_t dev; + dev_t rdev; ino_t ino, tino; }; @@ -75,6 +77,7 @@ mkent(struct entry *ent, char *path, int dostat, int follow) else ent->t = st.st_mtime; ent->dev = st.st_dev; + ent->rdev = st.st_rdev; ent->ino = st.st_ino; if (S_ISLNK(ent->mode)) { if (stat(path, &st) == 0) { @@ -192,7 +195,9 @@ output(const struct entry *ent) strftime(buf, sizeof(buf), fmt, localtime(&ent->t)); 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)); else printf("%10lu ", (unsigned long)ent->size);