FreeBSD 13.0-RELEASE から入った新規の tcgetwinsize でウィンドウの大きさを取得する ― 2021年03月19日 11時53分29秒
以前からウィンドウの大崎が変更された時に、SIGWINCH シグナルが渡されていた。この関数が出来る以前は sysctl でウィンドウの大きさを取得する必要があった。
プログラムをコンパイル、実行してから xterm の大きさを変えて出てきた出力。
#include <signal.h> // signal
#include <termios.h> // tcgetwinsize
#include <unistd.h> // write
#include <stdio.h> // sprintf, gets_s
#include <string.h> // strlen
void size( int signo )
{
char buf[ 1024 ];
struct winsize wsize;
tcgetwinsize( STDERR_FILENO, &wsize );
sprintf(buf, "row=%d, col=%d\n", wsize.ws_row, wsize.ws_col );
write( STDOUT_FILENO, buf, strlen( buf ) );
}
int main()
{
char c;
signal( SIGWINCH, size );
gets_s( &c, 1 );
}
% cc tcgetwinsize.c
% ./a.out
row=25, col=88
row=25, col=87
row=25, col=86
row=25, col=85
row=25, col=84
row=26, col=83
row=26, col=82
row=26, col=81
最近のコメント