Created
December 23, 2014 17:01
-
-
Save clausecker/722d03a71ca0ffcc0b48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <signal.h> | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
static void winch_handler(int); | |
static void print_tdim(void); | |
extern int | |
main() | |
{ | |
if (!isatty(STDIN_FILENO)) { | |
perror("Looks like this isn't a terminal"); | |
return (1); | |
} | |
print_tdim(); | |
signal(SIGWINCH, winch_handler); | |
/* wait for a signal */ | |
for (;;) | |
pause(); | |
} | |
static void | |
winch_handler(int n) | |
{ | |
(void)n; /* I don't care about n */ | |
print_tdim(); | |
signal(SIGWINCH, winch_handler); /* restore handler */ | |
} | |
static void | |
print_tdim(void) | |
{ | |
struct winsize ws; | |
/* Terminal IO Control Get WINdow SiZe */ | |
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != 0) { | |
perror("Cannot get terminal size"); | |
exit (1); | |
} | |
printf("columns: %3d rows: %3d\n", ws.ws_col, ws.ws_row); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment