NetBSD Problem Report #7540

Received: (qmail 24509 invoked from network); 9 May 1999 18:52:38 -0000
Message-Id: <199905091852.LAA08571@NetBSD.Wireless.Com>
Date: Sun, 9 May 1999 11:52:38 -0700 (PDT)
From: mac@wireless.com
Reply-To: mac@wireless.com
To: gnats-bugs@gnats.netbsd.org
Subject: Suggested improvement to "ls"
X-Send-Pr-Version: 3.95

>Number:         7540
>Notify-List:    erh@netbsd.org
>Category:       bin
>Synopsis:       Suggested improvement to "ls"
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    erh
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun May 09 12:05:00 +0000 1999
>Closed-Date:    Wed Apr 20 18:11:12 +0000 2011
>Last-Modified:  Wed Apr 20 18:11:12 +0000 2011
>Originator:     User mac
>Release:        5.0
>Organization:

>Environment:
	486/16MB
System: NetBSD NetBSD 1.4_BETA NetBSD 1.4_BETA (GENERIC) #1: Wed Apr 21 09:17:06 PDT 1999 root@struis:/usr/src/sys/arch/i386/compile/GENERIC i386


>Description:
	When doing "ls -l" where files are large, it's difficult quickly
	parse the size, e.g.
	 -rwxr-xr-x  1 root  wheel  45199899 May  9 00:44 foo

	It's easier to read when it's like this:
	 -rwxr-xr-x  1 root  wheel  45,199,899 May  9 00:44 foo

>How-To-Repeat:
	"ls -l" on large files
>Fix:
Here are the diffs to ls.h, ls.c, and print.c for the /usr/src/bin/ls/
directory.  I added the "-M" switch (for coMMas) that also forces "-l".
Note, I did -not- update the documentation, but will if asked.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- ls.original.h	Sun May  9 00:45:22 1999
+++ ls.h	Sun May  9 00:44:02 1999
@@ -51,6 +51,7 @@
 extern int f_statustime;	/* use time of last mode change */
 extern int f_type;		/* add type character for non-regular files */
 extern int f_typedir;		/* add type character for directories */
