ls: add -n option

This commit is contained in:
Quentin Rameau 2015-02-22 12:55:38 +01:00 committed by sin
parent 9e37634571
commit 7264acf7ed
3 changed files with 14 additions and 6 deletions

2
README
View File

@ -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

6
ls.1
View File

@ -6,7 +6,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl 1AacdFHhiLlqrtUu
.Op Fl 1AacdFHhiLlnqrtUu
.Op Ar file ...
.Sh DESCRIPTION
.Nm
@ -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

12
ls.c
View File

@ -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;
@ -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);
@ -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
@ -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;