Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 29 Jul 1999 19:41:07 +0200
From:      Sheldon Hearn <sheldonh@uunet.co.za>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/12866: [PATCH] RFE for /bin/ls to add a -n option for showing uid/gid numerically 
Message-ID:  <48179.933270067@axl.noc.iafrica.com>
In-Reply-To: Your message of "Thu, 29 Jul 1999 10:15:43 MST." <199907291715.KAA62031@freefall.freebsd.org> 

next in thread | previous in thread | raw e-mail | index | archive | help

Alright, here's a patch for your perusal. It differs from yours in that:

	* Variables have been renamed.
	* Your auxiliary buffers may need to hold as many as 10 digits
	  for uid_t and gid_t, which are currently unsigned 32-bit ints.
	* You _must_ keep usage() in sync with your getopt() rules.
	* I've added a diff for the manpage.

Ciao,
Sheldon.

Index: ls.1
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.1,v
retrieving revision 1.28
diff -u -d -r1.28 ls.1
--- ls.1	1999/05/08 10:20:27	1.28
+++ ls.1	1999/07/29 17:37:16
@@ -148,6 +148,9 @@
 (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 owner UID and GID instead of owner name and group respectively in a
+long output.
 .It Fl o
 Include the file flags in a long
 .Pq Fl l
@@ -248,6 +251,9 @@
 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
+or if the
+.Fl n
+option is given
 the numeric ID's are displayed.
 .Pp
 If the file is a character special or block special file,
Index: ls.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.c,v
retrieving revision 1.24
diff -u -d -r1.24 ls.c
--- ls.c	1999/05/08 10:20:30	1.24
+++ ls.c	1999/07/29 17:32:51
@@ -100,6 +100,7 @@
 int f_timesort;			/* sort by time vice name */
 int f_type;			/* add type character for non-regular files */
 int f_whiteout;			/* show whiteout entries */
+int f_numnames;			/* show numeric uid and gid in long listing */
 
 int rval;
 
@@ -137,7 +138,7 @@
 		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 @@
 		case 'k':
 			f_kblocks = 1;
 			break;
+		case 'n':
+			f_numnames = 1;
+			break;
 		case 'o':
 			f_flags = 1;
 			break;
@@ -401,6 +405,7 @@
 	char *initmax;
 	int entries, needstats;
 	char *user, *group, *flags, buf[20];	/* 32 bits == 10 digits */
+	char uid[10], gid[10];
 
 	/*
 	 * If list is NULL there are two possibilities: that the parent
@@ -512,10 +517,19 @@
 
 			btotal += sp->st_blocks;
 			if (f_longform) {
-				user = user_from_uid(sp->st_uid, 0);
+				if (f_numnames) {
+					snprintf(uid, sizeof(uid), "%u",
+					    sp->st_uid);
+					user = uid;
+					snprintf(gid, sizeof(gid), "%u",
+					    sp->st_gid);
+					group = gid;
+				} 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) {
Index: util.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/util.c,v
retrieving revision 1.18
diff -u -d -r1.18 util.c
--- util.c	1998/10/13 12:19:31	1.18
+++ util.c	1999/07/29 17:06:30
@@ -158,7 +158,7 @@
 void
 usage()
 {
-	(void)fprintf(stderr, "usage: ls [-ACFHLPRTWacdfgikloqrstu1]"
+	(void)fprintf(stderr, "usage: ls [-ACFHLPRTWacdfgiklnoqrstu1]"
 		      " [file ...]\n");
 	exit(1);
 }


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?48179.933270067>