[hackers] [sbase] ls: handle character/block files in long format || Quentin Rameau

From: <git_AT_suckless.org>
Date: Sun, 4 Oct 2015 17:45:55 +0200 (CEST)

commit 0e2f50988310356b2764b911980476ea514a312f
Author: Quentin Rameau <quinq_AT_fifth.space>
AuthorDate: Sun Oct 4 13:10:28 2015 +0200
Commit: sin <sin_AT_2f30.org>
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
_AT_@ -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
_AT_@ -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>
_AT_@ -22,6 +23,7 @@ struct entry {
         off_t size;
         time_t t;
         dev_t dev;
+ dev_t rdev;
         ino_t ino, tino;
 };
 
_AT_@ -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) {
_AT_@ -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);
Received on Sun Oct 04 2015 - 17:45:55 CEST

This archive was generated by hypermail 2.3.0 : Sun Oct 04 2015 - 17:48:11 CEST