Finding out the terminal's screen size?

Finding out the terminal's screen size?

Post by Kenneth Fal » Mon, 01 Mar 1993 01:59:26



I'd like to know the correct method(s) of finding out a terminal's
screen size, considering that it may change as a SIGWINCH occurs
(under X, for example).

I can think of three different ways:
 - The co# and li# entries in /etc/termcap (or the terminfo equivalent)
 - Environment variables COLUMNS and LINES
 - An ioctl call with TIOCGWINSZ

But what is considered the right order to try these methods to get the
real size? Also, I believe ioctl(TIOCGWINSZ...) is not POSIX; is there
a POSIX function for this?

Thanks.

--

 
 
 

Finding out the terminal's screen size?

Post by Brian Fitzgera » Mon, 01 Mar 1993 13:47:26


Quote:Kenneth Falck writes:
>I'd like to know the correct method(s) of finding out a terminal's
>screen size, considering that it may change as a SIGWINCH occurs
>(under X, for example).

>I can think of three different ways:
> - The co# and li# entries in /etc/termcap (or the terminfo equivalent)
> - Environment variables COLUMNS and LINES
> - An ioctl call with TIOCGWINSZ

>But what is considered the right order to try these methods to get the
>real size? Also, I believe ioctl(TIOCGWINSZ...) is not POSIX; is there
>a POSIX function for this?

This order:

Quote:> - An ioctl call with TIOCGWINSZ

(gives right answer after SIGWINCH)
Quote:> - The co# and li# entries in /etc/termcap (or the terminfo equivalent)

(use tgetent and tgetnum.  Gives wrong answer if term was resized.)
Quote:> - Environment variables COLUMNS and LINES

Not always set.

I have a few questions of my own?

- Why is it that when I log in from an xterm to a Sun, then resize,
TIOCGWINSZ on the remote sun gives the right answer, but when I log in
to an rs6000, it does not?

- Which headers should be included?
- Which is preferable, or standard?
<sys/ioctl.h>
<sys/termios.h>
<sgtty.h>

Brian

 
 
 

Finding out the terminal's screen size?

Post by Guy Harr » Tue, 02 Mar 1993 07:49:06


Quote:>>Also, I believe ioctl(TIOCGWINSZ...) is not POSIX; is there
>>a POSIX function for this?

No, there is no POSIX function for that.

Quote:>- Why is it that when I log in from an xterm to a Sun, then resize,
>TIOCGWINSZ on the remote sun gives the right answer, but when I log in
>to an rs6000, it does not?

Beats me.  Worked fine when I rlogged in from my Sun to an RS/6000 here,
running AIX 3.2.

Quote:>- Which headers should be included?
>- Which is preferable, or standard?
><sys/ioctl.h>
><sys/termios.h>
><sgtty.h>

There isn't a standard that covers all systems.

