See ioctl, if the return value is true, its not an error.
My manpage says:
RETURN VALUE Usually, on success zero is returned. A few ioctl() requests +use the return value as an output parameter and return a non-negative +value on success. On error, -1 is returned, and errno is set appropriat +ely.
Note the phrase: "Usually, on success, zero is returned."... Some return a value as an output param, but this call isn't one of them. My error check code appears to be valid according to the above, though I noticed an oddity if you print the value returning from ioctl in this code:
require 'sys/ioctl.ph'; die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; open(TTY, "+</dev/tty") or die "No tty: $!"; my $ret=ioctl(TTY, &TIOCGWINSZ, $winsize=''); printf "ret num=%d, retstring=%s\n",$ret,$ret; unless ($ret) { die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWIN +SZ; } ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize); print "(row,col) = ($row,$col)"; print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixe +l; print "\n";
The output is:
ret num=0, retstring=0 but true (row,col) = (79,80)
That is bizarre! 0 = true?!

In any event, that's a red herring, as my code works when I use "/dev/tty" as a device instead of STDOUT. Same goes for the unpack format.

I used S!*, which is likely "more correct" than S4 shown above.

The "!" after the "S" says to use the platform's native size for a short instead of fixed a fixed 16-bit size. Since the struct is declared as a short on my platform, I figured it was best to use my platform's native "short" size rather than a fixed 16 bit value -- wouldn't you agree??

The "*" vs. "4", makes no difference in this case as there are only 4 values to receive the output. "*" tells unpack to unpack as many as are in the struct -- so if the struct was 6 shorts long instead of 4, it would return 6 values, the last 2 of which would be thrown away as I only have an array of 4 vars to receive it. It would be like doing:

my @ar=(1,2,3,4,5,6); my ($x,$y,$z)=@ar;
$x-$z get values 1 through 3, and values 4,5,6 get ignored.

All of the above covers the stuff that "doesn't make a difference". Then comes the part that doesn't make sense.

As you note, fileno() on STD(IN,OUT,ERR) all return 0,1,2 as expected. Then why can't I use ioctl(STDOUT) with ioctl and have it work?


In reply to Re^2: 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.