Subject: bin/12090: Human-readable sizes for df
To: None <gnats-bugs@gnats.netbsd.org>
From: None <mark.white@st-edmund-hall.oxford.ac.uk>
List: netbsd-bugs
Date: 01/31/2001 08:41:23
>Number:         12090
>Category:       bin
>Synopsis:       Patch to add size display in megabytes etc. to df
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Jan 31 08:44:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Mark White <mark.white@seh.ox.ac.uk>
>Release:        patch against 1.5 release; should work on -current
>Organization:
n/a
>Environment:
K6-2/550 with 128Mb
System: NetBSD lorien 1.5 NetBSD 1.5 (LORIEN) #16: Tue Jan 9 03:46:38 GMT 2001 mark@lorien:/usr/src/sys/arch/i386/compile/LORIEN i386


>Description:

    Below is a patch to add a -h feature to NetBSD df, producing sizes
    in suitable [maybe fractional] units -- gigabtyes, megabytes etc.
    This is based on the -h feature of some of the GNU file utilities;
    for the very large numbers of blocks in typical df output, it
    saves a lot of digit counting.  

    Recently several people from port-i386 suggested send-pr'ing this
    patch.  This feature would probably be fairly useful in du and ls
    as well; if other people think so, I'll try to get around to
    implementing it in a uniform way there, too.

>How-To-Repeat:
>Fix:

Unified diff patch is below:

--- snip -------------------------------------------------------------

The following patch adds -h option to NetBSD df.  df -h will produce
the following sort of output:

    Filesystem   Total   Used  Avail Capacity  Mounted on
    /dev/wd0a     267M   108M   145M    42%    /
    /dev/wd1a    2.21G  1.63G   482M    77%    /usr
    kernfs          1k     1k     0k   100%    /kern
    [...]

ie. the output is a bit easier for humans to read, using megabytes and
so on instead of very large numbers of 1024- or 512-kilobyte blocks.

This is based on the -h feature of several of the GNU file utilities
(although only on my memory of using the feature; the code and output
format are probably both different).  It will probably be regarded by
BSD purists as bloat.  :-)

Apply the patch in /usr/src/bin/df; it contains updates for the source
and manpage.

Mark <>< / mark.white@seh.ox.ac.uk

--- df.orig.c	Thu Jan  4 16:58:03 2001
+++ df.c	Thu Jan  4 20:17:53 2001
@@ -38,6 +38,11 @@
  * SUCH DAMAGE.
  */
 
+/*
+ * Modified:
+ *	MJ White	04 Jan 2001	-h option (humanised sizes)
+ */
+
 #include <sys/cdefs.h>
 #ifndef lint
 __COPYRIGHT(
@@ -73,13 +78,14 @@
 int	 bread __P((off_t, void *, int));
 char	*getmntpt __P((char *));
 void	 prtstat __P((struct statfs *, int));
+char	*human __P((long int, char *));
 int	 ufs_df __P((char *, struct statfs *));
 int	 selected __P((const char *));
 void	 maketypelist __P((char *));
 long	 regetmntinfo __P((struct statfs **, long));
 void	 usage __P((void));
 
-int	iflag, kflag, lflag, nflag;
+int	iflag, kflag, lflag, nflag, hflag;
 char	**typelist = NULL;
 struct	ufs_args mdev;
 
@@ -94,7 +100,7 @@
 	int ch, i, maxwidth, width;
 	char *mntpt;
 
-	while ((ch = getopt(argc, argv, "iklnt:")) != -1)
+	while ((ch = getopt(argc, argv, "ikhlnt:")) != -1)
 		switch (ch) {
 		case 'i':
 			iflag = 1;
@@ -102,6 +108,9 @@
 		case 'k':
 			kflag = 1;
 			break;
+		case 'h':
+			hflag = 1;
+			break;
 		case 'l':
 			lflag = 1;
 			break;
@@ -334,6 +343,7 @@
 	static char *header;
 	long used, availblks, inodes;
 	static char     *full = "100%";
+	char *hbuf;
 
 	if (maxwidth < 11)
 		maxwidth = 11;
@@ -344,19 +354,42 @@
 			headerlen = strlen(header);
 		} else
 			header = getbsize(&headerlen, &blocksize);
-		(void)printf("%-*.*s %s     Used    Avail Capacity",
-		    maxwidth, maxwidth, "Filesystem", header);
+		if (hflag) {
+			blocksize = 1024;
+			header = " Total";
+			headerlen = strlen(header);
+		}		
+		if (hflag) {
+		    (void)printf("%-*.*s %s   Used  Avail Capacity",
+			maxwidth, maxwidth, "Filesystem", header);
+		} else {
+		    (void)printf("%-*.*s %s     Used    Avail Capacity",
+			maxwidth, maxwidth, "Filesystem", header);
+		}
 		if (iflag)
 			(void)printf(" iused   ifree  %%iused");
 		(void)printf("  Mounted on\n");
 	}