If we restrict ourselves to systems with "termio(s)" - which blows away
pre-4.3-reno BSD systems, for example - we find that:

        1) 4.3-reno and Net-2 define TIOCGWINSZ and TIOCSWINSZ in
           <sys/ioctl.h>, and do not automatically include <sys/ioctl.h>
           if you include <sys/termios.h>, so you can't just include
           <sys/termios.h> on those systems - you have to include
           <sys/ioctl.h> (or something that forces it to be included).

        2) SunOS 4.x defines them in <sys/ttycom.h>, which is included
           by <sys/termios.h>, although you have to make sure
           _POSIX_SOURCE is *not* defined (if it *is* defined, symbols
           such as "TIOC[GS]WINSZ" that "pollute the namespace" aren't
           defined).  You can, therefore, get them with <sys/termios.h>.

           You can also get them with <sys/ioctl.h>, as that includes
           <sys/ttold.h>, which includes <sys/ttycom.h> - *however*,
           <sys/ioctl.h>, for compatibility with pre-4.3-reno BSD
           releases, defines stuff for the older V7-flavored tty
           driver that collide with definitions in <sys/termios.h>
           (e.g., ECHO).

        3) SunOS 5.x defines them in <sys/termios.h> as well - again,
           only if _POSIX_SOURCE isn't defined.  <sys/ioctl.h> appears
           to act in the SunOS 4.x fashion only if BSD_COMP is defined.

           I don't have a "more vanilla" SVR4 system handy; however, the
           SVR4 documentation mentions TIOCGWINSZ and TIOCSWINSZ in the
           "termios(7)" manual page, so I suspect it gives you
           TIOC[GS]WINSZ if you include <sys/termios.h> and don't have
           _POSIX_SOURCE defined.  I suspect you may *not* get them if
           you include <sys/ioctl.h> (it may do so if BSD_COMP is
           defined).

        4) AIX 3.2 defines them in <sys/ioctl.h>, although _ALL_SOURCE
           has to be defined to have "struct winsize" defined.

           <sys/ioctl.h> has a comment that it contains "ioctl
           definitions a la 4.xBSD for BSD to AIX porting tools"; I've
           no idea whether or not they're under the delusion that
           TIOC[GS]WINSZ's only reason for existence is BSD compatibility.

           <sys/termio.h> doesn't define them, and there doesn't appear
           to be a <sys/termios.h> - at least not in "/usr/include/sys".

        5) HP-UX 8.07 (or whatever the hell version the machine in
           question is running) defines them in <sys/termio.h>, which is
           included by <sys/termios.h>; I haven't bothered checking what
           stuff has to be defined to turn that on, as their "vi" is
           singularly obnoxious - even though "stty size" correctly
           reports that my window has 66 rows, "vi" seems to insist that
           it doesn't have 66 rows....

           <sys/ioctl.h> includes only <sys/stdsyms.h>, which includes
           nothing; <sys/ioctl.h> doesn't define TIOC[GS]WINSZ.

So systems seem to fall into two camps:

        1) define it in <sys/termios.h> (at least if "strict POSIX
           namespace" isn't turned on), and either don't define it in
           <sys/ioctl.h> or don't encourage <sys/ioctl.h> to coexist
           with <sys/termios.h>;

        2) define it in <sys/ioctl.h> but not in <sys/termios.h>.

I somewhat prefer camp 1) (then again, I'm the one who put SunOS 4.x
*into* that camp); the fact that TIOC[GS]WINSZ pollute the POSIX
namespace doesn't bother me, as plenty of other tty driver features that
belong in <sys/termios.h> do so as well, so you're stuck with a "strict
POSIX" flag in any case.

If camp 1) causes some people a real problem, I'd vote for camp 3) -
have some separate include file, <sys/ttywinsz.h> or something such as
that - as long as *everybody* agrees on it, so you can write software
that uses "termios" and TIOC[GS]WINSZ without having to pollute it with
lots of stupid #ifdefs.

 
 
 

1. Problem in getting screen size when 'rlogin' and 'telnet'

Hi,
        I have a problem in getting screen size using "ioctl" function.

        The following program returns the correct screen size if the term is
by "rlogin" but does return an error if term is by "telnet".

#include<stdio.h>
#include<termios.h>
#include<unistd.h>
extern int errno;
main()
{
   struct winsize ws;

   if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
        printf("%d is the errno\n", errno);

   printf("%d is the width\n", ws.ws_col);
   printf("%d is the length\n", ws.ws_row);

The output for the above program from an "rlogin" window is
90 is the width
58 is the length

The output from a "telnet" window is
22 is the errno
0 is the width
0 is the length

Could anybody educate me regarding this?

        Also "stty" behaves differently from "rlogin" window and from a "telnet"
window.
        The command "stty rows 24" works Okay on an "rlogin" terminal
but, on a "telnet" terminal it gives the following error:
        unknown mode: rows

Could anybody help me in this regard? Please send me email.

Thanks

vasant

2. how do I do multiple forwarding??

3. STARTX: "No Screens Found"/"Can't find Display"

4. route cloning

5. : How does 'rlogin' copy terminal size to remote system ?

6. Error installing AdminSuite

7. 'ls' and AIX 4.3.2 smitty Screen size??

8. pppd send ioctl(PPPIOCSASYNCMAP): I/O error, and die

9. 'Matrix' screen saver-type x-terminal

10. Can apache/stronghold log when a user 'click outs' of your site?

11. Finding process size in 'C' code

12. HELP pls. 'find' sizes

13. 'find -ls' size in kbytes broken?