[comp.unix.wizards] when to use ioctl with TIOCGWINSZ

Leisner.Henr@xerox.com (Marty) (01/14/90)

I recently tried to bring up Elvis (a vi clone posted to comp.os.minix) on
my sun386i.

This code sequence seems to cause problems on some remote terminals (via
TCP/IP or XNS).  It works on rlogin between suns.  It doesn't work with
telnet.  It doesn't work on rlogin between a non-sun and a sun.

        /* get the window size, one way or another. */
#ifdef TIOCGWINSZ
        LINES = COLS = 0;
        if (ioctl(2, TIOCGWINSZ, &size) >= 0)
        {
                LINES = size.ws_row;
                COLS = size.ws_col;
        }
#else
        LINES = tgetnum("li");
        COLS = tgetnum("co");
#endif

Essentially, when there's a problem(?), ioctl isn't returning an error, and
LINES and COLS get set to 0.

TIOCGWINSZ returns the terminal driver's notion of the size.  I guess the
driver got the wrong notion (actually how can the driver know what size a
remote terminal is?).

How does one know when they'll get meaningful information out of
TIOCGWINSZ?

What's the correct way to handle this problem?

marty
ARPA:	leisner.henr@xerox.com
GV:  leisner.henr
NS:  leisner:wbst139:xerox
UUCP:	hplabs!arisia!leisner
 

gwyn@smoke.BRL.MIL (Doug Gwyn) (01/15/90)

In article <22086@adm.BRL.MIL> Leisner.Henr@xerox.com (Marty) writes:
-This code sequence seems to cause problems on some remote terminals (via
-TCP/IP or XNS).  It works on rlogin between suns.  It doesn't work with
-telnet.  It doesn't work on rlogin between a non-sun and a sun.

Window size information is not passed at all as part of TELNET protocol.
4.2BSD and 4.3BSD passed window size information differently for rlogin.

-        /* get the window size, one way or another. */
-#ifdef TIOCGWINSZ
-        LINES = COLS = 0;
-        if (ioctl(2, TIOCGWINSZ, &size) >= 0)
-        {
-                LINES = size.ws_row;
-                COLS = size.ws_col;
-        }
-#else
-        LINES = tgetnum("li");
-        COLS = tgetnum("co");
-#endif
-Essentially, when there's a problem(?), ioctl isn't returning an error, and
-LINES and COLS get set to 0.

The code is in error to assume that a successful ioctl means that the
ws_row and ws_col data describes the window.  As you have discovered,
they will be 0 until properly set to the window dimensions.  The code
should test for that common case and use the LINES and COLUMNS
environment variables if the ioctl fails, and if the environment
variables aren't set to reasonable values it should prefer the termcap
li and co capabilities.

-TIOCGWINSZ returns the terminal driver's notion of the size.  I guess the
-driver got the wrong notion (actually how can the driver know what size a
-remote terminal is?).

The information is set by some TIOCSWINSZ, for example when the rlogin
daemon successfully obtains the window information from the remote system.