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).
This commit is contained in:
erh 2011-03-15 03:52:37 +00:00
parent 9a5590a540
commit 4aaf499c53
4 changed files with 66 additions and 10 deletions

View File

@ -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 @@ and
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 @@ and
.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 @@ List in long format.
(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

View File

@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
#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_columnacross; /* columnated format, sorted across */
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 @@ ls_main(int argc, char *argv[])
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 @@ ls_main(int argc, char *argv[])
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 @@ display(FTSENT *p, FTSENT *list)
(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 @@ display(FTSENT *p, FTSENT *list)
(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) {

View File

@ -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_accesstime; /* use time of last access */
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 */

View File

@ -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 @@ printlong(DISPLAY *dp)
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 @@ printlong(DISPLAY *dp)
(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 @@ printlong(DISPLAY *dp)
(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 @@ printaname(FTSENT *p, int inodefield, int sizefield)
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 @@ printaname(FTSENT *p, int inodefield, int sizefield)
(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 @@ printtime(time_t ftime)
/*
* 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 @@ printtotal(DISPLAY *dp)
(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 @@ printlink(FTSENT *p)
else
(void)printf("%s", path);
}