diff options
| author | pooka <pooka@NetBSD.org> | 2010-02-24 11:00:27 +0000 |
|---|---|---|
| committer | pooka <pooka@NetBSD.org> | 2010-02-24 11:00:27 +0000 |
| commit | bc1cf39b94f8dccb618d7db00a74615434ec3cd8 (patch) | |
| tree | f76c00cb812c8e9851733f56a7b36927b7d01a3c /usr.bin/netstat | |
| parent | 2897183305a2e3ab73574b3a0e558a1ac06fafaa (diff) | |
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())
Diffstat (limited to 'usr.bin/netstat')
| -rw-r--r-- | usr.bin/netstat/if.c | 72 | ||||
| -rw-r--r-- | usr.bin/netstat/main.c | 7 | ||||
| -rw-r--r-- | usr.bin/netstat/netstat.1 | 14 | ||||
| -rw-r--r-- | usr.bin/netstat/netstat.h | 3 |
4 files changed, 75 insertions, 21 deletions
diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index c7d2d0c6869..b295cb5e213 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -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 @@ __RCSID("$NetBSD: if.c,v 1.67 2009/09/27 18:19:18 plunky Exp $"); #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 @@ print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd) } 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 @@ loop: } 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 @@ loop: } 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) diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 3cefd4ce83b..7d7ff11df90 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\ #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 @@ main(argc, argv) gflag = 1; break; #endif + case 'h': + hflag = 1; + break; case 'I': iflag = 1; interface = optarg; diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index c77dc5f5cfa..32b34fabf46 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -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 @@ for the specified 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 diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index ff3ed125e2c..078f4329bd7 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -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 @@ int dflag; /* show i/f dropped packets */ #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 */ |
