Subject: swapctl, -h, humanize_number(3) ?
To: None <current-users@netbsd.org>
From: Martin Weber <Ephaeton@gmx.net>
List: current-users
Date: 12/18/2003 10:26:31
--KFztAG8eRSV9hGtP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Is there any interest in using humanize_number(3) for
swapctl -l / -s ?

$ ./swapctl -hl                                                                                          
Device      Size     Used    Avail Capacity  Priority
/dev/wd0b   300M      15M     285M     5%    0
/dev/wd1b   134M     0.0B     134M     0%    1
Total       435M      15M     420M     3%

$ ./swapctl -hs 
total: 435M allocated = 15M used, 420M available.

-martin

--KFztAG8eRSV9hGtP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=swapctl-patch

Index: swapctl.8
===================================================================
RCS file: /pub/NetBSD-CVS/src/sbin/swapctl/swapctl.8,v
retrieving revision 1.26
diff -u -r1.26 swapctl.8
--- swapctl.8	2003/02/25 10:35:11	1.26
+++ swapctl.8	2003/12/18 09:22:35
@@ -57,6 +57,7 @@
 .Nm
 .Fl l | Fl s
 .Op Fl k
+.Op Fl h
 .Nm
 .Fl z
 .Nm swapon
@@ -181,6 +182,12 @@
 The
 .Fl k
 option uses 1024 byte blocks instead of the default 512 byte.
+.It Fl h
+The
+.Fl h
+option uses
+.Xr humanize_number 3
+to display the sizes.
 .It Fl t
 This flag modifies the function of the
 .Fl A
Index: swapctl.c
===================================================================
RCS file: /pub/NetBSD-CVS/src/sbin/swapctl/swapctl.c,v
retrieving revision 1.23
diff -u -r1.23 swapctl.c
--- swapctl.c	2003/10/21 02:32:54	1.23
+++ swapctl.c	2003/12/18 09:22:36
@@ -39,6 +39,7 @@
  *			or all non-block devices
  *	-a <dev>	add this device
  *	-d <dev>	remove this swap device
+ *	-h		use humanize_number(3) for listing
  *	-l		list swap devices
  *	-s		short listing of swap devices
  *	-k		use kilobytes
@@ -108,6 +109,9 @@
 int	kflag;		/* display in 1K blocks */
 #define	KFLAG_CMDS	(CMD_l | CMD_s)
 
+int	hflag;		/* display with humanize_number */
+#define HFLAG_CMDS	(CMD_l | CMD_s)
+
 int	pflag;		/* priority was specified */
 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
 
@@ -148,7 +152,7 @@
 	}
 #endif
 
