git: 4cd1e685e823 - stable/12 - netstat -i: do not truncate interface names

From: Mike Karels <karels_at_FreeBSD.org>
Date: Wed, 13 Jul 2022 13:23:30 UTC
The branch stable/12 has been updated by karels:

URL: https://cgit.FreeBSD.org/src/commit/?id=4cd1e685e8232c4b13552363b16f8531cb32d450

commit 4cd1e685e8232c4b13552363b16f8531cb32d450
Author:     Mike Karels <karels@FreeBSD.org>
AuthorDate: 2022-07-02 16:03:36 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2022-07-13 13:23:13 +0000

    netstat -i: do not truncate interface names
    
    The field for interface names for netstat -i was 5 characters by
    default, which is no longer sufficient with names like "vlan1234"
    and "vtnet0".  netstat -iW computed the necessary field width, but
    also enlarged the address field by a lot (especially with IPv6 enabled).
    Make netstat -i compute the field width for interface names with or
    without -W.  Note that the existing default output does not fit in
    80 columns in any case.  Update the man page accordingly, documenting
    the remaining effect of -W with -i.  Also add -W to the list of
    General Options, as there are numerous pointers to this.
    
    Reported by:    Chris Ross
    Reviewed by:    melifaro, rgrimes, cy
    Differential Revision: https://reviews.freebsd.org/D35703
    
    (cherry picked from commit 231092bb6fda75b922fc324edef6523e78e9e8ed)
---
 usr.bin/netstat/if.c      | 26 +++++++++++++-------------
 usr.bin/netstat/netstat.1 |  8 +++++++-
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c
index d9a7574c9e22..5ba4f3afc67b 100644
--- a/usr.bin/netstat/if.c
+++ b/usr.bin/netstat/if.c
@@ -284,20 +284,20 @@ intpr(void (*pfunc)(char *), int af)
 	if (aflag && getifmaddrs(&ifmap) != 0)
 		err(EX_OSERR, "getifmaddrs");
 
+	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+		if (interface != NULL &&
+		    strcmp(ifa->ifa_name, interface) != 0)
+			continue;
+		if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
+			continue;
+		ifn_len = strlen(ifa->ifa_name);
+		if ((ifa->ifa_flags & IFF_UP) == 0)
+			++ifn_len;
+		ifn_len_max = MAX(ifn_len_max, ifn_len);
+		if (ifa->ifa_addr->sa_family == AF_INET6)
+			has_ipv6 = 1;
+	}
 	if (Wflag) {
-		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-			if (interface != NULL &&
-			    strcmp(ifa->ifa_name, interface) != 0)
-				continue;
-			if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
-				continue;
-			ifn_len = strlen(ifa->ifa_name);
-			if ((ifa->ifa_flags & IFF_UP) == 0)
-				++ifn_len;
-			ifn_len_max = MAX(ifn_len_max, ifn_len);
-			if (ifa->ifa_addr->sa_family == AF_INET6)
-				has_ipv6 = 1;
-		}
 		if (has_ipv6) {
 			net_len = 24;
 			addr_len = 39;
diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1
index b2ef71da93af..a675afa17f61 100644
--- a/usr.bin/netstat/netstat.1
+++ b/usr.bin/netstat/netstat.1
@@ -314,7 +314,8 @@ Do not resolve numeric addresses and port numbers to names.
 See
 .Sx GENERAL OPTIONS .
 .It Fl W
-Avoid truncating interface names even if this causes some fields to overflow.
+Avoid truncating addresses even if this causes some fields to overflow.
+See
 .Sx GENERAL OPTIONS .
 .It Fl f Ar protocol_family
 Filter by
@@ -801,6 +802,11 @@ Normally
 .Nm
 attempts to resolve addresses and ports,
 and display them symbolically.
+.It Fl W
+Wider output; expand address fields, etc, to avoid truncation.
+Non-numeric values such as domain names may still be truncated; use the
+.Fl n
+option if necessary to avoid ambiguity.
 .El
 .Sh SEE ALSO
 .Xr fstat 1 ,