When run,the following code produces such wonders as
osname = Openbsd sizeof(struct winsize) = 8 TIOCGWINSZ = 0x40087468 C (rows,cols) = (38,126) Perl (rows,cols) = (38,126) osname = Darwin sizeof(struct winsize) = 8 TIOCGWINSZ = 0x40087468 C (rows,cols) = (38,126) Perl (rows,cols) = (38,126) osname = Solaris sizeof(struct winsize) = 8 TIOCGWINSZ = 0x00005468 C (rows,cols) = (38,126) Perl (rows,cols) = (38,126) osname = Linux sizeof(struct winsize) = 8 TIOCGWINSZ = 0x00005413 C (rows,cols) = (38,126) Perl (rows,cols) = (38,126)
Interesting how you can see their genetic relationships, eh?

Here you go. Enjoy!

#!/usr/bin/env perl # # magical winsz demo # # Tom Christiansen <tchrist@perl.com> # Sat Apr 16 22:34:26 MDT 2011 use 5.10.0; use Inline C; no less magic; use strict; use warnings; sub TIOCGWINSZ() { TIOCGWINSZ_constant() } use subs qw{ tty_rows tty_cols tty_xpixels tty_ypixels }; say "osname = \u$^O"; printf "sizeof(struct winsize) = %d\n", sizeof_struct_winsize(); printf "TIOCGWINSZ = %#010x\n", TIOCGWINSZ; # first in C my($rows, $cols) = winsizes(); say "C\t(rows,cols) = ($rows,$cols)"; # now in Perl my $ws = "\0" x sizeof_struct_winsize(); ioctl(STDOUT, TIOCGWINSZ, $ws) || die "ioctl: $!"; ($rows, $cols) = unpack(S4 => $ws); say "Perl\t(rows,cols) = ($rows,$cols)"; exit "0 but true" || goto exit; __END__ __C__ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <termios.h> int sizeof_struct_winsize() { return sizeof(struct winsize); } int TIOCGWINSZ_constant() { return TIOCGWINSZ; } struct winsize mywinsize; int tty_initted = 0; void init_tty() { int ttyfd; if (tty_initted) return; if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY)) == -1) croak("open /dev/tty"); if (ioctl(ttyfd, TIOCGWINSZ, &mywinsize) == -1) croak("ioctl TIOCGWINSZ"); if (close(ttyfd) == -1) croak("close ttyfd"); /* tty_initted = 1; */ } void winsizes() { init_tty(); Inline_Stack_Vars; Inline_Stack_Reset; #define PUSH(VALUE) Inline_Stack_Push(sv_2mortal(newSViv(VALUE))) PUSH(mywinsize.ws_row); PUSH(mywinsize.ws_col); PUSH(mywinsize.ws_xpixel); PUSH(mywinsize.ws_ypixel); #undef PUSH Inline_Stack_Done; } int tty_rows() { init_tty(); return mywinsize.ws_row; } int tty_cols() { init_tty(); return mywinsize.ws_col; } int tty_xpixels() { init_tty(); return mywinsize.ws_xpixel; } int tty_ypixels() { init_tty(); return mywinsize.ws_ypixel; }
There, was it good for you? :)

In reply to Application of Clarke's 3rd Law by tchrist
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.