diff options
author | grog <grog@FreeBSD.org> | 2012-11-15 03:39:21 +0000 |
---|---|---|
committer | grog <grog@FreeBSD.org> | 2012-11-15 03:39:21 +0000 |
commit | f51ddfd74dac8fdaed5c407388615bb96697a5fa (patch) | |
tree | fca6652b69263e19bce47016a1ef31e06679d5db /bin/df/df.c | |
parent | fe8941c8236b4e50913b6786e1ef63a4789e4558 (diff) | |
download | FreeBSD-src-f51ddfd74dac8fdaed5c407388615bb96697a5fa.zip FreeBSD-src-f51ddfd74dac8fdaed5c407388615bb96697a5fa.tar.gz |
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
Diffstat (limited to 'bin/df/df.c')
-rw-r--r-- | bin/df/df.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/bin/df/df.c b/bin/df/df.c index f865b8f1..851c758 100644 --- a/bin/df/df.c +++ b/bin/df/df.c @@ -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); } |