Module Name:    src
Committed By:   mrg
Date:           Sun Sep 20 00:30:04 UTC 2015

Modified Files:
        src/usr.bin/netstat: if.c

Log Message:
query the window size and use it instead of assuming 24 lines.
now the header isn't re-printed a lot of times in tall windows.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 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.81 src/usr.bin/netstat/if.c:1.82
--- src/usr.bin/netstat/if.c:1.81	Wed Jan  7 22:43:01 2015
+++ src/usr.bin/netstat/if.c	Sun Sep 20 00:30:04 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.c,v 1.81 2015/01/07 22:43:01 christos Exp $	*/
+/*	$NetBSD: if.c,v 1.82 2015/09/20 00:30:04 mrg 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.81 2015/01/07 22:43:01 christos Exp $");
+__RCSID("$NetBSD: if.c,v 1.82 2015/09/20 00:30:04 mrg Exp $");
 #endif
 #endif /* not lint */
 
@@ -44,6 +44,7 @@ __RCSID("$NetBSD: if.c,v 1.81 2015/01/07
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/sysctl.h>
+#include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -82,6 +83,7 @@ struct	iftot {
 	int ift_dr;			/* drops */
 };
 
+static void set_lines(void);
 static void print_addr(struct sockaddr *, struct sockaddr **, struct if_data *,
     struct ifnet *);
 static void sidewaysintpr(u_int, u_long);
@@ -100,6 +102,22 @@ static void intpr_kvm(u_long, void (*)(c
 struct iftot iftot[MAXIF], ip_cur, ip_old, sum_cur, sum_old;
 bool	signalled;			/* set if alarm goes off "early" */
 
+static unsigned redraw_lines = 21;
+
+static void
+set_lines(void)
+{
+	static bool first = true;
+	struct ttysize ts;
+
+	if (!first)
+		return;
+	first = false;
+	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1 && ts.ts_lines)
+		redraw_lines = ts.ts_lines - 3;
+}
+
+
 /*
  * Print a description of the network interfaces.
  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
@@ -613,7 +631,9 @@ __dead static void
 sidewaysintpr_sysctl(unsigned interval)
 {
 	sigset_t emptyset;
-	int line;
+	unsigned line;
+
+	set_lines();
 
 	fetchifs();
 	if (ip_cur.ift_name[0] == '\0') {
@@ -652,7 +672,7 @@ loop:
 		sigsuspend(&emptyset);
 	signalled = 0;
 	(void)alarm(interval);
-	if (line == 21)
+	if (line == redraw_lines)
 		goto banner;
 	goto loop;
 	/*NOTREACHED*/
@@ -665,11 +685,13 @@ sidewaysintpr_kvm(unsigned interval, u_l
 	struct ifnet ifnet;
 	u_long firstifnet;
 	struct iftot *ip, *total;
-	int line;
+	unsigned line;
 	struct iftot *lastif, *sum, *interesting;
 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
 	int oldmask;
 
+	set_lines();
+
 	/*
 	 * Find the pointer to the first ifnet structure.  Replace
 	 * the pointer to the TAILQ_HEAD with the actual pointer
@@ -883,7 +905,7 @@ loop:
 	}
 	sigsetmask(oldmask);
 	signalled = false;
-	if (line == 21)
+	if (line == redraw_lines)
 		goto banner;
 	goto loop;
 	/*NOTREACHED*/

Reply via email to