-	while ((c = getopt(argc, argv, "ADUacdlkp:st:z")) != -1) {
+	while ((c = getopt(argc, argv, "ADUacdhlkp:st:z")) != -1) {
 		switch (c) {
 		case 'A':
 			SET_COMMAND(CMD_A);
@@ -174,6 +178,10 @@
 			SET_COMMAND(CMD_d);
 			break;
 
+		case 'h':
+			hflag = 1;
+			break;
+
 		case 'l':
 			SET_COMMAND(CMD_l);
 			break;
@@ -246,11 +254,11 @@
 	/* Dispatch the command. */
 	switch (command) {
 	case CMD_l:
-		list_swap(pri, kflag, pflag, 0, 1);
+		list_swap(pri, kflag, pflag, 0, 1, hflag);
 		break;
 
 	case CMD_s:
-		list_swap(pri, kflag, pflag, 0, 0);
+		list_swap(pri, kflag, pflag, 0, 0, hflag);
 		break;
 
 	case CMD_c:
@@ -568,6 +576,6 @@
 	fprintf(stderr, "       %s -a [-p priority] path\n", progname);
 	fprintf(stderr, "       %s -c -p priority path\n", progname);
 	fprintf(stderr, "       %s -d path\n", progname);
-	fprintf(stderr, "       %s -l | -s [-k]\n", progname);
+	fprintf(stderr, "       %s -l | -s [-k|-h]\n", progname);
 	exit(1);
 }
Index: swapctl.h
===================================================================
RCS file: /pub/NetBSD-CVS/src/sbin/swapctl/swapctl.h,v
retrieving revision 1.2
diff -u -r1.2 swapctl.h
--- swapctl.h	1997/10/10 05:39:54	1.2
+++ swapctl.h	2003/12/18 09:22:36
@@ -29,4 +29,4 @@
  */
 
 /* pri, kflag, pflag, tflag, dolong (1 for long, 0 for short) */
-void list_swap __P((int, int, int, int, int));
+void list_swap __P((int, int, int, int, int, int));
Index: swaplist.c
===================================================================
RCS file: /pub/NetBSD-CVS/src/sbin/swapctl/swaplist.c,v
retrieving revision 1.10
diff -u -r1.10 swaplist.c
--- swaplist.c	2003/06/23 11:53:45	1.10
+++ swaplist.c	2003/12/18 09:22:36
@@ -45,6 +45,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
+#include <util.h>
 
 #define	dbtoqb(b) dbtob((int64_t)(b))
 
@@ -55,16 +56,18 @@
 #include "swapctl.h"
 
 void
-list_swap(pri, kflag, pflag, tflag, dolong)
+list_swap(pri, kflag, pflag, tflag, dolong, hflag)
 	int	pri;
 	int	kflag;
 	int	pflag;
 	int	tflag;
 	int	dolong;
+	int	hflag;
 {
 	struct	swapent *sep, *fsep;
 	long	blocksize;
 	char	*header;
+	char	szbuf[5], usbuf[5], avbuf[5]; /* size, used, avail */
 	size_t	l;
 	int	hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
 	int	rnswap, nswap = swapctl(SWAP_NSWAP, 0, 0), i;
@@ -90,7 +93,11 @@
 			header = "1K-blocks";
 			blocksize = 1024;
 			hlen = strlen(header);
-		} else
+		} else if (hflag) {
+			header = "Size";
+			blocksize = 1; /* unused */
+			hlen = strlen(header);
+		} else 
 			header = getbsize(&hlen, &blocksize);
 		for (i = rnswap; i-- > 0; sep++)
 			if (pathmax < (l = strlen(sep->se_path)))
@@ -111,33 +118,81 @@
 		totalinuse += inuse;
 
 		if (dolong && tflag == 0) {
-			(void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
-			    (long)(dbtoqb(size) / blocksize));
-
-			(void)printf("%8ld %8ld %5.0f%%    %d\n",
-			    (long)(dbtoqb(inuse) / blocksize),
-			    (long)(dbtoqb(size - inuse) / blocksize),
-			    (double)inuse / (double)size * 100.0,
-			    sep->se_priority);
+			if (hflag == 0) {
+				(void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen,
+				    (long)(dbtoqb(size) / blocksize));
+
+				(void)printf("%8ld %8ld %5.0f%%    %d\n",
+				    (long)(dbtoqb(inuse) / blocksize),
+				    (long)(dbtoqb(size - inuse) / blocksize),
+				    (double)inuse / (double)size * 100.0,
+				    sep->se_priority);
+			} else {
+				if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(size)),
+					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+					err(1, "humanize_number");
+				if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(inuse)),
+					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+					err(1, "humanize_number");
+				if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(size-inuse)),
+					"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+					err(1, "humanize_number");
+				(void)printf("%-*s %*s ", pathmax, sep->se_path, hlen, szbuf);
+
+				(void)printf("%8s %8s %5.0f%%    %d\n",
+				    usbuf, avbuf, 
+				    (double)inuse / (double)size * 100.0,
+				    sep->se_priority);
+			}
 		}
 	}
 	if (tflag)
 		(void)printf("%dM/%dM swap space\n",
 		    (int)(dbtoqb(totalinuse) / (1024 * 1024)),
 		    (int)(dbtoqb(totalsize) / (1024 * 1024)));
-	else if (dolong == 0)
+	else if (dolong == 0) {
+		if (hflag) {
+			if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			(void)printf("total: %s allocated = %s used, %s available.\n",
+				szbuf, usbuf, avbuf);
+		} else {
 		    printf("total: %ldk bytes allocated = %ldk used, "
 			   "%ldk available\n",
 		    (long)(dbtoqb(totalsize) / 1024),
 		    (long)(dbtoqb(totalinuse) / 1024),
 		    (long)(dbtoqb(totalsize - totalinuse) / 1024));
-	else if (ncounted > 1)
-		(void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
-		    hlen,
-		    (long)(dbtoqb(totalsize) / blocksize),
-		    (long)(dbtoqb(totalinuse) / blocksize),
-		    (long)(dbtoqb(totalsize - totalinuse) / blocksize),
-		    (double)(totalinuse) / (double)totalsize * 100.0);
+		}
+	} else if (ncounted > 1) {
+		if (hflag) {
+			if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			if ((humanize_number(avbuf, sizeof(avbuf), (dbtoqb(totalsize-totalinuse)),
+				"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
+				err(1, "humanize_number");
+			(void)printf("%-*s %*s %8s %8s %5.0f%%\n", pathmax, "Total",
+				hlen, szbuf, usbuf, avbuf, 
+			        (double)(totalinuse) / (double)totalsize * 100.0);
+		} else {
+			(void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total",
+			    hlen,
+			    (long)(dbtoqb(totalsize) / blocksize),
+			    (long)(dbtoqb(totalinuse) / blocksize),
+			    (long)(dbtoqb(totalsize - totalinuse) / blocksize),
+			    (double)(totalinuse) / (double)totalsize * 100.0);
+		}
+	}
 	if (fsep)
 		(void)free(fsep);
 }

--KFztAG8eRSV9hGtP--