Subject: ifconfig -h patch
To: None <tech-userlevel@NetBSD.org>
From: Rui Paulo <rpaulo@NetBSD.org>
List: tech-userlevel
Date: 08/06/2005 19:31:08
--aVD9QWMuhilNxW9f
Content-Type: multipart/mixed; boundary="k1lZvvs/B4yU6o8G"
Content-Disposition: inline
--k1lZvvs/B4yU6o8G
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Hi,
The following small patch adds the '-h' option to ifconfig(8). As you may
have already gussed, it prints the byte statistics of a given interface
in human-readable format. This is something that I miss often, but
I don't know how many people would want this. Note that I'm not breaking
anything (I guess): it's a new flag and scripts that already depend on
the old -v format will work.
An example output (addresses removed):
> ifconfig -v ex0
ex0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
capabilities=3f00<IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx,UDP4CSUM_Rx,UDP4CSUM_Tx>
enabled=3f00<IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx,UDP4CSUM_Rx,UDP4CSUM_Tx>
address: 00:00:00:00:00:00
media: Ethernet autoselect (100baseTX full-duplex)
status: active
input: 4701561 packets, 6885740641 bytes, 11206 multicasts
output: 6499381 packets, 1615921802 bytes, 473 multicasts
inet 0.0.0.0 netmask 0xffffffff broadcast 255.255.255.255
> ifconfig -hv ex0
ex0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
capabilities=3f00<IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx,UDP4CSUM_Rx,UDP4CSUM_Tx>
enabled=3f00<IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx,UDP4CSUM_Rx,UDP4CSUM_Tx>
address: 00:00:00:00:00:00
media: Ethernet autoselect (100baseTX full-duplex)
status: active
input: 4701585 packets, 6.4G bytes, 11208 multicasts
output: 6499405 packets, 1.5G bytes, 473 multicasts
inet 0.0.0.0 netmask 0xffffffff broadcast 255.255.255.255
The patch is attached. I'm not ignoring the return value of printf()
because the rest of the file doesn't do it. Should we fix that ?
-- Rui Paulo
--k1lZvvs/B4yU6o8G
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="ifconfig-h.diff"
Content-Transfer-Encoding: quoted-printable
Index: ifconfig.8
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/sbin/ifconfig/ifconfig.8,v
retrieving revision 1.78
diff -u -r1.78 ifconfig.8
--- ifconfig.8 2 May 2005 15:37:06 -0000 1.78
+++ ifconfig.8 6 Aug 2005 18:11:54 -0000
@@ -29,7 +29,7 @@
.\"
.\" @(#)ifconfig.8 8.4 (Berkeley) 6/1/94
.\"
-.Dd May 3, 2005
+.Dd August 6, 2005
.Dt IFCONFIG 8
.Os
.Sh NAME
@@ -44,12 +44,12 @@
.Oc
.Op Ar parameters
.Nm
-.Op Fl Lmvz
+.Op Fl hLmvz
.Ar interface
.Op Ar protocol_family
.Nm
.Fl a
-.Op Fl bdLmsuvz
+.Op Fl bdhLmsuvz
.Op Ar protocol_family
.Nm
.Fl l
@@ -667,6 +667,11 @@
.Fl v
flag prints statistics on packets sent and received on the given
interface.
+If
+.Fl h
+is used in conjunction with
+.Fl v ,
+the byte statistics will be printed in "human-readable" format.
The
.Fl z
flag is identical to the
Index: ifconfig.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.168
diff -u -r1.168 ifconfig.c
--- ifconfig.c 2 May 2005 15:35:16 -0000 1.168
+++ ifconfig.c 6 Aug 2005 18:11:54 -0000
@@ -135,6 +135,7 @@
int conflicting =3D 0;
int af;
int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
+int hflag;
#ifdef INET6
int Lflag;
#endif
@@ -361,7 +362,7 @@
=20
/* Parse command-line options */
aflag =3D mflag =3D vflag =3D zflag =3D 0;
- while ((ch =3D getopt(argc, argv, "AabCdlmsuvz"
+ while ((ch =3D getopt(argc, argv, "AabCdhlmsuvz"
#ifdef INET6
"L"
#endif
@@ -386,7 +387,9 @@
case 'd':
dflag =3D 1;
break;
-
+ case 'h':
+ hflag =3D 1;
+ break;
#ifdef INET6
case 'L':
Lflag =3D 1;
@@ -1414,12 +1417,23 @@
err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
} else {
struct if_data * const ifi =3D &ifdr.ifdr_data;
+ char buf[5];
+
#define PLURAL(n) ((n) =3D=3D 1 ? "" : "s")
- printf("\tinput: %llu packet%s, %llu byte%s",
+#define PLURALSTR(s) ((atof(s)) =3D=3D 1.0 ? "" : "s")
+ printf("\tinput: %llu packet%s, ",=20
(unsigned long long) ifi->ifi_ipackets,
- PLURAL(ifi->ifi_ipackets),
- (unsigned long long) ifi->ifi_ibytes,
- PLURAL(ifi->ifi_ibytes));
+ PLURAL(ifi->ifi_ipackets));
+ if (hflag) {
+ (void) humanize_number(buf, sizeof(buf),
+ (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,=20
+ HN_NOSPACE | HN_DECIMAL);
+ printf("%s byte%s", buf,
+ PLURALSTR(buf));
+ } else
+ printf("%llu byte%s",
+ (unsigned long long) ifi->ifi_ibytes,
+ PLURAL(ifi->ifi_ibytes));
if (ifi->ifi_imcasts)
printf(", %llu multicast%s",
(unsigned long long) ifi->ifi_imcasts,
@@ -1435,11 +1449,19 @@
if (ifi->ifi_noproto)
printf(", %llu unknown protocol",
(unsigned long long) ifi->ifi_noproto);
- printf("\n\toutput: %llu packet%s, %llu byte%s",
+ printf("\n\toutput: %llu packet%s, ",
(unsigned long long) ifi->ifi_opackets,
- PLURAL(ifi->ifi_opackets),
- (unsigned long long) ifi->ifi_obytes,
- PLURAL(ifi->ifi_obytes));
+ PLURAL(ifi->ifi_opackets));
+ if (hflag) {
+ (void) humanize_number(buf, sizeof(buf),
+ (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
+ HN_NOSPACE | HN_DECIMAL);
+ printf("%s byte%s", buf,
+ PLURALSTR(buf));
+ } else
+ printf("%llu byte%s",
+ (unsigned long long) ifi->ifi_obytes,
+ PLURAL(ifi->ifi_obytes));
if (ifi->ifi_omcasts)
printf(", %llu multicast%s",
(unsigned long long) ifi->ifi_omcasts,
@@ -1454,6 +1476,7 @@
PLURAL(ifi->ifi_collisions));
printf("\n");
#undef PLURAL
+#undef PLURALSTR
}
=20
ieee80211_statistics();
@@ -1481,7 +1504,7 @@
const char *progname =3D getprogname();
=20
fprintf(stderr,
- "usage: %s [-m] [-v] [-z] "
+ "usage: %s [-h] [-m] [-v] [-z] "
#ifdef INET6
"[-L] "
#endif
@@ -1500,7 +1523,7 @@
"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
- " %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
+ " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
" %s -l [-b] [-d] [-u] [-s]\n"
" %s -C\n"
" %s interface create\n"
--k1lZvvs/B4yU6o8G--
--aVD9QWMuhilNxW9f
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (NetBSD)
iD8DBQFC9QHrZPqyxs9FH4QRApQ2AJ9plyxSIVvRohgea0UsCyW5c4WPEgCfekxf
DOJc+x0aQIpyXtwppwUnZa4=
=aBHq
-----END PGP SIGNATURE-----
--aVD9QWMuhilNxW9f--