Terminal window size of terminal I/O

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.