perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Failing to get current TTY's rows & columns...
by Anonymous Monk on Apr 13, 2011 at 20:32 UTC | |
by perl-diddler (Chaplain) on Apr 14, 2011 at 02:10 UTC | |
by Anonymous Monk on Apr 14, 2011 at 08:13 UTC | |
by perl-diddler (Chaplain) on Apr 15, 2011 at 22:08 UTC | |
by Anonymous Monk on Apr 15, 2011 at 22:29 UTC | |
| |
by perl-diddler (Chaplain) on Apr 15, 2011 at 22:00 UTC | |
by Anonymous Monk on Apr 15, 2011 at 22:20 UTC | |
|
Re: Failing to get current TTY's rows & columns...
by eff_i_g (Curate) on Apr 13, 2011 at 20:40 UTC |