in reply to Don't understand why can't get 'size' assoc w/STDOUT
I don’t trust sys/ioctl.ph. This code works for me:
But you may have to recompile and rerun the C code in DATA to get the proper values for the TIOCGWINSZ and the size of the structure.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 <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <termios.h> 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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Don't understand why can't get 'size' assoc w/STDOUT
by perl-diddler (Chaplain) on Apr 18, 2011 at 00:00 UTC | |
by tchrist (Pilgrim) on Apr 18, 2011 at 02:33 UTC | |
by tchrist (Pilgrim) on Apr 18, 2011 at 14:10 UTC | |
by perl-diddler (Chaplain) on Apr 19, 2011 at 01:41 UTC |