Bug 12866

Summary: [PATCH] RFE for /bin/ls to add a -n option for showing uid/gid numerically
Product: Base System Reporter: pckizer
Component: binAssignee: Sheldon Hearn <sheldonh>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 3.2-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description pckizer 1999-07-29 07:20:00 UTC
I am familiar with having a '-n' option to /bin/ls on other platforms (i.e.
Solaris/Linux/IRIX/etc) to print uid/gid information numerically in long
(-l) listings rather than performing username/groupname lookups.  I was
unable to find any similar option to /bin/ls or similar programs.  (Unless
I just completely overlooked it).

Fix: Determine if my patch is acceptable, as given, or in concept; and apply in
/usr/src/bin/ls:
How-To-Repeat: 
No similar option found on FreeBSD, other OSs output similar to:
% ls -ldn
drwxr-x--x    9 1179     100         4096 Jul 29 00:53 .
Comment 1 nick.hibma 1999-07-29 08:34:00 UTC
A good reason for implementing this:  Try to do an ls -l on a local disk
when the NIS server is down.
Comment 2 Sheldon Hearn freebsd_committer freebsd_triage 1999-07-29 18:14:30 UTC
Responsible Changed
From-To: freebsd-bugs->sheldonh

I'll take this one. There are a couple of nits, so I'll post back 
a revised patch for your enjoyment. 

Comment 3 Doug 1999-07-29 22:31:26 UTC
pckizer@nostrum.com wrote:

> I am familiar with having a '-n' option to /bin/ls on other platforms 

	I'm very much in favor of adding this option, thank you for taking the
time to code it. My one suggestion would be that you optimize the code
slightly for the most likely case, namely that the -n option has not been
specified. 


> +                          if (!f_numbers) {
> +                                  user = user_from_uid(sp->st_uid, 0);
> +                                  group = group_from_gid(sp->st_gid, 0);
> +                           } else {
> +                                   snprintf(user_num, sizeof(user_num),
> +                                       "%d", sp->st_uid);
> +                                   user = user_num;
> +                                   snprintf(group_num, sizeof(group_num),
> +                                       "%d", sp->st_gid);
> +                                   group = user_num;
> +                           }

Hope this helps,

Doug
Comment 4 Sheldon Hearn 1999-07-30 01:36:43 UTC
On Thu, 29 Jul 1999 18:05:49 EST, Mike Pritchard wrote:

> If NetBSD or OpenBSD implement the -n option, we should probably just
> incorporate their program and man page changes instead of cooking up
> our own.

Both platforms support -n, both in different ways. I'll use their
variable names, no problem. In OpenBSD, -n implies -l, which is stupid.
NetBSD is more sensible.

I only want -n to have an effect when -l is specified, so that I can do

alias ls='ls -n'

When I do ``ls -l'' I'll get long listings with numeric user and group
columns. Otherwise, I'll get short listings as normal.

I'll copy NetBSD. Thanks for the tip.

Ciao,
Sheldon.
Comment 5 Bruce Evans 1999-07-30 01:45:54 UTC
>Alright, here's a patch for your perusal. It differs from yours in that:
>
>	* Variables have been renamed.
 	...

I see a few more problems.

>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 */

Disorder.

>@@ -401,6 +405,7 @@
> 	char *initmax;
> 	int entries, needstats;
> 	char *user, *group, *flags, buf[20];	/* 32 bits == 10 digits */
>+	char uid[10], gid[10];

Disorder.  char variables not grouped together.  At least the arrays should
be.  The comment for buf[] applies more directly to the new arrays.  You
shouldn't assume that uid_t is 32 bits.

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

snprintf() returns a value which is always ignored.  It is explicitly
(void)ed elsewhere.  This is not the way to use snprintf.  Either ensure
that the buffer is large enough, and don't use snprintf, or size the
buffer sloppily and check that it was large enough using snprintf and
handle the error somehow.

Bruce
Comment 6 Sheldon Hearn freebsd_committer freebsd_triage 1999-08-02 15:58:42 UTC
State Changed
From-To: open->suspended

Committed to HEAD, MFC should probably be held off until Bruce's 
concerns are addressed. 

Comment 7 Sheldon Hearn freebsd_committer freebsd_triage 1999-08-19 13:27:24 UTC
State Changed
From-To: suspended->analyzed

The style issues have been addressed. This one's an MFC candidate, once 
it's had a little time in CURRENT. 

Comment 8 Sheldon Hearn 1999-12-24 10:56:14 UTC
Just some feedback.  I've filed a defect report with the OpenGroup
people in which I propose that -n, -g and -o should not imply -l.  When
we get feedback from them, we'll go with the standard, whatever they
decide.

Ciao,
Sheldon.
Comment 9 Sheldon Hearn freebsd_committer freebsd_triage 2000-08-08 13:10:21 UTC
State Changed
From-To: analyzed->closed

My Open Group report went nowhere.  Since ls(1) is not recommended 
as a source for reliable input in portable shell scripts, and 
since our ls(1) already deviates from the standards, I'm closing 
this PR and leaving the behaviour as is. 

The documentation aspect of this issue will be addressed when my 
standards documentation project gets off the ground, probably 
after BSDcon.