+
+	hbuf = (char*) malloc (sizeof(char) *
+	    ( (headerlen > 9) ? (headerlen + 1) : 9 ) );
+	
 	(void)printf("%-*.*s", maxwidth, maxwidth, sfsp->f_mntfromname);
 	used = sfsp->f_blocks - sfsp->f_bfree;
 	availblks = sfsp->f_bavail + used;
-	(void)printf(" %*ld %8ld %8ld", 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) {
+	    (void)printf(" %*s", headerlen,
+		human(fsbtoblk(sfsp->f_blocks, sfsp->f_bsize, blocksize), hbuf));
+	    (void)printf(" %5s",
+		human(fsbtoblk(used, sfsp->f_bsize, blocksize), hbuf));
+	    (void)printf(" %5s",
+		human(fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize), hbuf));
+	} else {
+	    (void)printf(" %*ld %8ld %8ld", 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(" %6s",
 	    availblks == 0 ? full : strpct((u_long)used, (u_long)availblks, 0));
 	if (iflag) {
@@ -367,6 +400,45 @@
 	} else 
 		(void)printf("  ");
 	(void)printf("  %s\n", sfsp->f_mntonname);
+
+	free(hbuf);
+}
+
+/*
+ * Return a 'humanised' version of block count
+ */
+char *
+human(kblks, hbuf)
+    long int kblks;
+    char * hbuf;
+{
+    if (kblks >= (1024*1024*100)) {	/* 100G */
+	sprintf(hbuf, "%5ldG", kblks / (1024 * 1024));
+	return hbuf;
+    }
+    if (kblks >= (1024*1024*10)) {	/* 10G */
+	sprintf(hbuf, "%5.1fG", ((float)kblks / (1024.0 * 1024.0)));
+	return hbuf;
+    }
+    if (kblks >= (1024*1024)) {		/* 1G */
+	sprintf(hbuf, "%5.2fG", ((float)kblks / (1024.0 * 1024.0)));
+	return hbuf;
+    }
+    if (kblks >= (1024*100)) {		/* 100M */
+	sprintf(hbuf, "%5ldM", kblks / 1024);
+	return hbuf;
+    }
+    if (kblks >= (1024*10)) {		/* 10M */
+	sprintf(hbuf, "%5.1fM", ((float)kblks / 1024.0));
+	return hbuf;
+    }
+    if (kblks >= (1024)) {		/* 1M */
+	sprintf(hbuf, "%5.2fM", ((float)kblks / 1024.0));
+	return hbuf;
+    }
+    /* otherwise, just print number of k */
+    sprintf(hbuf, "%5ldk", kblks);
+    return hbuf;
 }
 
 /*
@@ -451,7 +523,7 @@
 void
 usage()
 {
-	(void)fprintf(stderr, "usage: df [-ikln] [-t type] [file | file_system ...]\n");
+	(void)fprintf(stderr, "usage: df [-ikhln] [-t type] [file | file_system ...]\n");
 	exit(1);
 	/* NOTREACHED */
 }
--- df.orig.1	Thu Jan  4 17:48:04 2001
+++ df.1	Thu Jan  4 20:00:22 2001
@@ -41,7 +41,7 @@
 .Nd display free disk space
 .Sh SYNOPSIS
 .Nm
-.Op Fl ikln
+.Op Fl ikhln
 .Op Fl t Ar type
 .Op Ar file | Ar file_system ...
 .Sh DESCRIPTION
@@ -77,6 +77,10 @@
 The
 .Fl k
 option causes the numbers to be reported in kilobyte counts.
+.It Fl h
+Causes sizes to be reported in appropriate manageable human units, such as
+megabytes.  1k (kilobyte) = 1024 bytes; 1M (megabyte) = 1024k; 1G (gigabyte) =
+1024M.
 .It Fl l
 Display statistics only about mounted file systems with the MNT_LOCAL
 flag set.  If a non-local file system is given as an argument, a
@@ -109,7 +113,9 @@
 .Ev BLOCKSIZE
 is set, and the
 .Fl k
-option is not specified, the block counts will be displayed in units of that
+and
+.Fl h
+options are not specified, the block counts will be displayed in units of that
 size block.
 .El
 .Sh SEE ALSO
@@ -128,3 +134,8 @@
 .Nm
 utility appeared in
 .At v6 .
+.Pp
+MJ White added the 
+.Fl h
+size display to NetBSD 1.5 on lorien, after finding
+a similar feature of the GNU file utilities particularly useful in df.
>Release-Note:
>Audit-Trail:
>Unformatted: