得到 Linux, Unix 终端大小

In bash, the $LINES and $COLUMNS environmental variables should be able to do the trick. The will be set automatically upon any change in the terminal size. (i.e. the SIGWINCH signal)

 

shell> stty -a
speed 9600 baud; rows = 56 ; columns = 157 ; ypixels = 0; xpixels = 0; csdata ? eucw 1:0:0:0, scrw 1:0:0:0 intr = ^c; quit = ^/; erase = ^h; kill = ^u; eof = ^d; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^q; stop = ^s; susp = ^z; dsusp = ^y; rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v; -parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext -ignbrk brkint -ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc ixon -ixany -ixoff imaxbel isig icanon -xcase echo echoe echok -echonl -noflsh -tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3

 

 

C++:

#include <sys/ioctl.h> #include <stdio.h> #include <unistd.h> __________________ int main (void) { int cols = 80; int lines = 24; #ifdef TIOCGSIZE struct ttysize ts; ioctl(STDIN_FILENO, TIOCGSIZE, &ts); cols = ts.ts_cols; lines = ts.ts_lines; #elif defined(TIOCGWINSZ) struct winsize ts; ioctl(STDIN_FILENO, TIOCGWINSZ, &ts); cols = ts.ws_col; lines = ts.ws_row; #endif /* TIOCGSIZE */ printf("Terminal is %dx%d/n", cols, lines); }

 

#include <ncurses.h> namespace dim { int x() { int xdim; int ydim; getmaxyx(stdscr, ydim, xdim); return xdim; } int y() { int xdim; int ydim; getmaxyx(stdscr, ydim, xdim); return ydim; } } cout << "The size of your terminal is x: " << dim::x << " y: " << dim::y << endl;

 

#include <sys/ioctl.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> int main(int argc,char **argv) { struct winsize ws; if (ioctl(0,TIOCGWINSZ,&ws)!=0) { fprintf(stderr,"TIOCGWINSZ:%s/n",strerror(errno)); exit(1); } printf("row=%d, col=%d, xpixel=%d, ypixel=%d/n", ws.ws_row,ws.ws_col,ws.ws_xpixel,ws.ws_ypixel); return 0; }

你可能感兴趣的:(得到 Linux, Unix 终端大小)