Module Name:    src
Committed By:   martin
Date:           Thu Jan 12 12:07:23 UTC 2023

Modified Files:
        src/usr.bin/netstat [netbsd-10]: if.c

Log Message:
Pull up following revision(s) (requested by mrg in ticket #42):

        usr.bin/netstat/if.c: revision 1.109

if not given an interface to monitor by default, pick the one
with the most bytes in/out the first time and keep using it.

in addition to picking the most likely intersting interface,
this actually fixes a bug where interfaces coming/going in
the middle of eg, "netstat -b -w 1", may end up showing the
data for another interface on any output line (including the
the header declaring the interface!)


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.108.2.1 src/usr.bin/netstat/if.c

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.108 src/usr.bin/netstat/if.c:1.108.2.1
--- src/usr.bin/netstat/if.c:1.108	Mon Dec 12 05:09:33 2022
+++ src/usr.bin/netstat/if.c	Thu Jan 12 12:07:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.c,v 1.108 2022/12/12 05:09:33 msaitoh Exp $	*/
+/*	$NetBSD: if.c,v 1.108.2.1 2023/01/12 12:07:23 martin 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.108 2022/12/12 05:09:33 msaitoh Exp $");
+__RCSID("$NetBSD: if.c,v 1.108.2.1 2023/01/12 12:07:23 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -1171,7 +1171,11 @@ fetchifs(void)
 
 			if_data_ext_get(name, &dext);
 
-			if (interface != NULL && !strcmp(name, interface)) {
+			if ((interface != NULL && !strcmp(name, interface)) ||
+			    (interface == NULL &&
+			    ((ip_cur.ift_ib + ip_cur.ift_ob) == 0 ||
+			     (ip_cur.ift_ib + ip_cur.ift_ob <
+			     ifd->ifi_ibytes + ifd->ifi_obytes)))) {
 				strlcpy(ip_cur.ift_name, name,
 				    sizeof(ip_cur.ift_name));
 				ip_cur.ift_ip = ifd->ifi_ipackets;
@@ -1197,17 +1201,11 @@ fetchifs(void)
 			break;
 		}
 	}
-	if (interface == NULL) {
-		strlcpy(ip_cur.ift_name, name,
-		    sizeof(ip_cur.ift_name));
-		ip_cur.ift_ip = ifd->ifi_ipackets;
-		ip_cur.ift_ib = ifd->ifi_ibytes;
-		ip_cur.ift_ie = ifd->ifi_ierrors;
-		ip_cur.ift_op = ifd->ifi_opackets;
-		ip_cur.ift_ob = ifd->ifi_obytes;
-		ip_cur.ift_oe = ifd->ifi_oerrors;
-		ip_cur.ift_co = ifd->ifi_collisions;
-		ip_cur.ift_iq = ifd->ifi_iqdrops;
-		ip_cur.ift_oq = dext.ifi_oqdrops;
-	}
+
+	/*
+	 * If we picked an interface, be sure to keep using it for the rest
+	 * of this instance.
+	 */
+	if (interface == NULL)
+		interface = ip_cur.ift_name;
 }

Reply via email to