Module Name:    src
Committed By:   pooka
Date:           Wed Feb 24 11:00:28 UTC 2010

Modified Files:
        src/usr.bin/netstat: if.c main.c netstat.1 netstat.h

Log Message:
Add -h, which makes output of bytes counts "humanized" (e.g. -bih)

(netstat had -h some 15 years ago, but since then it has been just
a fancy way of calling usage())


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/netstat/if.c
cvs rdiff -u -r1.73 -r1.74 src/usr.bin/netstat/main.c
cvs rdiff -u -r1.54 -r1.55 src/usr.bin/netstat/netstat.1
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/netstat/netstat.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/netstat/if.c
diff -u src/usr.bin/netstat/if.c:1.67 src/usr.bin/netstat/if.c:1.68
--- src/usr.bin/netstat/if.c:1.67	Sun Sep 27 18:19:18 2009
+++ src/usr.bin/netstat/if.c	Wed Feb 24 11:00:27 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.c,v 1.67 2009/09/27 18:19:18 plunky Exp $	*/
+/*	$NetBSD: if.c,v 1.68 2010/02/24 11:00:27 pooka Exp $	*/
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from: @(#)if.c	8.2 (Berkeley) 2/21/94";
 #else
-__RCSID("$NetBSD: if.c,v 1.67 2009/09/27 18:19:18 plunky Exp $");
+__RCSID("$NetBSD: if.c,v 1.68 2010/02/24 11:00:27 pooka Exp $");
 #endif
 #endif /* not lint */
 
@@ -68,6 +68,8 @@
 
 #define	MAXIF	100
 
+#define HUMBUF_SIZE 7
+
 struct	iftot {
 	char ift_name[IFNAMSIZ];	/* interface name */
 	u_quad_t ift_ip;		/* input packets */
@@ -523,9 +525,19 @@
 	}
 
 	if (bflag) {
-		printf("%10llu %10llu", 
-			(unsigned long long)ifd->ifi_ibytes,
-			(unsigned long long)ifd->ifi_obytes);
+		char humbuf[HUMBUF_SIZE];
+
+		if (hflag && humanize_number(humbuf, sizeof(humbuf),
+		    ifd->ifi_ibytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+			printf("%10s ", humbuf);
+		else
+			printf("%10llu ", (unsigned long long)ifd->ifi_ibytes);
+
+		if (hflag && humanize_number(humbuf, sizeof(humbuf),
+		    ifd->ifi_obytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+			printf("%10s", humbuf);
+		else
+			printf("%10llu", (unsigned long long)ifd->ifi_obytes);
 	} else {
 		printf("%8llu %5llu %8llu %5llu %5llu",
 			(unsigned long long)ifd->ifi_ipackets,
@@ -793,11 +805,27 @@
 		}
 		if (ip == interesting) {
 			if (bflag) {
-				printf("%10llu %8.8s %10llu %5.5s",
-				    (unsigned long long)(ifnet.if_ibytes -
-					ip->ift_ib), " ",
-				    (unsigned long long)(ifnet.if_obytes -
-					ip->ift_ob), " ");
+				char humbuf[HUMBUF_SIZE];
+
+				if (hflag && humanize_number(humbuf,
+				    sizeof(humbuf),
+				    ifnet.if_ibytes - ip->ift_ib, "",
+				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+					printf("%10s %8.8s ", humbuf, " ");
+				else
+					printf("%10llu %8.8s ", 
+					    (unsigned long long)
+					    (ifnet.if_ibytes-ip->ift_ib), " ");
+
+				if (hflag && humanize_number(humbuf,
+				    sizeof(humbuf),
+				    ifnet.if_obytes - ip->ift_ob, "",
+				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+					printf("%10s %5.5s", humbuf, " ");
+				else
+					printf("%10llu %5.5s", 
+					    (unsigned long long)
+					    (ifnet.if_obytes-ip->ift_ob), " ");
 			} else {
 				printf("%8llu %5llu %8llu %5llu %5llu",
 				    (unsigned long long)
@@ -836,11 +864,25 @@
 	}
 	if (lastif - iftot > 0) {
 		if (bflag) {
-			printf("  %10llu %8.8s %10llu %5.5s",
-			    (unsigned long long)
-				(sum->ift_ib - total->ift_ib), " ",
-			    (unsigned long long)
-				(sum->ift_ob - total->ift_ob), " ");
+			char humbuf[HUMBUF_SIZE];
+
+			if (hflag && humanize_number(humbuf,
+			    sizeof(humbuf), sum->ift_ib - total->ift_ib, "",
+			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+				printf("%10s %8.8s ", humbuf, " ");
+			else
+				printf("%10llu %8.8s ", 
+				    (unsigned long long)
+				    (sum->ift_ib - total->ift_ib), " ");
+
+			if (hflag && humanize_number(humbuf,
+			    sizeof(humbuf), sum->ift_ob -  total->ift_ob, "",
+			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
+				printf("%10s %5.5s", humbuf, " ");
+			else
+				printf("%10llu %5.5s", 
+				    (unsigned long long)
+				    (sum->ift_ob - total->ift_ob), " ");
 		} else {
 			printf("  %8llu %5llu %8llu %5llu %5llu",
 			    (unsigned long long)

Index: src/usr.bin/netstat/main.c
diff -u src/usr.bin/netstat/main.c:1.73 src/usr.bin/netstat/main.c:1.74
--- src/usr.bin/netstat/main.c:1.73	Mon Sep 14 10:36:51 2009
+++ src/usr.bin/netstat/main.c	Wed Feb 24 11:00:27 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.73 2009/09/14 10:36:51 degroote Exp $	*/
+/*	$NetBSD: main.c,v 1.74 2010/02/24 11:00:27 pooka Exp $	*/
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "from: @(#)main.c	8.4 (Berkeley) 3/1/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.73 2009/09/14 10:36:51 degroote Exp $");
+__RCSID("$NetBSD: main.c,v 1.74 2010/02/24 11:00:27 pooka Exp $");
 #endif
 #endif /* not lint */
 
@@ -471,6 +471,9 @@
 			gflag = 1;
 			break;
 #endif
+		case 'h':
+			hflag = 1;
+			break;
 		case 'I':
 			iflag = 1;
 			interface = optarg;

Index: src/usr.bin/netstat/netstat.1
diff -u src/usr.bin/netstat/netstat.1:1.54 src/usr.bin/netstat/netstat.1:1.55
--- src/usr.bin/netstat/netstat.1:1.54	Sun Sep 13 09:17:26 2009
+++ src/usr.bin/netstat/netstat.1	Wed Feb 24 11:00:27 2010
@@ -1,4 +1,4 @@
-.\"	$NetBSD: netstat.1,v 1.54 2009/09/13 09:17:26 wiz Exp $
+.\"	$NetBSD: netstat.1,v 1.55 2010/02/24 11:00:27 pooka Exp $
 .\"
 .\" Copyright (c) 1983, 1990, 1992, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)netstat.1	8.8 (Berkeley) 4/18/94
 .\"
-.Dd September 12, 2009
+.Dd February 24, 2009
 .Dt NETSTAT 1
 .Os
 .Sh NAME
@@ -42,7 +42,7 @@
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Nm
-.Op Fl bdgiLmnqrSsv
+.Op Fl bdghiLmnqrSsv
 .Op Fl f Ar address_family
 .Op Fl M Ar core
 .Op Fl N Ar system
@@ -181,6 +181,14 @@
 or
 .Ar protocol ,
 respectively.
+.It Fl h
+When used with
+.Fl b
+in combination with either
+.Fl i
+or
+.Fl I ,
+output "human-readable" byte counts.
 .It Fl i
 Show the state of interfaces which have been auto-configured
 (interfaces statically configured into a system, but not

Index: src/usr.bin/netstat/netstat.h
diff -u src/usr.bin/netstat/netstat.h:1.39 src/usr.bin/netstat/netstat.h:1.40
--- src/usr.bin/netstat/netstat.h:1.39	Mon Sep 14 10:36:51 2009
+++ src/usr.bin/netstat/netstat.h	Wed Feb 24 11:00:27 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: netstat.h,v 1.39 2009/09/14 10:36:51 degroote Exp $	*/
+/*	$NetBSD: netstat.h,v 1.40 2010/02/24 11:00:27 pooka Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -41,6 +41,7 @@
 #ifndef SMALL
 int	gflag;		/* show group (multicast) routing or stats */
 #endif
+int	hflag;		/* humanize byte counts */
 int	iflag;		/* show interfaces */
 int	Lflag;		/* don't show LLINFO entries */
 int	lflag;		/* show routing table with use and ref */

Reply via email to