Most UNIX systems provide a function to track the window size of the current terminal. When the window size changes, the kernel notifies the foreground process group. The kernel stores a winsize structure for each terminal and Pseudo Terminal:
Struct winsize {
Unsigned short ws_row;/* rows, in character */
Unsigned short ws_col;/* columns, in characters */
Unsigned short ws_xpixel;/* horizontal size, pixels (unused )*/
Unsigned short ws_ypixel;/* vertical size, pixels (unused )*/
};
This structure has the following functions:
Instance: print the current window size and then sleep. Each time the window size changes, the SIGWINCH signal is captured and the new window size is printed. The program must be terminated with a signal.
Program list 18-12 print window size
# Include "apue. h"
# Include <termios. h>
# Ifndef TIOCGWINSZ
# Include <sys/ioctl. h>
# Endif
Static void
Pr_winsize (int fd)
{
Struct winsize size;
If (ioctl (fd, TIOCGWINSZ, (char *) & size) <0)
Err_sys ("TIOCGWINSZ error ");
Printf ("% d rows, % d columns \ n", size. ws_row, size. ws_col );
}
Static void
Sig_winch (int signo)
{
Printf ("SIGWINCH received \ n ");
Pr_winsize (STDIN_FILENO );
}
Int
Main (void)
{
If (isatty (STDIN_FILENO) = 0)
Exit (1 );
If (signal (SIGWINCH, sig_winch) = SIG_ERR)
Err_sys ("signal error ");
Pr_winsize (STDIN_FILENO);/* print initial size */
For (;)/* and sleep forever */
Pause ();
}