1320 Chapter 62
Listing 62-5: Monitoring changes in the terminal window size
–––––––––––––––––––––––––––––––––––––––––––––––––––––– tty/demo_SIGWINCH.c
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "tlpi_hdr.h"
static void
sigwinchHandler(int sig)
{
}
int
main(int argc, char *argv[])
{
struct winsize ws;
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sigwinchHandler;
if (sigaction(SIGWINCH, &sa, NULL) == -1)
errExit("sigaction");
for (;;) {
pause(); /* Wait for SIGWINCH signal */
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
errExit("ioctl");
printf("Caught SIGWINCH, new window size: "
"%d rows * %d columns\n", ws.ws_row, ws.ws_col);
}
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––– tty/demo_SIGWINCH.c
It is also possible to change the terminal driver’s notion of the window size by pass-
ing an initialized winsize structure in an ioctl() TIOCSWINSZ operation:
ws.ws_row = 40;
ws.ws_col = 100;
if (ioctl(fd, TIOCSWINSZ, &ws) == -1)
errExit("ioctl");
If the new values in the winsize structure differ from the terminal driver’s current
notion of the terminal window size, two things happen:
z The terminal driver data structures are updated using the values supplied in
the ws argument.
z A SIGWINCH signal is sent to the foreground process group of the terminal.