diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2012-08-22 00:11:58 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2012-08-22 00:11:58 +0000 |
commit | 338926dfcc9ce2f85915c4b7d0588d91fb89660b (patch) | |
tree | be7536748ac68b11b6b382b50bdc1d428406e4d0 | |
parent | 8ea12e1a71d98a976d5102db782d6d767ff6810d (diff) |
-h flag to print human numbers in conjunction with -w -b
-rw-r--r-- | usr.bin/netstat/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/netstat/if.c | 54 | ||||
-rw-r--r-- | usr.bin/netstat/main.c | 7 | ||||
-rw-r--r-- | usr.bin/netstat/netstat.h | 3 |
4 files changed, 48 insertions, 22 deletions
diff --git a/usr.bin/netstat/Makefile b/usr.bin/netstat/Makefile index a6ebfb924f0..a94e38650a4 100644 --- a/usr.bin/netstat/Makefile +++ b/usr.bin/netstat/Makefile @@ -1,11 +1,11 @@ -# $OpenBSD: Makefile,v 1.20 2011/07/09 00:45:40 henning Exp $ +# $OpenBSD: Makefile,v 1.21 2012/08/22 00:11:57 tedu Exp $ PROG= netstat SRCS= if.c inet.c inet6.c main.c mbuf.c mroute.c route.c \ unix.c mroute6.c net80211.c show.c BINGRP= kmem BINMODE=2555 -LDADD= -lkvm -DPADD= ${LIBKVM} +LDADD= -lkvm -lutil +DPADD= ${LIBKVM} ${LIBUTIL} .include <bsd.prog.mk> diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index ad565bb7002..e88595d6888 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.64 2011/07/09 00:45:40 henning Exp $ */ +/* $OpenBSD: if.c,v 1.65 2012/08/22 00:11:57 tedu Exp $ */ /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */ /* @@ -53,6 +53,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <util.h> #include "netstat.h" @@ -311,10 +312,17 @@ hexprint: putchar(' '); break; } - if (bflag) - printf("%10llu %10llu", - ifd->ifi_ibytes, ifd->ifi_obytes); - else + if (bflag) { + if (hflag) { + char ibytes[FMT_SCALED_STRSIZE]; + char obytes[FMT_SCALED_STRSIZE]; + fmt_scaled(ifd->ifi_ibytes, ibytes); + fmt_scaled(ifd->ifi_obytes, obytes); + printf("%10s %10s", ibytes, obytes); + } else + printf("x %10llu %10llu", + ifd->ifi_ibytes, ifd->ifi_obytes); + } else printf("%8llu %5llu %8llu %5llu %5llu", ifd->ifi_ipackets, ifd->ifi_ierrors, ifd->ifi_opackets, ifd->ifi_oerrors, @@ -351,6 +359,8 @@ sidewaysintpr(unsigned int interval, int repeatcount) { sigset_t emptyset; int line; + char ibytes[FMT_SCALED_STRSIZE]; + char obytes[FMT_SCALED_STRSIZE]; fetchifs(); if (ip_cur.ift_name[0] == '\0') { @@ -393,7 +403,7 @@ banner: printf(" %5.5s", "drops"); if (bflag) - printf(" %10.10s %8.8s %10.10s %5.5s", + printf("%10.10s %8.8s %10.10s %5.5s", "bytes", " ", "bytes", " "); else printf(" %8.8s %5.5s %8.8s %5.5s %5.5s", @@ -410,11 +420,17 @@ loop: fetchifs(); - if (bflag) - printf("%10llu %8.8s %10llu %5.5s", - ip_cur.ift_ib - ip_old.ift_ib, " ", - ip_cur.ift_ob - ip_old.ift_ob, " "); - else + if (bflag) { + if (hflag) { + fmt_scaled(ip_cur.ift_ib - ip_old.ift_ib, ibytes); + fmt_scaled(ip_cur.ift_ob - ip_old.ift_ob, obytes); + printf("%10s %8.8s %10s %5.5s", + ibytes, " ", obytes, " "); + } else + printf("%10llu %8.8s %10llu %5.5s", + ip_cur.ift_ib - ip_old.ift_ib, " ", + ip_cur.ift_ob - ip_old.ift_ob, " "); + } else printf("%8llu %5llu %8llu %5llu %5llu", ip_cur.ift_ip - ip_old.ift_ip, ip_cur.ift_ie - ip_old.ift_ie, @@ -428,11 +444,17 @@ loop: ip_old = ip_cur; - if (bflag) - printf(" %10llu %8.8s %10llu %5.5s", - sum_cur.ift_ib - sum_old.ift_ib, " ", - sum_cur.ift_ob - sum_old.ift_ob, " "); - else + if (bflag) { + if (hflag) { + fmt_scaled(sum_cur.ift_ib - sum_old.ift_ib, ibytes); + fmt_scaled(sum_cur.ift_ob - sum_old.ift_ob, obytes); + printf(" %10s %8.8s %10s %5.5s", + ibytes, " ", obytes, " "); + } else + printf(" %10llu %8.8s %10llu %5.5s", + sum_cur.ift_ib - sum_old.ift_ib, " ", + sum_cur.ift_ob - sum_old.ift_ob, " "); + } else printf(" %8llu %5llu %8llu %5llu %5llu", sum_cur.ift_ip - sum_old.ift_ip, sum_cur.ift_ie - sum_old.ift_ie, diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 95e000df9c9..9037b1b3206 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.92 2011/11/01 17:30:04 mikeb Exp $ */ +/* $OpenBSD: main.c,v 1.93 2012/08/22 00:11:57 tedu Exp $ */ /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */ /* @@ -166,7 +166,7 @@ main(int argc, char *argv[]) tableid = getrtable(); while ((ch = getopt(argc, argv, - "AaBbc:dFf:gI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1) + "AaBbc:dFf:ghI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1) switch (ch) { case 'A': Aflag = 1; @@ -216,6 +216,9 @@ main(int argc, char *argv[]) case 'g': gflag = 1; break; + case 'h': + hflag = 1; + break; case 'I': iflag = 1; interface = optarg; diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 459222d9705..06bb0cafbc9 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -1,4 +1,4 @@ -/* $OpenBSD: netstat.h,v 1.62 2011/11/01 17:30:04 mikeb Exp $ */ +/* $OpenBSD: netstat.h,v 1.63 2012/08/22 00:11:57 tedu Exp $ */ /* $NetBSD: netstat.h,v 1.6 1996/05/07 02:55:05 thorpej Exp $ */ /* @@ -44,6 +44,7 @@ int bflag; /* show bytes instead of packets */ int dflag; /* show i/f dropped packets */ int Fflag; /* show routes whose gateways are in specified AF */ int gflag; /* show group (multicast) routing or stats */ +int hflag; /* print human numbers */ int iflag; /* show interfaces */ int lflag; /* show routing table with use and ref */ int mflag; /* show memory stats */ |