/* Demo for SIGWINCH; thp[thpinfo.com] 2009-01-22 */

#include <signal.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>

void handle_sigwinch(int signal)
{
    fprintf(stderr, "winch signal received\n");
    struct winsize ws;
    int fd;
    
    if ((fd = open("/dev/tty",O_RDWR)) != -1) {
        if (ioctl(fd,TIOCGWINSZ,&ws)!=0) {
            perror("ioctl(/dev/tty,TIOCGWINSZ)");
        } else {
            fprintf(stderr, "rows %i\n", ws.ws_row);
            fprintf(stderr, "cols %i\n", ws.ws_col);
        }
        close(fd);
    } else {
        perror("open /dev/tty");
    }
}

int main(int argc, char** argv) {
    int seconds = 30;
    signal(SIGWINCH, handle_sigwinch);
    while ((seconds=sleep(seconds))>0);
    return 0;
}


