/* Query terminal dimensions. Requires stderr to not be redirected. */ #include #include #include #include int main(int argc, char **argv) { struct winsize ww; if( argc != 2 ) return printf("%s \n", *argv); /* Ask for the window size of stderr, and not stdout. We do this because stdout is going to be consumed by the script calling this utility, while stderr will remain connected to the real terminal. */ ioctl(STDERR_FILENO, TIOCGWINSZ, &ww); #define OUTPUT_FIELD(field) \ if( strcmp(argv[1], #field) == 0 ) \ { \ printf("%d\n", (int)(ww.field)); \ } \ else OUTPUT_FIELD(ws_row) OUTPUT_FIELD(ws_col) OUTPUT_FIELD(ws_xpixel) OUTPUT_FIELD(ws_ypixel) { printf("Unrecognized field: %s\n", argv[1]); return 1; } return 0; }