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.

Modified files:

Index: src/bin/ls/ls.1
diff -u src/bin/ls/ls.1:1.66 src/bin/ls/ls.1:1.67
--- src/bin/ls/ls.1:1.66	Fri Dec 17 19:20:42 2010
+++ src/bin/ls/ls.1	Tue Mar 15 03:52:37 2011
@@ -1,4 +1,4 @@
-.\"	$NetBSD: ls.1,v 1.66 2010/12/17 19:20:42 njoly Exp $
+.\"	$NetBSD: ls.1,v 1.67 2011/03/15 03:52:37 erh Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1991, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -129,7 +129,9 @@
 options, causing the sizes to be reported in bytes displayed in a human
 readable format.
 Overrides
-.Fl k .
+.Fl k
+and
+.Fl M .
 .It Fl i
 For each file, print the file's file serial number (inode number).
 .It Fl k
@@ -142,7 +144,9 @@
 .Fl h
 flags overrides the previous flag.
 See also
-.Fl h .
+.Fl h 
+and
+.Fl M .
 .It Fl L
 For each file, if it's a link, evaluate file information and file type
 of the referenced file and not the link itself; however still print
@@ -156,6 +160,18 @@
 (See below.)
 A total sum for all the file sizes is output on a line before the long
 listing.
+.It Fl M
+Modifies the
+.Fl l
+and
+.Fl s
+options, causing the sizes or block counts reported to be separated with 
+commas (or a locale appropriate separator) resulting in a more readable
+output.
+Overrides 
+.Fl h .
+Does not override
+.Fl k .
 .It Fl m
 Stream output format; list files across the page, separated by commas.
 .It Fl n

Index: src/bin/ls/ls.c
diff -u src/bin/ls/ls.c:1.67 src/bin/ls/ls.c:1.68
--- src/bin/ls/ls.c:1.67	Thu Jul  8 20:43:34 2010
+++ src/bin/ls/ls.c	Tue Mar 15 03:52:37 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: ls.c,v 1.67 2010/07/08 20:43:34 rmind Exp $	*/
+/*	$NetBSD: ls.c,v 1.68 2011/03/15 03:52:37 erh Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)ls.c	8.7 (Berkeley) 8/5/94";
 #else
-__RCSID("$NetBSD: ls.c,v 1.67 2010/07/08 20:43:34 rmind Exp $");
+__RCSID("$NetBSD: ls.c,v 1.68 2011/03/15 03:52:37 erh Exp $");
 #endif
 #endif /* not lint */
 
@@ -91,6 +91,7 @@
 int f_flags;			/* show flags associated with a file */
 int f_grouponly;		/* long listing without owner */
 int f_humanize;			/* humanize the size field */
+int f_commas;           /* separate size field with comma */
 int f_inode;			/* print inode */
 int f_listdir;			/* list actual directory, not contents */
 int f_listdot;			/* list files beginning with . */
@@ -137,7 +138,7 @@
 		f_listdot = 1;
 
 	fts_options = FTS_PHYSICAL;