+extern int f_commas;		/* nonzero if you want commas in filesizes */

 typedef struct {
 	FTSENT *list;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- ls.original.c	Sat May  8 00:26:50 1999
+++ ls.c	Sat May  8 22:06:23 1999
@@ -108,6 +108,7 @@
 int f_type;			/* add type character for non-regular files */
 int f_typedir;			/* add type character for directories */
 int f_whiteout;			/* show whiteout entries */
+int f_commas;			/* show numbers with commas; implies -l */

 int
 main(argc, argv)
@@ -136,7 +137,8 @@
 		f_listdot = 1;

 	fts_options = FTS_PHYSICAL;
-	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) {
+	f_commas = 0; /* Normally, no commas */
+	while ((ch = getopt(argc, argv, "1ACFLMRSTWacdfgiklmnopqrstux")) != -1) {
 		switch (ch) {
 		/*
 		 * The -1, -C, -l, -m and -x options all override each other so
@@ -151,6 +153,8 @@
 			f_columnacross = f_longform = f_singlecol = f_stream =
 			    0;
 			break;
+		case 'M':
+			f_commas = 1;
 		case 'l':
 			f_longform = 1;
 			f_column = f_columnacross = f_singlecol = f_stream = 0;
@@ -558,6 +562,8 @@
 		d.s_nlink = strlen(buf);
 		(void)snprintf(buf, sizeof(buf), "%llu", (long long)maxsize);
 		d.s_size = strlen(buf);
+		if(f_commas)
+			d.s_size += (d.s_size - 1) / 3; /* space for commas */
 		d.s_user = maxuser;
 		if (bcfile) {
 			(void)snprintf(buf, sizeof(buf), "%u", maxmajor);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- print.original.c	Sat May  8 00:26:54 1999
+++ print.c	Sun May  9 00:41:54 1999
@@ -68,6 +68,7 @@
 static void	printlink __P((FTSENT *));
 static void	printtime __P((time_t));
 static int	printtype __P((u_int));
+static void     commas(char *);

 static time_t	now;

@@ -94,7 +95,7 @@
 	struct stat *sp;
 	FTSENT *p;
 	NAMES *np;
-	char buf[20];
+	char buf[27]; /* Was 20; More room, in case commas are used */

 	now = time(NULL);

@@ -123,9 +124,12 @@
 			(void)printf("%*u, %*u ",
 			    dp->s_major, major(sp->st_rdev), dp->s_minor,
 			    minor(sp->st_rdev));
-		else
-			(void)printf("%*llu ", dp->s_size,
-			    (long long)sp->st_size);
+		else {
+		  sprintf(buf,"%llu",(long long)sp->st_size);
+		  if(f_commas) commas(buf);
+		  (void)printf("%*s ", dp->s_size, buf);
+		}
+
 		if (f_accesstime)
 			printtime(sp->st_atime);
 		else if (f_statustime)
@@ -380,4 +384,39 @@
 	}
 	path[lnklen] = '\0';
 	(void)printf(" -> %s", path);
+}
+
+/*
+ * Return a string with commas every 3 positions.
+ * the original string is replace with the string with commas.
+ *
+ * The caller must be sure that there is enough room for the resultant
+ * string.
+ *
+ *
+ * Mike Cheponis   4 Dec 87
+ */
+static void
+commas(char *destination){
+
+  char *source, *core; /* Place holder for malloc */
+  unsigned int comma_counter;	/* The comma counter */
+  unsigned int length;
+
+  length = strlen(destination);/* Make copy, so we can change return value */
+  core = source = strdup(destination);
+  if(core == NULL){
+    *destination = '\0';/* We lost, so return null string */
+    return;
+  }
+  comma_counter = (length-1)%3 + 1;	/* Tells us when to insert a comma */
+
+  while(*source){
+    *destination++ = *source++;
+    if( --comma_counter == 0  &&  *source ){
+      *destination++ = ','; comma_counter = 3;
+    }
+  }
+  free(core);
+  *destination = '\0';
 }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>Release-Note:
>Audit-Trail:

From: "Erik E. Fair" <fair@clock.org>
To: mac@wireless.com
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/7540: Suggested improvement to "ls"
Date: Sun, 9 May 1999 13:33:30 -0700

 In Europe, they use dot for comma, and comma for dot (e.g. $1.000,00). If
 you generalize the routine to handle that case, and add one more option to
 ls to emit this format, it'll be i18n ready...

 	Erik <fair@clock.org>

From: joda@pdc.kth.se (Johan Danielsson)
To: mac@wireless.com
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/7540: Suggested improvement to "ls"
Date: 10 May 1999 12:12:42 +0200

 mac@wireless.com writes:

 > 	 -rwxr-xr-x  1 root  wheel  45,199,899 May  9 00:44 foo

 This strikes me as completely bogus. If you want this, you should use
 some lang-ified way to print digits (and dates), not all locales use
 commas for this purpose.

 A better idea is to steal the `humanize' option from GNU ls (the above
 then becomes 43G).

 /Johan

From: Giles Lean <giles@nemeton.com.au>
To: mac@wireless.com
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/7540: Suggested improvement to "ls" 
Date: Tue, 11 May 1999 20:14:40 +1000

 On Sun, 9 May 1999 11:52:38 -0700 (PDT)  mac@wireless.com wrote:

 > >Description:
 > 	When doing "ls -l" where files are large, it's difficult quickly
 > 	parse the size, e.g.
 > 	 -rwxr-xr-x  1 root  wheel  45199899 May  9 00:44 foo
 > 
 > 	It's easier to read when it's like this:
 > 	 -rwxr-xr-x  1 root  wheel  45,199,899 May  9 00:44 foo

 Erm ... not all the world uses commas.  I think the appropriate thing
 to do would be to use locale specific formatting.  I'm rusty on
 internationalisation to know precisely what to direct you to, but
 setlocale(3) wouldn't be a bad start.

 Bonus is that you could do locale dependent sorting, too. :-)

 Regards,

 Giles



From: Mike Cheponis <mac@Wireless.Com>
To: Giles Lean <giles@nemeton.com.au>
Cc: gnats-bugs@gnats.netbsd.org, fair@clock.org
Subject: Re: bin/7540: Suggested improvement to "ls" 
Date: Tue, 11 May 1999 15:28:45 -0700 (PDT)

 This is an excellent suggestion!

 setlocale(3) says, in part:


  thousands_sep      The separator between groups of digits before the dec-
                         imal point, except for currency values.

 So I think that's it! We can pick some letter NOT in ACFLRSTWacdfgiklmnoqrstux
 (I've arbitrarily chosen "-M" so if anybody thinks something else would
 be better, speak up now) and use setlocale's thousands_sep to specify
 the particular separator character.

 -Mike  mac@wireless.com

 p.s. For the buzzword prone, I've discovered that "Internationalization" is
 often spelled "i18n".  Hmmmm....


 On Tue, 11 May 1999, Giles Lean wrote:
 > On Sun, 9 May 1999 11:52:38 -0700 (PDT)  mac@wireless.com wrote:
 > 
 > > >Description:
 > > 	When doing "ls -l" where files are large, it's difficult quickly
 > > 	parse the size, e.g.
 > > 	 -rwxr-xr-x  1 root  wheel  45199899 May  9 00:44 foo
 > > 
 > > 	It's easier to read when it's like this:
 > > 	 -rwxr-xr-x  1 root  wheel  45,199,899 May  9 00:44 foo
 > 
 > Erm ... not all the world uses commas.  I think the appropriate thing
 > to do would be to use locale specific formatting.  I'm rusty on
 > internationalisation to know precisely what to direct you to, but
 > setlocale(3) wouldn't be a bad start.
 > 
 > Bonus is that you could do locale dependent sorting, too. :-)
 > 
 > Regards,
 > 
 > Giles


From: Olaf Seibert <rhialto@polder.ubc.kun.nl>
To: mac@wireless.com
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/7540: Suggested improvement to "ls"
Date: Thu, 13 May 1999 13:45:05 +0200 (CEST)

 Europe, and I think most of the rest of the world too, uses dots,
 not commas, to separate digit groups: 123.456,00.

 -Olaf.
 --
 ___ Olaf 'Rhialto' Seibert - rhialto@polder.ubc. ---- Unauthorized duplication,
 \X/ .kun.nl ---- while sometimes necessary, is never as good as the real thing. 


From: Mike Cheponis <mac@Wireless.Com>
To: Olaf Seibert <rhialto@polder.ubc.kun.nl>
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: bin/7540: Suggested improvement to "ls"
Date: Thu, 13 May 1999 13:01:06 -0700 (PDT)

 Yes, thanks for that, and I actually knew that but didn't know of a clean way
 to deal with that (other than re-educating the rest of the world, but I
 guess that wouldn't be Politically Correct...).

 The answer seems to be in using setlocale(3).

 -mac


 On Thu, 13 May 1999, Olaf Seibert wrote:

 > Europe, and I think most of the rest of the world too, uses dots,
 > not commas, to separate digit groups: 123.456,00.
 > 
 > -Olaf.
 > --
 > ___ Olaf 'Rhialto' Seibert - rhialto@polder.ubc. ---- Unauthorized duplication,
 > \X/ .kun.nl ---- while sometimes necessary, is never as good as the real thing. 
 > 
 > 

Responsible-Changed-From-To: bin-bug-people->erh
Responsible-Changed-By: erh@netbsd.org
Responsible-Changed-When: Sat, 23 Sep 2006 23:43:51 +0000
Responsible-Changed-Why:
The ls command now has a "-h" option, implementing the humanize number feature.
So, it seems like the change requested in this PR to use commas/dots/whatever
is no longer really needed.  I'm going to close this.


State-Changed-From-To: open->feedback
State-Changed-By: erh@netbsd.org
State-Changed-When: Sat, 23 Sep 2006 23:43:51 +0000
State-Changed-Why:


State-Changed-From-To: feedback->open
State-Changed-By: erh@netbsd.org
State-Changed-When: Sun, 24 Sep 2006 03:22:39 +0000
State-Changed-Why:
The change still makes sense, since the humanized output isn't so readable.
.


From: Mike Cheponis <mac@Wireless.Com>
To: erh@NetBSD.org
Cc: 
Subject: Re: bin/7540 (Suggested improvement to "ls")
Date: Sat, 23 Sep 2006 18:17:07 -0700 (PDT)

 Should I submit another PR?

 Observe what ls -hl outputs:

 -rw-r--r--    1 mac  users   82B Sep 19 04:01 t2.c
 -rw-r--r--    1 mac  users  1.1K Sep 19 04:02 t2.ihx
 -rw-r--r--    1 mac  users  168B Sep 19 04:02 t2.lnk
 -rw-r--r--    1 mac  users  9.9K Sep 19 04:02 t2.lst
 -rw-r--r--    1 mac  users  6.3K Sep 19 04:02 t2.map
 -rw-r--r--    1 mac  users  941B Sep 19 04:02 t2.mem
 -rw-r--r--    1 mac  users  2.1K Sep 19 04:02 t2.rel
 -rw-r--r--    1 mac  users  9.9K Sep 19 04:02 t2.rst

 Compare this to what I've suggested:


 -rw-r--r--  1 mac  users      82 Sep 19 04:01 t2.c
 -rw-r--r--  1 mac  users   1,160 Sep 19 04:02 t2.ihx
 -rw-r--r--  1 mac  users     168 Sep 19 04:02 t2.lnk
 -rw-r--r--  1 mac  users  10,167 Sep 19 04:02 t2.lst
 -rw-r--r--  1 mac  users   6,456 Sep 19 04:02 t2.map
 -rw-r--r--  1 mac  users     941 Sep 19 04:02 t2.mem
 -rw-r--r--  1 mac  users   2,147 Sep 19 04:02 t2.rel
 -rw-r--r--  1 mac  users  10,167 Sep 19 04:02 t2.rst


 Notice how much easier to read this is versus the "humanized" output.


 Also note that my merely "scanning" with your eyes, you can VERY quickly see which file is biggest, which is smallest, etc.  Not to mention that you can extract the _exact_ file size with my method.

 By introducing two paradigms at once (numbers [truncated at that!]  + some "power of 10" code) the human interface is more complicated.  Plus, it's ugly.

 ------

 A long time ago, I fixed this problem in my local ls, but at that time, my changes were not accepted back into the tree because I had not made them i18n-compatible (never mind that there _was_ no i18n structure at that time.)

 Still, since NetBSD is the finest unix-like OS in the known universe, I do think it's important for details like this to be done right.

 Thanks again,

 -Mike



 On Sat, 23 Sep 2006, erh@NetBSD.org wrote:

 > Date: Sat, 23 Sep 2006 23:43:52 +0000 (UTC)
 > From: erh@NetBSD.org
 > Reply-To: gnats-bugs@NetBSD.org
 > To: erh@NetBSD.org, gnats-admin@NetBSD.org, netbsd-bugs@NetBSD.org,
 >     erh@NetBSD.org, mac@wireless.com
 > Cc: erh@NetBSD.org
 > Subject: Re: bin/7540 (Suggested improvement to "ls")
 > 
 > Synopsis: Suggested improvement to "ls"
 >
 > Responsible-Changed-From-To: bin-bug-people->erh
 > Responsible-Changed-By: erh@netbsd.org
 > Responsible-Changed-When: Sat, 23 Sep 2006 23:43:51 +0000
 > Responsible-Changed-Why:
 > The ls command now has a "-h" option, implementing the humanize number feature.
 > So, it seems like the change requested in this PR to use commas/dots/whatever
 > is no longer really needed.  I'm going to close this.
 >
 >
 > State-Changed-From-To: open->feedback
 > State-Changed-By: erh@netbsd.org
 > State-Changed-When: Sat, 23 Sep 2006 23:43:51 +0000
 > State-Changed-Why:
 >
 >
 >

From: Eric Haszlakiewicz <erh@NetBSD.org>
To: Mike Cheponis <mac@Wireless.Com>
Cc: 
Subject: Re: bin/7540 (Suggested improvement to "ls")
Date: Sat, 23 Sep 2006 22:21:25 -0500

 On Sat, Sep 23, 2006 at 06:17:07PM -0700, Mike Cheponis wrote:
 > 
 > Observe what ls -hl outputs:
 [snip]
 > Compare this to what I've suggested:
 [snip]
 > 
 > Notice how much easier to read this is versus the "humanized" output.

 Yeah, I see what you mean.  It looks like -M is still an available flag,
 so I can probably use most of the previously provided patch.

 > Should I submit another PR?
 	no, I'll just move this one back to opened.

 eric

From: "Eric Haszlakiewicz" <erh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/7540 CVS commit: src/lib/libc/gen
Date: Tue, 15 Mar 2011 03:47:04 +0000

 Module Name:	src
 Committed By:	erh
 Date:		Tue Mar 15 03:47:04 UTC 2011

 Modified Files:
 	src/lib/libc/gen: Makefile.inc
 Added Files:
 	src/lib/libc/gen: commaize_number.3 commaize_number.c

 Log Message:
 PR#7540, add a commaize_number function, which inserts comma into a string
  of digits to make it more readable.  This is soon to be used in /bin/ls.


 To generate a diff of this commit:
 cvs rdiff -u -r1.174 -r1.175 src/lib/libc/gen/Makefile.inc
 cvs rdiff -u -r0 -r1.1 src/lib/libc/gen/commaize_number.3 \
     src/lib/libc/gen/commaize_number.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Eric Haszlakiewicz" <erh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/7540 CVS commit: src/bin/ls
Date: Tue, 15 Mar 2011 03:52:38 +0000

 Module Name:	src
 Committed By:	erh
 Date:		Tue Mar 15 03:52:38 UTC 2011

 Modified Files:
 	src/bin/ls: ls.1 ls.c ls.h print.c

 Log Message:
 PR#7540, add a -M option to ls which causes sizes (and number of blocks) to be
 displayed with comma separators (or a locale specific separator).


 To generate a diff of this commit:
 cvs rdiff -u -r1.66 -r1.67 src/bin/ls/ls.1
 cvs rdiff -u -r1.67 -r1.68 src/bin/ls/ls.c
 cvs rdiff -u -r1.17 -r1.18 src/bin/ls/ls.h
 cvs rdiff -u -r1.48 -r1.49 src/bin/ls/print.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: erh@NetBSD.org
State-Changed-When: Wed, 16 Mar 2011 01:38:05 +0000
State-Changed-Why:
The changes to /bin/ls are in -current, although using the %'llu format
to printf instead of the unnecessary commaize_number() function.


State-Changed-From-To: feedback->closed
State-Changed-By: erh@NetBSD.org
State-Changed-When: Wed, 20 Apr 2011 18:11:12 +0000
State-Changed-Why:
I'm closing this, since there are no objections to the currently implemented feature.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.