in reply to Re: Failing to get current TTY's rows & columns...
in thread Failing to get current TTY's rows & columns...

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?

Replies are listed 'Best First'.
Re^3: Failing to get current TTY's rows & columns...
by Anonymous Monk on Apr 15, 2011 at 22:20 UTC
    My manpage says:

    Manpage? I already linked to ioctl before, perldoc -f ioctl