Subject: Patches for du(1) and df(1)
To: None <tech-userlevel@netbsd.org>
From: Tomas Svensson <tsn@gbdev.net>
List: tech-userlevel
Date: 11/22/2001 15:35:20
--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Hi,
I added a -h flag to du(1) since noone complained on the second patch...
Example output from du -h:
4.0K ./ypcat/CVS
14K ./ypcat
4.0K ./ypmatch/CVS
14K ./ypmatch
4.0K ./ypwhich/CVS
21K ./ypwhich
14M .
I also included a new patch for df with a clarification of the manpage
and fixed a possible rounding error.
If there's no objections against this, can someone commit the changes?
-Tomas
--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="du.diff"
Index: du.1
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/du/du.1,v
retrieving revision 1.10
diff -u -r1.10 du.1
--- du.1 2001/07/08 15:34:14 1.10
+++ du.1 2001/11/22 12:08:59
@@ -43,7 +43,7 @@
.Nm
.Op Fl H | Fl L | Fl P
.Op Fl a | Fl s
-.Op Fl ckmrx
+.Op Fl chkmrx
.Op Ar file ...
.Sh DESCRIPTION
The
@@ -65,6 +65,12 @@
No symbolic links are followed.
.It Fl a
Display an entry for each file in the file hierarchy.
+.It Fl c
+Display the grand total after all the arguments have been processed.
+.It Fl h
+Display sizes in a more "human-readable" and compact form. Use unit suffixes:
+Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte and Exabyte in order to
+reduce the number of digits to four or fewer using base 2 for sizes.
.It Fl k
By default,
.Nm
@@ -80,8 +86,6 @@
.Fl m
flag is specified, the number displayed is the number of megabyte
(1024*1024 bytes) blocks.
-.It Fl c
-Display the grand total after all the arguments have been processed.
.It Fl r
Generate warning messages about directories that cannot be read.
This is the default behaviour.
@@ -122,6 +126,7 @@
If the environment variable
.Ev BLOCKSIZE
is set, and the
+.Fl h
.Fl k
and
.Fl m
Index: du.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/du/du.c,v
retrieving revision 1.17
diff -u -r1.17 du.c
--- du.c 2001/01/04 23:05:54 1.17
+++ du.c 2001/11/22 12:09:00
@@ -64,6 +64,7 @@
int linkchk __P((FTSENT *));
int main __P((int, char **));
+void prthuman(int64_t);
void usage __P((void));
int
@@ -75,14 +76,15 @@
FTSENT *p;
long blocksize, totalblocks;
int ftsoptions, listdirs, listfiles;
- int Hflag, Lflag, Pflag, aflag, ch, cflag, kmflag, notused, rval, sflag;
+ int Hflag, Lflag, Pflag, aflag, ch, cflag, hflag, kmflag, notused,
+ rval, sflag;
char **save;
save = argv;
- Hflag = Lflag = Pflag = aflag = cflag = kmflag = sflag = 0;
+ Hflag = Lflag = Pflag = aflag = cflag = hflag = kmflag = sflag = 0;
totalblocks = 0;
ftsoptions = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "HLPackmrsx")) != -1)
+ while ((ch = getopt(argc, argv, "HLPachkmrsx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@@ -102,6 +104,9 @@
case 'c':
cflag = 1;
break;
+ case 'h':
+ hflag = 1;
+ break;
case 'k':
blocksize = 1024;
kmflag = 1;
@@ -182,10 +187,16 @@
* or directories and this is post-order of the
* root of a traversal, display the total.
*/
- if (listdirs || (!listfiles && !p->fts_level))
- (void)printf("%ld\t%s\n",
- howmany(p->fts_number, blocksize),
- p->fts_path);
+ if (listdirs || (!listfiles && !p->fts_level)) {
+ if (hflag) {
+ prthuman(howmany(p->fts_number,
+ blocksize) * blocksize * 512);
+ (void)printf("\t%s\n",p->fts_path);
+ } else
+ (void)printf("%ld\t%s\n",
+ howmany(p->fts_number, blocksize),
+ p->fts_path);
+ }
break;
case FTS_DC: /* Ignore. */
break;
@@ -202,19 +213,32 @@
* If listing each file, or a non-directory file was
* the root of a traversal, display the total.
*/
- if (listfiles || !p->fts_level)
- (void)printf("%lld\t%s\n", (long long)
- howmany(p->fts_statp->st_blocks, blocksize),
- p->fts_path);
+ if (listfiles || !p->fts_level) {
+ if (hflag) {
+ prthuman(howmany(p->fts_statp->\
+ st_blocks, blocksize) * blocksize
+ * 512);
+ (void)printf("\t%s\n",p->fts_path);
+ } else
+ (void)printf("%lld\t%s\n", (long long)
+ howmany(p->fts_statp->st_blocks,
+ blocksize), p->fts_path);
+ }
p->fts_parent->fts_number += p->fts_statp->st_blocks;
if (cflag)
totalblocks += p->fts_statp->st_blocks;
}
if (errno)
err(1, "fts_read");
- if (cflag)
- (void)printf("%ld\ttotal\n",
- howmany(totalblocks, blocksize));
+ if (cflag) {
+ if (hflag) {
+ prthuman(howmany(totalblocks, blocksize) * blocksize
+ * 512);
+ (void)printf("\ttotal\n");
+ } else
+ (void)printf("%ld\ttotal\n",
+ howmany(totalblocks, blocksize));
+ }
exit(0);
}
@@ -249,11 +273,45 @@
return (0);
}
+/*
+ * Print size in a more "human-readable" and compact form.
+ */
+void
+prthuman(int64_t bytes)
+{
+ int i, size1, size2, unit;
+
+ if (bytes == 0)
+ (void)printf(" 0B");
+ else {
+ i = 0;
+ bytes *= 100;
+ for(unit = -1; unit == -1; i++)
+ if (bytes < 102400 || i == 6)
+ unit = i;
+ else
+ bytes /= 1024;
+ if (bytes >= 1000)
+ (void)printf("%4lli%c",((bytes + 50) / 100),
+ "BKMGTPE"[unit]);
+ else {
+ size1 = bytes / 100;
+ if ((size2 = (((bytes % 100) + 5) / 10)) == 10) {
+ size1++;
+ size2 = 0;
+ }
+ (void)printf(" %1i.%1i%c", size1, size2,
+ "BKMGTPE"[unit]);
+ }
+ }
+}
+
+
void
usage()
{
(void)fprintf(stderr,
- "usage: du [-H | -L | -P] [-a | -s] [-ckmrx] [file ...]\n");
+ "usage: du [-H | -L | -P] [-a | -s] [-chkmrx] [file ...]\n");
exit(1);
}
--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="df.diff"
Index: df.1
===================================================================
RCS file: /cvsroot/basesrc/bin/df/df.1,v
retrieving revision 1.23
diff -u -r1.23 df.1
--- df.1 2001/11/05 17:51:56 1.23
+++ df.1 2001/11/22 11:49:56
@@ -41,7 +41,7 @@
.Nd display free disk space
.Sh SYNOPSIS
.Nm
-.Op Fl aiklmnP
+.Op Fl aghHiklmnP
.Op Fl t Ar type
.Op Ar file | Ar file_system ...
.Sh DESCRIPTION
@@ -51,7 +51,7 @@
or on the file system of which
.Ar file
is a part.
-Values are displayed in 512-byte per block block counts.
+Values are displayed in 512-byte per block counts.
If neither a file or a
.Ar file_system
operand is specified,
@@ -74,22 +74,30 @@
Show all filesystems. By default only filesystems mounted with the
.Dv MNT_IGNORE
flag clear are shown.
+.It Fl g
+Use 1073741824-byte (gigabyte) block counts rather than the default. This
+overrides the BLOCKSIZE specification from the environment.
+.It Fl h
+Display sizes in a more "human-readable" and compact form. Use unit suffixes:
+Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte and Exabyte in order to
+reduce the number of digits to four or fewer using base 2 for sizes.
+.It Fl H
+Same as
+.Fl h
+but reduce the number of digits to three or fewer using base 10 for sizes.
.It Fl i
Include statistics on the number of free inodes.
.It Fl k
-By default, all sizes are reported in 512-byte block counts.
-The
-.Fl k
-option causes the numbers to be reported in kilobyte (1024 bytes) counts.
+Use 1024-byte (kilobyte) block counts rather than the default. This overrides
+the BLOCKSIZE specification from the environment.
.It Fl l
Display statistics only about mounted file systems with the
.Dv MNT_LOCAL
flag set. If a non-local file system is given as an argument, a
warning is issued and no information is given on that file system.
.It Fl m
-The
-.Fl m
-option causes the numbers to be reported in megabyte (1024*1024 bytes) counts.
+Use 1048576-byte (megabyte) block counts rather than the default. This
+overrides the BLOCKSIZE specification from the environment.
.It Fl n
Print out the previously obtained statistics from the file systems.
This option should be used if it is possible that one or more
@@ -146,6 +154,9 @@
If the environment variable
.Ev BLOCKSIZE
is set, and the
+.Fl g
+.Fl h
+.Fl H
.Fl k
and
.Fl m
Index: df.c
===================================================================
RCS file: /cvsroot/basesrc/bin/df/df.c,v
retrieving revision 1.42
diff -u -r1.42 df.c
--- df.c 2001/10/11 16:31:33 1.42
+++ df.c 2001/11/22 11:49:57
@@ -72,14 +72,15 @@
int main(int, char *[]);
int bread(off_t, void *, int);
char *getmntpt(char *);
-void prtstat(struct statfs *, int);
+void prthuman(int64_t, int);
+void prtstat(struct statfs *, int, int);
int ufs_df(char *, struct statfs *);
int selected(const char *);
void maketypelist(char *);
long regetmntinfo(struct statfs **, long);
void usage(void);
-int aflag, iflag, kflag, lflag, mflag, nflag, Pflag;
+int aflag, gflag, hflag, iflag, kflag, lflag, mflag, nflag, Pflag;
char **typelist = NULL;
struct ufs_args mdev;
@@ -89,14 +90,25 @@
struct stat stbuf;
struct statfs *mntbuf;
long mntsize;
- int ch, i, maxwidth, width;
+ int base, ch, i, maxwidth, width;
char *mntpt;
- while ((ch = getopt(argc, argv, "aiklmnPt:")) != -1)
+ while ((ch = getopt(argc, argv, "aghHiklmnPt:")) != -1)
switch (ch) {
case 'a':
aflag = 1;
break;
+ case 'g':
+ gflag = 1;
+ break;
+ case 'h':
+ hflag = 1;
+ base = 1024;
+ break;
+ case 'H':
+ hflag = 1;
+ base = 1000;
+ break;
case 'i':
iflag = 1;
break;
@@ -203,7 +215,7 @@
maxwidth = width;
}
for (i = 0; i < mntsize; i++)
- prtstat(&mntbuf[i], maxwidth);
+ prtstat(&mntbuf[i], maxwidth, base);
exit(0);
/* NOTREACHED */
}
@@ -326,10 +338,49 @@
(num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
/*
+ * Print statistics in a more "human-readable" and compact form.
+ */
+void
+prthuman(int64_t bytes, int base)
+{
+ int i, neg, size1, size2, unit;
+
+ if (bytes == 0)
+ (void)printf(" 0B");
+ else {
+ i = 0;
+ if (bytes < 0) {
+ bytes *= -100;
+ neg = -1;
+ } else {
+ bytes *= 100;
+ neg = 1;
+ }
+ for(unit = -1; unit == -1; i++)
+ if (bytes < (base * 100) || i == 6)
+ unit = i;
+ else
+ bytes /= base;
+ if (bytes >= 1000)
+ (void)printf(" %5lli%c",(neg * ((bytes + 50) / 100)),
+ "BKMGTPE"[unit]);
+ else {
+ size1 = bytes / 100;
+ if ((size2 = (((bytes % 100) + 5) / 10)) == 10) {
+ size1++;
+ size2 = 0;
+ }
+ (void)printf(" %3i.%1i%c", neg * size1, neg * size2,
+ "BKMGTPE"[unit]);
+ }
+ }
+}
+
+/*
* Print out status about a filesystem.
*/
void
-prtstat(struct statfs *sfsp, int maxwidth)
+prtstat(struct statfs *sfsp, int maxwidth, int base)
{
static long blocksize;
static int headerlen, timesthrough;
@@ -340,19 +391,30 @@
if (maxwidth < 11)
maxwidth = 11;
if (++timesthrough == 1) {
- if (kflag) {
- blocksize = 1024;
- header = Pflag ? "1024-blocks" : "1K-blocks";
- headerlen = strlen(header);
- } else if (mflag) {
- blocksize = 1024 * 1024;
- header = Pflag ? "1048576-blocks" : "1M-blocks";
- headerlen = strlen(header);
- } else
- header = getbsize(&headerlen, &blocksize);
- (void)printf("%-*.*s %s Used %9s Capacity",
- maxwidth, maxwidth, "Filesystem", header,
- Pflag ? "Available" : "Avail");
+ if (hflag)
+ (void)printf("%-*.*s Size Used Avail Capacity",
+ maxwidth, maxwidth, "Filesystem");
+ else {
+ if (kflag) {
+ blocksize = 1024;
+ header = Pflag ? "1024-blocks" : "1K-blocks";
+ headerlen = strlen(header);
+ } else if (mflag) {
+ blocksize = 1024 * 1024;
+ header = Pflag ? "1048576-blocks" :
+ "1M-blocks";
+ headerlen = strlen(header);
+ } else if (gflag) {
+ blocksize = 1024 * 1024 * 1024;
+ header = Pflag ? "1073741824-blocks" :
+ "1G-blocks";
+ headerlen = strlen(header);
+ } else
+ header = getbsize(&headerlen, &blocksize);
+ (void)printf("%-*.*s %s Used %9s Capacity",
+ maxwidth, maxwidth, "Filesystem", header,
+ Pflag ? "Available" : "Avail");
+ }
if (iflag)
(void)printf(" iused ifree %%iused");
(void)printf(" Mounted on\n");
@@ -360,10 +422,15 @@
(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
used = sfsp->f_blocks - sfsp->f_bfree;
availblks = sfsp->f_bavail + used;
- (void)printf(" %*ld %8ld %9ld", headerlen,
- fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
- fsbtoblk(used, sfsp->f_bsize, blocksize),
- fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
+ if (hflag) {
+ prthuman((int64_t)sfsp->f_blocks * (int64_t)sfsp->f_bsize, base);
+ prthuman((int64_t)used * (int64_t)sfsp->f_bsize, base);
+ prthuman((int64_t)sfsp->f_bavail * (int64_t)sfsp->f_bsize, base);
+ } else
+ (void)printf(" %*ld %8ld %9ld", headerlen,
+ fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize),
+ fsbtoblk(used, sfsp->f_bsize, blocksize),
+ fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
(void)printf("%7s",
availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0));
if (iflag) {
@@ -460,7 +527,7 @@
{
(void)fprintf(stderr,
- "Usage: %s [-aiklmnP] [-t type] [file | file_system ...]\n",
+ "Usage: %s [-aghHiklmnP] [-t type] [file | file_system ...]\n",
getprogname());
exit(1);
/* NOTREACHED */
--Q68bSM7Ycu6FN28Q--