1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
26 * return terminal rows and cols
32 #if defined(__STDPP__directive) && defined(__STDPP__hide)
33 __STDPP__directive pragma pp
:hide ioctl sleep
35 #define ioctl ______ioctl
36 #define sleep ______sleep
40 #include <sys/ioctl.h>
43 #if defined(TIOCGWINSZ)
44 #if _sys_stream && _sys_ptem
45 #include <sys/stream.h>
49 #if !defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
51 #define jwinsize winsize
55 #define jwinsize winsize
56 #include <sys/jioctl.h>
62 #if defined(__STDPP__directive) && defined(__STDPP__hide)
63 __STDPP__directive pragma pp
:nohide ioctl sleep
69 static int ttctl(int, int, void*);
72 astwinsize(int fd
, register int* rows
, register int* cols
)
78 if (!ttctl(fd
, TIOCGWINSZ
, &ws
) && ws
.ws_col
> 0 && ws
.ws_row
> 0)
80 if (rows
) *rows
= ws
.ws_row
;
81 if (cols
) *cols
= ws
.ws_col
;
89 if (!ttctl(fd
, TIOCGSIZE
, &ts
) && ts
.ts_lines
> 0 && ts
.ts_cols
> 0)
91 if (rows
) *rows
= ts
.ts_lines
;
92 if (cols
) *cols
= ts
.ts_cols
;
100 if (!ttctl(fd
, JWINSIZE
, &ws
) && ws
.bytesx
> 0 && ws
.bytesy
> 0)
102 if (rows
) *rows
= ws
.bytesy
;
103 if (cols
) *cols
= ws
.bytesx
;
112 if (rows
) *rows
= (s
= getenv("LINES")) ? strtol(s
, NiL
, 0) : 0;
113 if (cols
) *cols
= (s
= getenv("COLUMNS")) ? strtol(s
, NiL
, 0) : 0;
120 * tty ioctl() -- no cache
124 ttctl(register int fd
, int op
, void* tt
)
130 for (fd
= 0; fd
<= 2; fd
++)
131 if (!ioctl(fd
, op
, tt
)) return(0);
132 if ((fd
= open("/dev/tty", O_RDONLY
)) >= 0)
134 v
= ioctl(fd
, op
, tt
);
139 else if (!ioctl(fd
, op
, tt
)) return(0);