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 =* ln yes none
=* logger yes none =* logger yes none
=* logname 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 =* md5sum non-posix none
=* mkdir yes none =* mkdir yes none
=* mkfifo yes none =* mkfifo yes none

6
ls.1
View File

@ -6,7 +6,7 @@
.Nd list directory contents .Nd list directory contents
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl 1AacdFHhiLlqrtUu .Op Fl 1AacdFHhiLlnqrtUu
.Op Ar file ... .Op Ar file ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
@ -38,6 +38,10 @@ themselves.
.It Fl l .It Fl l
List detailed information about each file, including their type, permissions, List detailed information about each file, including their type, permissions,
links, owner, group, size, and last file status/modification time. 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 .It Fl p
Append a file type indicator to directories. Append a file type indicator to directories.
.It Fl q .It Fl q

12
ls.c
View File

@ -34,6 +34,7 @@ static int hflag = 0;
static int iflag = 0; static int iflag = 0;
static int Lflag = 0; static int Lflag = 0;
static int lflag = 0; static int lflag = 0;
static int nflag = 0;
static int pflag = 0; static int pflag = 0;
static int qflag = 0; static int qflag = 0;
static int Rflag = 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_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T'; if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
pw = getpwuid(ent->uid); if (!nflag && (pw = getpwuid(ent->uid)))
if (pw)
snprintf(pwname, LEN(pwname), "%s", pw->pw_name); snprintf(pwname, LEN(pwname), "%s", pw->pw_name);
else else
snprintf(pwname, LEN(pwname), "%d", ent->uid); snprintf(pwname, LEN(pwname), "%d", ent->uid);
gr = getgrgid(ent->gid); if (!nflag && (gr = getgrgid(ent->gid)))
if (gr)
snprintf(grname, LEN(grname), "%s", gr->gr_name); snprintf(grname, LEN(grname), "%s", gr->gr_name);
else else
snprintf(grname, LEN(grname), "%d", ent->gid); snprintf(grname, LEN(grname), "%d", ent->gid);
@ -159,6 +158,7 @@ output(const struct entry *ent)
strftime(buf, sizeof(buf), fmt, localtime(&ent->t)); strftime(buf, sizeof(buf), fmt, localtime(&ent->t));
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname); printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
if (hflag) if (hflag)
printf("%10s ", humansize((unsigned long)ent->size)); printf("%10s ", humansize((unsigned long)ent->size));
else else
@ -307,6 +307,10 @@ main(int argc, char *argv[])
case 'l': case 'l':
lflag = 1; lflag = 1;
break; break;
case 'n':
lflag = 1;
nflag = 1;
break;
case 'p': case 'p':
pflag = 1; pflag = 1;
break; break;