[hackers] [sbase] ls: add -n option || Quentin Rameau

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:53:59 +0100 (CET)

commit 7264acf7edaa2e90105b9220f500e3d596b5c552
Author: Quentin Rameau <quinq_AT_quinq.eu.org>
Date: Sun Feb 22 12:55:38 2015 +0100

    ls: add -n option

diff --git a/README b/README
index 534a3c2..a45ffb4 100644
--- a/README
+++ b/README
_AT_@ -41,7 +41,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =* ln yes none
 =* logger yes none
 =* logname yes none
-= ls no (-C), -S, -f, -m, -n, -s, -x
+= ls no (-C), -S, -f, -m, -s, -x
 =* md5sum non-posix none
 =* mkdir yes none
 =* mkfifo yes none
diff --git a/ls.1 b/ls.1
index e3220bc..260afc0 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -6,7 +6,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm
-.Op Fl 1AacdFHhiLlqrtUu
+.Op Fl 1AacdFHhiLlnqrtUu
 .Op Ar file ...
 .Sh DESCRIPTION
 .Nm
_AT_@ -38,6 +38,10 @@ themselves.
 .It Fl l
 List detailed information about each file, including their type, permissions,
 links, owner, group, size, 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
+numeric IDs.
 .It Fl p
 Append a file type indicator to directories.
 .It Fl q
diff --git a/ls.c b/ls.c
index 8195be1..ac38a09 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -34,6 +34,7 @@ static int hflag = 0;
 static int iflag = 0;
 static int Lflag = 0;
 static int lflag = 0;
+static int nflag = 0;
 static int pflag = 0;
 static int qflag = 0;
 static int Rflag = 0;
_AT_@ -140,14 +141,12 @@ output(const struct entry *ent)
         if (ent->mode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
         if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
 
- pw = getpwuid(ent->uid);
- if (pw)
+ if (!nflag && (pw = getpwuid(ent->uid)))
                 snprintf(pwname, LEN(pwname), "%s", pw->pw_name);
         else
                 snprintf(pwname, LEN(pwname), "%d", ent->uid);
 
- gr = getgrgid(ent->gid);
- if (gr)
+ if (!nflag && (gr = getgrgid(ent->gid)))
                 snprintf(grname, LEN(grname), "%s", gr->gr_name);
         else
                 snprintf(grname, LEN(grname), "%d", ent->gid);
_AT_@ -159,6 +158,7 @@ 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)
                 printf("%10s ", humansize((unsigned long)ent->size));
         else
_AT_@ -307,6 +307,10 @@ main(int argc, char *argv[])
         case 'l':
                 lflag = 1;
                 break;
+ case 'n':
+ lflag = 1;
+ nflag = 1;
+ break;
         case 'p':
                 pflag = 1;
                 break;
Received on Tue Mar 24 2015 - 23:53:59 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:10:59 CET