From owner-svn-src-all@FreeBSD.ORG Thu Jan 15 03:12:46 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 10D2E1065674; Thu, 15 Jan 2009 03:12:46 +0000 (UTC) (envelope-from keramida@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id F14AB8FC18; Thu, 15 Jan 2009 03:12:45 +0000 (UTC) (envelope-from keramida@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n0F3CjST062007; Thu, 15 Jan 2009 03:12:45 GMT (envelope-from keramida@svn.freebsd.org) Received: (from keramida@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n0F3Cj9F062006; Thu, 15 Jan 2009 03:12:45 GMT (envelope-from keramida@svn.freebsd.org) Message-Id: <200901150312.n0F3Cj9F062006@svn.freebsd.org> From: Giorgos Keramidas Date: Thu, 15 Jan 2009 03:12:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-7@freebsd.org X-SVN-Group: stable-7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r187265 - stable/7/usr.sbin/iostat X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Jan 2009 03:12:46 -0000 Author: keramida (doc committer) Date: Thu Jan 15 03:12:45 2009 New Revision: 187265 URL: http://svn.freebsd.org/changeset/base/187265 Log: MFC 175562 from /head Repeat iostat header after rows-3 instead of a hardcoded 20. Use ioctl() to get the window size in iostat(8), and force a new header to be prepended to the output every time the current window size changes. Change the number of lines before each header to `rows - 3' when the terminal is resized, so that the full terminal length can be used for output lines. Modified: stable/7/usr.sbin/iostat/ (props changed) stable/7/usr.sbin/iostat/iostat.c Modified: stable/7/usr.sbin/iostat/iostat.c ============================================================================== --- stable/7/usr.sbin/iostat/iostat.c Thu Jan 15 02:57:07 2009 (r187264) +++ stable/7/usr.sbin/iostat/iostat.c Thu Jan 15 03:12:45 2009 (r187265) @@ -113,6 +113,7 @@ #include #include #include +#include #include #include #include @@ -129,17 +130,23 @@ struct nlist namelist[] = { { NULL }, }; +#define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */ + struct statinfo cur, last; int num_devices; struct device_selection *dev_select; int maxshowdevs; volatile sig_atomic_t headercount; +volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */ +unsigned short wrows; /* Current number of tty rows. */ int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0; int xflag = 0, zflag = 0; /* local function declarations */ static void usage(void); static void needhdr(int signo); +static void needresize(int signo); +static void doresize(void); static void phdr(void); static void devstats(int perf_select, long double etime, int havelast); static void cpustats(void); @@ -424,6 +431,20 @@ main(int argc, char **argv) */ (void)signal(SIGCONT, needhdr); + /* + * If our standard output is a tty, then install a SIGWINCH handler + * and set wresized so that our first iteration through the main + * iostat loop will peek at the terminal's current rows to find out + * how many lines can fit in a screenful of output. + */ + if (isatty(fileno(stdout)) != 0) { + wresized = 1; + (void)signal(SIGWINCH, needresize); + } else { + wresized = 0; + wrows = IOSTAT_DEFAULT_ROWS; + } + for (headercount = 1;;) { struct devinfo *tmp_dinfo; long tmp; @@ -457,7 +478,9 @@ main(int argc, char **argv) if (!--headercount) { phdr(); - headercount = 20; + if (wresized != 0) + doresize(); + headercount = wrows; } tmp_dinfo = last.dinfo; @@ -499,7 +522,9 @@ main(int argc, char **argv) break; case 1: phdr(); - headercount = 20; + if (wresized != 0) + doresize(); + headercount = wrows; break; default: break; @@ -534,7 +559,9 @@ main(int argc, char **argv) break; case 1: phdr(); - headercount = 20; + if (wresized != 0) + doresize(); + headercount = wrows; break; default: break; @@ -595,6 +622,47 @@ needhdr(int signo) headercount = 1; } +/* + * When the terminal is resized, force an update of the maximum number of rows + * printed between each header repetition. Then force a new header to be + * prepended to the next output. + */ +void +needresize(int signo) +{ + + wresized = 1; + headercount = 1; +} + +/* + * Update the global `wrows' count of terminal rows. + */ +void +doresize(void) +{ + int status; + struct winsize w; + + for (;;) { + status = ioctl(fileno(stdout), TIOCGWINSZ, &w); + if (status == -1 && errno == EINTR) + continue; + else if (status == -1) + err(1, "ioctl"); + if (w.ws_row > 3) + wrows = w.ws_row - 3; + else + wrows = IOSTAT_DEFAULT_ROWS; + break; + } + + /* + * Inhibit doresize() calls until we are rescheduled by SIGWINCH. + */ + wresized = 0; +} + static void phdr(void) {