UPDATE: See this node for a more complete solution.

I don’t trust sys/ioctl.ph. This code works for me:

my $winsize = "\0" x 8; # XXX: should be require sys/ioctl.pl # value below is for BSD, eg: OpenBSD, darwin=MacOS X, (etc?) my $TIOCGWINSZ = 0x40087468; if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) { ($rows, $cols, $xpix, $ypix) = unpack('S4', $winsize); } else { $cols = 80; } print "$cols\n"; __END__ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <termios.h> main() { struct winsize mywinsize; int ttyfd; if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY)) == -1) { perror("open /dev/tty"); exit(-1); } if (ioctl(ttyfd, TIOCGWINSZ, &mywinsize) == -1) { perror("ioctl TIOCGWINSZ"); exit(-1); } printf("TIOCGWINSZ %#08x\n", TIOCGWINSZ ); printf("sizeof struct winsize %d\n", sizeof(struct winsize) ); printf("rows %d\n", mywinsize.ws_row ); printf("cols %d\n", mywinsize.ws_col ); if (fclose(stdout) == EOF) { perror("close stdout"); exit(-1); } exit(0); }
But you may have to recompile and rerun the C code in DATA to get the proper values for the TIOCGWINSZ and the size of the structure.

In reply to Re: Don't understand why can't get 'size' assoc w/STDOUT by tchrist
in thread Don't understand why can't get 'size' assoc w/STDOUT 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.