From f3a6a64e4a52e75c73f41755b9133f121785f889 Mon Sep 17 00:00:00 2001 From: Sheldon Hearn Date: Mon, 2 Aug 1999 14:55:58 +0000 Subject: [PATCH] Add -n option to print numeric user and group IDs instead of names in a long (-l) listing. MFC-jockies should make sure that bde's concerns regarding the number of digits required to represent a uid_t and the use of snprintf on the associated PR have been addressed before going wild. PR: 12866 Reported by: Philip Kizer Obtained from: NetBSD --- bin/ls/ls.1 | 12 ++++++++++-- bin/ls/ls.c | 24 +++++++++++++++++++----- bin/ls/util.c | 4 ++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 2975f987e633..d5611f776ec3 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -33,7 +33,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)ls.1 8.7 (Berkeley) 7/29/94 -.\" $Id: ls.1,v 1.27 1999/04/27 23:33:52 hoek Exp $ +.\" $Id: ls.1,v 1.28 1999/05/08 10:20:27 kris Exp $ .\" .Dd July 29, 1994 .Dt LS 1 @@ -148,6 +148,11 @@ not blocks. This option overrides the environment variable BLOCKSIZE. (The lowercase letter ``ell.'') List in long format. (See below.) If the output is to a terminal, a total sum for all the file sizes is output on a line before the long listing. +.It Fl n +Display user and group IDs numerically rather than converting to a user +or group name in a long +.Pq Fl l +output. .It Fl o Include the file flags in a long .Pq Fl l @@ -247,7 +252,10 @@ If the modification time of the file is more than 6 months in the past or future, then the year of the last modification is displayed in place of the hour and minute fields. .Pp -If the owner or group names are not a known user or group name +If the owner or group names are not a known user or group name, +or the +.Fl n +option is given, the numeric ID's are displayed. .Pp If the file is a character special or block special file, diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 25c1ed2aada9..8aaba1f18c33 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -45,7 +45,7 @@ static const char copyright[] = static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; #else static const char rcsid[] = - "$Id: ls.c,v 1.23 1998/08/02 22:47:11 hoek Exp $"; + "$Id: ls.c,v 1.24 1999/05/08 10:20:30 kris Exp $"; #endif #endif /* not lint */ @@ -88,6 +88,7 @@ int f_listdot; /* list files beginning with . */ int f_longform; /* long listing format */ int f_nonprint; /* show unprintables as ? */ int f_nosort; /* don't sort output */ +int f_numericonly; /* don't convert uid/gid to name */ int f_octal; /* show unprintables as \xxx */ int f_octal_escape; /* like f_octal but use C escapes if possible */ int f_recursive; /* ls subdirectories also */ @@ -137,7 +138,7 @@ main(argc, argv) f_listdot = 1; fts_options = FTS_PHYSICAL; - while ((ch = getopt(argc, argv, "1ABCFHLPRTWabcdfgikloqrstu")) != -1) { + while ((ch = getopt(argc, argv, "1ABCFHLPRTWabcdfgiklnoqrstu")) != -1) { switch (ch) { /* * The -1, -C and -l options all override each other so shell @@ -209,6 +210,9 @@ main(argc, argv) case 'k': f_kblocks = 1; break; + case 'n': + f_numericonly = 1; + break; case 'o': f_flags = 1; break; @@ -400,7 +404,8 @@ display(p, list) int bcfile, flen, glen, ulen, maxflags, maxgroup, maxuser; char *initmax; int entries, needstats; - char *user, *group, *flags, buf[20]; /* 32 bits == 10 digits */ + char *user, *group, *flags; + char nuser[12], ngroup[12], buf[21]; /* 32 bits == 10 digits */ /* * If list is NULL there are two possibilities: that the parent @@ -512,10 +517,19 @@ display(p, list) btotal += sp->st_blocks; if (f_longform) { - user = user_from_uid(sp->st_uid, 0); + if (f_numericonly) { + (void)snprintf(nuser, sizeof(nuser), + "%u", sp->st_uid); + (void)snprintf(ngroup, sizeof(ngroup), + "%u", sp->st_gid); + user = nuser; + group = ngroup; + } else { + user = user_from_uid(sp->st_uid, 0); + group = group_from_gid(sp->st_gid, 0); + } if ((ulen = strlen(user)) > maxuser) maxuser = ulen; - group = group_from_gid(sp->st_gid, 0); if ((glen = strlen(group)) > maxgroup) maxgroup = glen; if (f_flags) { diff --git a/bin/ls/util.c b/bin/ls/util.c index d4b55e797660..9d5ba39bcec9 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94"; #else static const char rcsid[] = - "$Id: util.c,v 1.17 1998/04/25 00:12:32 ache Exp $"; + "$Id: util.c,v 1.18 1998/10/13 12:19:31 des Exp $"; #endif #endif /* not lint */ @@ -158,7 +158,7 @@ prn_octal(s) void usage() { - (void)fprintf(stderr, "usage: ls [-ACFHLPRTWacdfgikloqrstu1]" + (void)fprintf(stderr, "usage: ls [-ACFHLPRTWacdfgiklnoqrstu1]" " [file ...]\n"); exit(1); }