my $winsize = "\0" x 8; # XXX: should be require sys/ioctl.pl # value below is for BSD, eg: OpenBSD, darwin=MacOS X, (etc?) my $TIOCGWINSZ = 0x40087468; if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) { ($rows, $cols, $xpix, $ypix) = unpack('S4', $winsize); } else { $cols = 80; } print "$cols\n"; __END__ #include #include #include #include #include #include main() { struct winsize mywinsize; int ttyfd; if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY)) == -1) { perror("open /dev/tty"); exit(-1); } if (ioctl(ttyfd, TIOCGWINSZ, &mywinsize) == -1) { perror("ioctl TIOCGWINSZ"); exit(-1); } printf("TIOCGWINSZ %#08x\n", TIOCGWINSZ ); printf("sizeof struct winsize %d\n", sizeof(struct winsize) ); printf("rows %d\n", mywinsize.ws_row ); printf("cols %d\n", mywinsize.ws_col ); if (fclose(stdout) == EOF) { perror("close stdout"); exit(-1); } exit(0); }