xref: /titanic_44/usr/src/lib/libast/common/port/astwinsize.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1 /***********************************************************************
2 *                                                                      *
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                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * AT&T Research
26  * return terminal rows and cols
27  */
28 
29 #include <ast.h>
30 #include <ast_tty.h>
31 
32 #if defined(__STDPP__directive) && defined(__STDPP__hide)
33 __STDPP__directive pragma pp:hide ioctl sleep
34 #else
35 #define ioctl		______ioctl
36 #define sleep		______sleep
37 #endif
38 
39 #if _sys_ioctl
40 #include <sys/ioctl.h>
41 #endif
42 
43 #if defined(TIOCGWINSZ)
44 #if _sys_stream && _sys_ptem
45 #include <sys/stream.h>
46 #include <sys/ptem.h>
47 #endif
48 #else
49 #if !defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
50 #if _hdr_jioctl
51 #define jwinsize	winsize
52 #include <jioctl.h>
53 #else
54 #if _sys_jioctl
55 #define jwinsize	winsize
56 #include <sys/jioctl.h>
57 #endif
58 #endif
59 #endif
60 #endif
61 
62 #if defined(__STDPP__directive) && defined(__STDPP__hide)
63 __STDPP__directive pragma pp:nohide ioctl sleep
64 #else
65 #undef	ioctl
66 #undef	sleep
67 #endif
68 
69 static int		ttctl(int, int, void*);
70 
71 void
astwinsize(int fd,register int * rows,register int * cols)72 astwinsize(int fd, register int* rows, register int* cols)
73 {
74 #ifdef	TIOCGWINSZ
75 #define NEED_ttctl
76 	struct winsize	ws;
77 
78 	if (!ttctl(fd, TIOCGWINSZ, &ws) && ws.ws_col > 0 && ws.ws_row > 0)
79 	{
80 		if (rows) *rows = ws.ws_row;
81 		if (cols) *cols = ws.ws_col;
82 	}
83 	else
84 #else
85 #ifdef	TIOCGSIZE
86 #define NEED_ttctl
87 	struct ttysize	ts;
88 
89 	if (!ttctl(fd, TIOCGSIZE, &ts) && ts.ts_lines > 0 && ts.ts_cols > 0)
90 	{
91 		if (rows) *rows = ts.ts_lines;
92 		if (cols) *cols = ts.ts_cols;
93 	}
94 	else
95 #else
96 #ifdef	JWINSIZE
97 #define NEED_ttctl
98 	struct winsize	ws;
99 
100 	if (!ttctl(fd, JWINSIZE, &ws) && ws.bytesx > 0 && ws.bytesy > 0)
101 	{
102 		if (rows) *rows = ws.bytesy;
103 		if (cols) *cols = ws.bytesx;
104 	}
105 	else
106 #endif
107 #endif
108 #endif
109 	{
110 		char*		s;
111 
112 		if (rows) *rows = (s = getenv("LINES")) ? strtol(s, NiL, 0) : 0;
113 		if (cols) *cols = (s = getenv("COLUMNS")) ? strtol(s, NiL, 0) : 0;
114 	}
115 }
116 
117 #ifdef	NEED_ttctl
118 
119 /*
120  * tty ioctl() -- no cache
121  */
122 
123 static int
ttctl(register int fd,int op,void * tt)124 ttctl(register int fd, int op, void* tt)
125 {
126 	register int	v;
127 
128 	if (fd < 0)
129 	{
130 		for (fd = 0; fd <= 2; fd++)
131 			if (!ioctl(fd, op, tt)) return(0);
132 		if ((fd = open("/dev/tty", O_RDONLY)) >= 0)
133 		{
134 			v = ioctl(fd, op, tt);
135 			close(fd);
136 			return(v);
137 		}
138 	}
139 	else if (!ioctl(fd, op, tt)) return(0);
140 	return(-1);
141 }
142 
143 #endif
144