Originally, I was calling the "/bin/stty size" prog and grabbing the output, but decided I didn't want to have to call an outside program.
I looked at the source of 'stty', and found it using the TIOCGWINSZ call to 'ioctl' on stdout and getting back a struct of 4 shorts (16-bit).
So, converting that to perl, I end up with:
But this doesn't work -- returns all zeros for the sizes.#!/usr/bin/perl -w use strict; my $need_update; my ($maxcols, $maxrows); sub getwinsize () { sub TIOCGWINSZ () {0x5413} #from ioctls.ph, which wouldn't include return ($maxcols,$maxrows) if $maxcols && $maxrows && !$need_update; my $winsize=0; my $err = ioctl (STDOUT, TIOCGWINSZ, $winsize); if ($err) { print "ioctl err: $?\n"; } my ($rows, $cols, $xpix, $ypix) = unpack "s!*", $winsize; printf STDERR "r=%d, c=%d, xp=%d, yp=%d\n", $rows//0, $cols//0, $xpix//0, $ypix//0; $need_update = undef; ($maxcols, $maxrows) = ($cols ? $cols : -1, $rows ? $rows : -1); } #main my ($rows,$cols) = getwinsize; print "rows=$rows, cols=$cols\n";
Why? The ioctl works in 'C', why not in Perl?
Thanks!
In reply to Failing to get current TTY's rows & columns... by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |