2 * $Id: ttysize.c,v 1.2 2019/07/25 00:07:15 tom Exp $
4 * ttysize.c -- obtain terminal-size for dialog
6 * Copyright 2018,2019 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
30 * This is based on work I did for ncurses in 1997, and improved/extended for
31 * other terminal-based programs. The comments are from my original version -TD
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
43 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
44 * in termio.h and ptem.h (the former conflicts with other definitions).
46 # include <sys/stream.h>
47 # include <sys/ptem.h>
51 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
52 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
54 #if defined(TIOCGSIZE)
55 # define IOCTL_WINSIZE TIOCGSIZE
56 # define STRUCT_WINSIZE struct ttysize
57 # define WINSIZE_ROWS(n) (int)n.ts_lines
58 # define WINSIZE_COLS(n) (int)n.ts_cols
59 #elif defined(TIOCGWINSZ)
60 # define IOCTL_WINSIZE TIOCGWINSZ
61 # define STRUCT_WINSIZE struct winsize
62 # define WINSIZE_ROWS(n) (int)n.ws_row
63 # define WINSIZE_COLS(n) (int)n.ws_col
65 # undef HAVE_SIZECHANGE
69 dlg_ttysize(int fd, int *high, int *wide)
72 #ifdef HAVE_SIZECHANGE
76 if (ioctl(fd, IOCTL_WINSIZE, &size) >= 0) {
77 *high = WINSIZE_ROWS(size);
78 *wide = WINSIZE_COLS(size);
85 #endif /* HAVE_SIZECHANGE */