-	while ((ch = getopt(argc, argv, "1ABCFLRSTWabcdfghiklmnopqrstuwx")) != -1) {
+	while ((ch = getopt(argc, argv, "1ABCFLMRSTWabcdfghiklmnopqrstuwx")) != -1) {
 		switch (ch) {
 		/*
 		 * The -1, -C, -l, -m and -x options all override each other so
@@ -230,6 +231,11 @@
 		case 'h':
 			f_humanize = 1;
 			kflag = 0;
+			f_commas = 0;
+			break;
+		case 'M':
+			f_humanize = 0;
+			f_commas = 1;
 			break;
 		case 'n':
 			f_numericonly = 1;
@@ -603,6 +609,8 @@
 			(void)snprintf(buf, sizeof(buf), "%llu",
 			    (long long)howmany(maxblock, blocksize));
 			d.s_block = strlen(buf);
+			if (f_commas) /* allow for commas before every third digit */
+				d.s_block += (d.s_block - 1) / 3;
 		}
 		d.s_flags = maxflags;
 		d.s_group = maxgroup;
@@ -617,6 +625,8 @@
 			(void)snprintf(buf, sizeof(buf), "%llu",
 			    (long long)maxsize);
 			d.s_size = strlen(buf);
+			if (f_commas) /* allow for commas before every third digit */
+				d.s_size += (d.s_size - 1) / 3;
 		}
 		d.s_user = maxuser;
 		if (bcfile) {

Index: src/bin/ls/ls.h
diff -u src/bin/ls/ls.h:1.17 src/bin/ls/ls.h:1.18
--- src/bin/ls/ls.h:1.17	Sat Feb 14 08:02:04 2009
+++ src/bin/ls/ls.h	Tue Mar 15 03:52:38 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: ls.h,v 1.17 2009/02/14 08:02:04 lukem Exp $	*/
+/*	$NetBSD: ls.h,v 1.18 2011/03/15 03:52:38 erh Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -42,6 +42,7 @@
 extern int f_flags;		/* show flags associated with a file */
 extern int f_grouponly;		/* long listing without owner */
 extern int f_humanize;		/* humanize size field */
+extern int f_commas;        /* separate size field with commas */
 extern int f_inode;		/* print inode */
 extern int f_longform;		/* long listing format */
 extern int f_octal;		/* print octal escapes for nongraphic characters */

Index: src/bin/ls/print.c
diff -u src/bin/ls/print.c:1.48 src/bin/ls/print.c:1.49
--- src/bin/ls/print.c:1.48	Wed Aug 18 02:53:54 2010
+++ src/bin/ls/print.c	Tue Mar 15 03:52:38 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: print.c,v 1.48 2010/08/18 02:53:54 enami Exp $	*/
+/*	$NetBSD: print.c,v 1.49 2011/03/15 03:52:38 erh Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)print.c	8.5 (Berkeley) 7/28/94";
 #else
-__RCSID("$NetBSD: print.c,v 1.48 2010/08/18 02:53:54 enami Exp $");
+__RCSID("$NetBSD: print.c,v 1.49 2011/03/15 03:52:38 erh Exp $");
 #endif
 #endif /* not lint */
 
@@ -92,6 +92,7 @@
 	FTSENT *p;
 	NAMES *np;
 	char buf[20], szbuf[5];
+    char commabuf[27];  /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
 
 	now = time(NULL);
 
@@ -112,6 +113,13 @@
 				    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
 					err(1, "humanize_number");
 				(void)printf("%*s ", dp->s_block, szbuf);
+			} else if (f_commas) {
+				if (commaize_number(commabuf, sizeof(commabuf),
+				    (long long)howmany(sp->st_blocks,
+				    blocksize)) == -1)
+					err(1, "commaize_number(blocks=%lld)",
+					    (long long)howmany(sp->st_blocks, blocksize));
+				(void)printf("%*s ", dp->s_block, commabuf);
 			} else {
 				(void)printf("%*llu ", dp->s_block,
 				    (long long)howmany(sp->st_blocks,
@@ -138,6 +146,11 @@
 				    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
 					err(1, "humanize_number");
 				(void)printf("%*s ", dp->s_size, szbuf);
+			} else if (f_commas) {
+				if (commaize_number(commabuf, sizeof(commabuf),
+				    sp->st_size) == -1)
+					err(1, "commaize_number(size=%lld)", sp->st_size);
+				(void)printf("%*s ", dp->s_size, commabuf);
 			} else {
 				(void)printf("%*llu ", dp->s_size,
 				    (long long)sp->st_size);
@@ -321,6 +334,7 @@
 	struct stat *sp;
 	int chcnt;
 	char szbuf[5];
+    char commabuf[27];  /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
 
 	sp = p->fts_statp;
 	chcnt = 0;
@@ -333,6 +347,12 @@
 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
 				err(1, "humanize_number");
 			chcnt += printf("%*s ", sizefield, szbuf);
+		} else if (f_commas) {
+			if (commaize_number(commabuf, sizeof(commabuf),
+			    (long long)howmany(sp->st_blocks, blocksize) == -1))
+				err(1, "commaize_number(blocks=%lld)",
+				    (long long)howmany(sp->st_blocks, blocksize));
+			(void)printf("%*s ", sizefield, commabuf);
 		} else {
 			chcnt += printf("%*llu ", sizefield,
 			    (long long)howmany(sp->st_blocks, blocksize));
@@ -380,12 +400,14 @@
 /*
  * Display total used disk space in the form "total: %u\n".
  * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
- * but we humanise it with -h and use 1024 with -k.
+ * but we humanise it with -h, or separate it with commas with -M, and use 1024
+ * with -k.
  */
 static void
 printtotal(DISPLAY *dp)
 {
 	char szbuf[5];
+    char commabuf[27];  /* 64 bits == 20 digits, +6 for commas, +1 for NUL */
 	
 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
 		if (f_humanize) {
@@ -394,6 +416,12 @@
 			    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
 				err(1, "humanize_number");
 			(void)printf("total %s\n", szbuf);
+		} else if (f_commas) {
+			if (commaize_number(commabuf, sizeof(commabuf),
+			    (long long)howmany(dp->btotal, blocksize)) == -1)
+				err(1, "commaize_number(total=%lld)",
+				    (long long)howmany(dp->btotal, blocksize));
+			(void)printf("total %s\n", commabuf);
 		} else {
 			(void)printf("total %llu\n",
 			    (long long)(howmany(dp->btotal, blocksize)));
@@ -452,3 +480,4 @@
 	else
 		(void)printf("%s", path);
 }
+

Reply via email to