Confusing docs for system ioctl & perl ioctl -- got it.

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)?

#!/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
My output:
ERROR: ioctl on STDOUT: Bad address ERROR: ioctl on fileno(STDOUT): Bad file descriptor (rows=0, cols=0) rows=-1, cols=-1

In reply to Re^6: Failing to get current TTY's rows & columns... by perl-diddler
in thread Failing to get current TTY's rows & columns... by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.