Skip to content

Instantly share code, notes, and snippets.

@EchoAbstract
Created February 17, 2013 02:09
Show Gist options
  • Save EchoAbstract/4969725 to your computer and use it in GitHub Desktop.
Save EchoAbstract/4969725 to your computer and use it in GitHub Desktop.
This shows how to grab the rows and columns of your terminal using either the TIOCGSIZE or TIOCGWINSZ ioctls. NB: There's no error handling and no fallback case. Tested on Debian and OS-X.
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
struct term_info {
int rows;
int cols;
};
struct term_info
GetTermInfo(){
struct term_info info = {-1, -1};
#ifdef TIOCGSIZE
struct ttysize ts;
ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
info.cols = ts.ts_cols;
info.rows = ts.ts_lines;
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
info.cols = ts.ws_col;
info.rows = ts.ws_row;
#endif /* TIOCGSIZE */
return info;
}
int main(int argc, char **argv){
struct term_info ti = GetTermInfo();
printf("Rows: %d, Cols: %d\n", ti.rows, ti.cols);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment