/***********************/ /* vt100resize.c 1.04 */ /* by Adam M. Costello */ /* */ /* */ /* vt100resize [ -q ] */ /* **************/ /* Asks the terminal (which must be */ /* vt100-compatible) how large it */ /* is, calls stty to inform the tty */ /* of this information, and resets */ /* the margins to the full screen. */ /* Unless -q is supplied, prints a */ /* message to stdout with the form */ /* "N rows, M columns". */ /************************************/ /* This is ANSI C plus POSIX code. */ #include #include #include #include #include #ifdef NeXT /* Compile with cc -posix, requires NeXTSTEP 3.2 or later. */ #include #include #endif #ifdef __osf__ #include #endif void usage(const char *progname) { fprintf(stderr, "usage:\n%s [ -q ]\n", progname); exit(1); } int getcsi(FILE *f) { int c; c = fgetc(f); if (c == '\233') return 1; if (c != '\033') return 0; c = fgetc(f); return c == '['; } int main(int argc, char **argv) { int fd, rows, cols, quiet; struct termios oldattr, newattr; struct winsize size; FILE *f; if (argc > 2) usage(argv[0]); if (argc == 2) { if (strcmp(argv[1], "-q")) usage(argv[0]); quiet = 1; } else quiet = 0; fd = open("/dev/tty", O_RDONLY); if (fd < 0) { perror("open"); exit(1); } if (tcgetattr(fd, &oldattr)) { perror("tcgetattr"); exit(1); } newattr = oldattr; newattr.c_lflag &= ~ICANON; newattr.c_cc[VMIN] = 1; newattr.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSANOW, &newattr)) { perror("tcsetattr"); exit(1); } f = fopen("/dev/tty", "r+"); if (!f) { perror("fopen"); tcsetattr(fd, TCSANOW, &oldattr); exit(1); } /* Allow movement outside of margins. Move */ /* to 999,999. Query cursor position. */ fputs("\033[?6l\033[999;999H\033[6n", f); fflush(f); if (!getcsi(f) || fscanf(f, "%d;%dR", &rows, &cols) != 2) { fputs("terminal did not respond properly", stderr); tcsetattr(fd, TCSANOW, &oldattr); exit(1); } if (tcsetattr(fd, TCSANOW, &oldattr)) { perror("tcsetattr"); exit(1); } size.ws_row = rows; size.ws_col = cols; if (ioctl(fd, TIOCSWINSZ, &size) < 0) { perror("ioctl"); exit(1); } close(fd); /* Set margins to 1,rows. Move cursor to */ /* beginning of last row. Erase the line. */ fprintf(f, "\033[1;%dr\033[%dH\033[2K", rows, rows); fflush(f); if (!quiet) printf("%d rows, %d columns\n", rows, cols); exit(0); }