From 749e113b3c549b84b5e38d1065e78e93083dd4bf Mon Sep 17 00:00:00 2001 From: Quentin Rameau Date: Sun, 19 Apr 2015 00:40:02 +0200 Subject: [PATCH 3/4] ls: add -g and -o options --- ls.1 | 12 ++++++++++-- ls.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/ls.1 b/ls.1 index 7c57e98..ea4e9b4 100644 --- a/ls.1 +++ b/ls.1 @@ -1,4 +1,4 @@ -.Dd March 27, 2015 +.Dd Avril 19, 2015 .Dt LS 1 .Os sbase .Sh NAME @@ -7,7 +7,7 @@ .Sh SYNOPSIS .Nm .Op Fl iqr -.Op Fl ln +.Op Fl glno .Op Fl A | a .Op Fl 1 .Op Fl h | F | p @@ -43,6 +43,10 @@ and disables .Fl S and .Fl t . +.It Fl g +Turn on +.Fl l +but do not display owner field. .It Fl H List information about the targets of symbolic links specified on the command line instead of the links themselves. @@ -60,6 +64,10 @@ links, owner, group, size, and last file status/modification time. 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 o +Turn on +.Fl l +but do not display group field. .It Fl p Append a file type indicator to directories. .It Fl q diff --git a/ls.c b/ls.c index 87392c3..ae9f593 100644 --- a/ls.c +++ b/ls.c @@ -39,12 +39,14 @@ static int cflag = 0; static int dflag = 0; static int Fflag = 0; static int fflag = 0; +static int gflag = 0; static int Hflag = 0; static int hflag = 0; static int iflag = 0; static int Lflag = 0; static int lflag = 0; static int nflag = 0; +static int oflag = 0; static int pflag = 0; static int qflag = 0; static int Rflag = 0; @@ -177,15 +179,19 @@ 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'; - if (!nflag && (pw = getpwuid(ent->uid))) - snprintf(pwname, LEN(pwname), "%s", pw->pw_name); - else - snprintf(pwname, LEN(pwname), "%d", ent->uid); + if (!gflag) { + if (!nflag && (pw = getpwuid(ent->uid))) + snprintf(pwname, LEN(pwname), "%s", pw->pw_name); + else + snprintf(pwname, LEN(pwname), "%d", ent->uid); + } - if (!nflag && (gr = getgrgid(ent->gid))) - snprintf(grname, LEN(grname), "%s", gr->gr_name); - else - snprintf(grname, LEN(grname), "%d", ent->gid); + if (!oflag) { + if (!nflag && (gr = getgrgid(ent->gid))) + snprintf(grname, LEN(grname), "%s", gr->gr_name); + else + snprintf(grname, LEN(grname), "%d", ent->gid); + } if (time(NULL) > ent->t + (180 * 24 * 60 * 60)) /* 6 months ago? */ fmt = "%b %d %Y"; @@ -193,7 +199,11 @@ output(const struct entry *ent) fmt = "%b %d %H:%M"; strftime(buf, sizeof(buf), fmt, localtime(&ent->t)); - printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname); + printf("%s %4ld ", mode, (long)ent->nlink); + if (!gflag) + printf("%-8.8s ", pwname); + if (!oflag) + printf("%-8.8s ", grname); if (hflag) printf("%10s ", humansize((unsigned long)ent->size)); @@ -339,7 +349,7 @@ ls(char *path, struct entry *ent, int listdir) static void usage(void) { - eprintf("usage: %s [-1AacdFfHhiLlpqRrStUu] [file ...]\n", argv0); + eprintf("usage: %s [-1AacdFfgHhiLlopqRrStUu] [file ...]\n", argv0); } int @@ -376,6 +386,10 @@ main(int argc, char *argv[]) Sflag = 0; tflag = 0; break; + case 'g': + gflag = 1; + lflag = 1; + break; case 'H': Hflag = 1; break; @@ -395,6 +409,10 @@ main(int argc, char *argv[]) nflag = 1; lflag = 1; break; + case 'o': + lflag = 1; + oflag = 1; + break; case 'p': pflag = 1; break; -- 2.3.5