#!/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