Add , (comma) option to print sizes grouped and separated by thousands

using the non-monetary separator returned by localeconv(3), typically
a comma or period.

MFC after:  14 days
This commit is contained in:
Greg Lehey 2012-11-15 03:39:21 +00:00
parent fab7340c59
commit 019e4a53cb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=243049
2 changed files with 57 additions and 16 deletions

View File

@ -66,16 +66,14 @@ flag.
This is implied for file systems specified on the command line.
.It Fl b
Use 512-byte blocks rather than the default.
Note that
this overrides the
This overrides the
.Ev BLOCKSIZE
specification from the environment.
.It Fl c
Display a grand total.
.It Fl g
Use 1073741824-byte (1-Gbyte) blocks rather than the default.
Note that
this overrides the
This overrides the
.Ev BLOCKSIZE
specification from the environment.
.It Fl H
@ -97,16 +95,16 @@ are always printed in base 10.
Include statistics on the number of free inodes.
.It Fl k
Use 1024-byte (1-Kbyte) blocks rather than the default.
Note that
this overrides the
This overrides the
.Ev BLOCKSIZE
specification from the environment.
specification from the environment and the
.Fl P
option.
.It Fl l
Only display information about locally-mounted file systems.
.It Fl m
Use 1048576-byte (1-Mbyte) blocks rather than the default.
Note that
this overrides the
This overrides the
.Ev BLOCKSIZE
specification from the environment.
.It Fl n
@ -120,9 +118,12 @@ will not request new statistics from the file systems, but will respond
with the possibly stale statistics that were previously obtained.
.It Fl P
Use POSIX compliant output of 512-byte blocks rather than the default.
Note that this overrides the
This overrides the
.Ev BLOCKSIZE
specification from the environment.
The
.Fl k
option overrides this option.
.It Fl t
Only print out statistics for file systems of the specified types.
More than one type may be specified in a comma separated list.
@ -148,6 +149,13 @@ command can be used to find out the types of file systems
that are available on the system.
.It Fl T
Include file system type.
.It Fl ,
(Comma) Print sizes grouped and separated by thousands using the
non-monetary separator returned by
.Xr localeconv 3 ,
typically a comma or period.
If no locale is set, or the locale does not have a non-monetary separator, this
option has no effect.
.El
.Sh ENVIRONMENT
.Bl -tag -width BLOCKSIZE
@ -165,9 +173,21 @@ is set, the block counts will be displayed in units of that size block.
.Xr getfsstat 2 ,
.Xr statfs 2 ,
.Xr getmntinfo 3 ,
.Xr localeconv 3 ,
.Xr fstab 5 ,
.Xr mount 8 ,
.Xr quot 8
.Sh STANDARDS
With the exception of most options,
the
.Nm
utility conforms to
.St -p1003.1-2004 ,
which defines only the
.Fl k , P
and
.Fl t
options.
.Sh HISTORY
A
.Nm

View File

@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
#include <ufs/ufs/ufsmount.h>
#include <err.h>
#include <libutil.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -95,6 +96,7 @@ imax(int a, int b)
}
static int aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
static int thousands;
static struct ufs_args mdev;
int
@ -111,12 +113,12 @@ main(int argc, char *argv[])
int ch, rv;
fstype = "ufs";
(void)setlocale(LC_ALL, "");
memset(&totalbuf, 0, sizeof(totalbuf));
totalbuf.f_bsize = DEV_BSIZE;
strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
vfslist = NULL;
while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T")) != -1)
while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T,")) != -1)
switch (ch) {
case 'a':
aflag = 1;
@ -180,6 +182,9 @@ main(int argc, char *argv[])
case 'T':
Tflag = 1;
break;
case ',':
thousands = 1;
break;
case '?':
default:
usage();
@ -410,10 +415,18 @@ prtstat(struct statfs *sfsp, struct maxwidths *mwp)
static int headerlen, timesthrough = 0;
static const char *header;
int64_t used, availblks, inodes;
const char *format;
if (++timesthrough == 1) {
mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
if (thousands) { /* make space for commas */
mwp->total += (mwp->total - 1) / 3;
mwp->used += (mwp->used - 1) / 3;
mwp->avail += (mwp->avail - 1) / 3;
mwp->iused += (mwp->iused - 1) / 3;
mwp->ifree += (mwp->ifree - 1) / 3;
}
if (hflag) {
header = " Size";
mwp->total = mwp->used = mwp->avail =
@ -428,7 +441,7 @@ prtstat(struct statfs *sfsp, struct maxwidths *mwp)
(void)printf("%-*s", mwp->mntfrom, "Filesystem");
if (Tflag)
(void)printf(" %-*s", mwp->fstype, "Type");
(void)printf(" %-*s %*s %*s Capacity", mwp->total, header,
(void)printf(" %*s %*s %*s Capacity", mwp->total, header,
mwp->used, "Used", mwp->avail, "Avail");
if (iflag) {
mwp->iused = imax(hflag ? 0 : mwp->iused,
@ -448,7 +461,11 @@ prtstat(struct statfs *sfsp, struct maxwidths *mwp)
if (hflag) {
prthuman(sfsp, used);
} else {
(void)printf(" %*jd %*jd %*jd",
if (thousands)
format = " %*j'd %*j'd %*j'd";
else
format = " %*jd %*jd %*jd";
(void)printf(format,
mwp->total, fsbtoblk(sfsp->f_blocks,
sfsp->f_bsize, blocksize),
mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
@ -465,7 +482,11 @@ prtstat(struct statfs *sfsp, struct maxwidths *mwp)
prthumanvalinode(used);
prthumanvalinode(sfsp->f_ffree);
} else {
(void)printf(" %*jd %*jd", mwp->iused, (intmax_t)used,
if (thousands)
format = " %*j'd %*j'd";
else
format = " %*jd %*jd";
(void)printf(format, mwp->iused, (intmax_t)used,
mwp->ifree, (intmax_t)sfsp->f_ffree);
}
(void)printf(" %4.0f%% ", inodes == 0 ? 100.0 :
@ -542,7 +563,7 @@ usage(void)
{
(void)fprintf(stderr,
"usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...]\n");
"usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,] [file | filesystem ...]\n");
exit(EX_USAGE);
}