in reply to Re^5: Failing to get current TTY's rows & columns...
in thread Failing to get current TTY's rows & columns...
So my error check has a high level of bogosity! Details! ;-)
Regardless of the bogus error check, it still tries to extract the row,col meaning that if ioctl(STDOUT...) is valid, it should display the rows & columns.
I understand it won't work if STDOUT is redirected, but neither will opening "/dev/tty", if the program isn't running with a controlling terminal but DOES have STDOUT directed to a tty. I.e. one can always design a failure case -- but I'm more interested in why what should be a 'success' case doesn't work, namely using ioctl with 'STDOUT' when STDOUT is attached to a terminal.
Here's a simple program that should work but doesn't. Why doesn't it (I believe I've addressed the error case you raised, as well as the unpack format)?
My output:#!/usr/bin/perl -w use strict; use feature ':5.10'; { no warnings "all"; $SIG{__WARN__} = sub { }; #pretend "no warnings" works require "sys/ioctl.ph"; } sub getwinsize (;$) { my $recheck=$_[0]; my $winsize=0; state ($maxcols, $maxrows); return ($maxcols,$maxrows) if $maxcols && $maxrows && !$recheck; my $err = ioctl STDOUT, &TIOCGWINSZ, $winsize; unless ($err) { print STDERR "ERROR: ioctl on STDOUT: $!\n"; $err = ioctl fileno(STDOUT), &TIOCGWINSZ, $winsize; unless ($err) { print STDERR "ERROR: ioctl on fileno(STDOUT): $!\n"; } else { printf STDERR "ioctl on fileno(STDOUT) worked\n"; } } else { printf STDERR "ioctl on STDOUT worked\n"; } my ($rows, $cols, $unused1, $unused2) = unpack "S!4", $winsize; printf STDERR "(rows=%u, cols=%u)\n", $rows//0, $cols//0; ($maxcols, $maxrows) = ($cols || -1, $rows || -1); } my ($rows,$cols) = getwinsize; print "rows=$rows, cols=$cols\n"; # vim: ts=2: sw=2
ERROR: ioctl on STDOUT: Bad address ERROR: ioctl on fileno(STDOUT): Bad file descriptor (rows=0, cols=0) rows=-1, cols=-1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Application of Clarke's 3rd Law
by tchrist (Pilgrim) on Apr 17, 2011 at 05:13 UTC |