commit 0e2f50988310356b2764b911980476ea514a312f
Author:     Quentin Rameau <[email protected]>
AuthorDate: Sun Oct 4 13:10:28 2015 +0200
Commit:     sin <[email protected]>
CommitDate: Sun Oct 4 16:45:51 2015 +0100

    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.

diff --git a/ls.1 b/ls.1
index 7c57e98..62f1933 100644
--- 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
index 54aa2d8..c998fc7 100644
--- 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);

Reply via email to