I am trying to get the current number of rows and columns of the TTY I am attached to under linux.

Originally, I was calling the "/bin/stty size" prog and grabbing the output, but decided I didn't want to have to call an outside program.

I looked at the source of 'stty', and found it using the TIOCGWINSZ call to 'ioctl' on stdout and getting back a struct of 4 shorts (16-bit).

So, converting that to perl, I end up with:

#!/usr/bin/perl -w use strict; my $need_update; my ($maxcols, $maxrows); sub getwinsize () { sub TIOCGWINSZ () {0x5413} #from ioctls.ph, which wouldn't include return ($maxcols,$maxrows) if $maxcols && $maxrows && !$need_update; my $winsize=0; my $err = ioctl (STDOUT, TIOCGWINSZ, $winsize); if ($err) { print "ioctl err: $?\n"; } my ($rows, $cols, $xpix, $ypix) = unpack "s!*", $winsize; printf STDERR "r=%d, c=%d, xp=%d, yp=%d\n", $rows//0, $cols//0, $xpix//0, $ypix//0; $need_update = undef; ($maxcols, $maxrows) = ($cols ? $cols : -1, $rows ? $rows : -1); } #main my ($rows,$cols) = getwinsize; print "rows=$rows, cols=$cols\n";
But this doesn't work -- returns all zeros for the sizes.

Why? The ioctl works in 'C', why not in Perl?

Thanks!


In reply to 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.