forked from opnsense/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttysize.c
87 lines (79 loc) · 2.29 KB
/
ttysize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* $Id: ttysize.c,v 1.2 2019/07/25 00:07:15 tom Exp $
*
* ttysize.c -- obtain terminal-size for dialog
*
* Copyright 2018,2019 Thomas E. Dickey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to
* Free Software Foundation, Inc.
* 51 Franklin St., Fifth Floor
* Boston, MA 02110, USA.
*
* An earlier version of this program lists as authors
* Savio Lam ([email protected])
*/
#include <dialog.h>
/*
* This is based on work I did for ncurses in 1997, and improved/extended for
* other terminal-based programs. The comments are from my original version -TD
*/
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef NEED_PTEM_H
/* On SCO, they neglected to define struct winsize in termios.h -- it's only
* in termio.h and ptem.h (the former conflicts with other definitions).
*/
# include <sys/stream.h>
# include <sys/ptem.h>
#endif
/*
* SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
* Solaris, IRIX) define TIOCGWINSZ and struct winsize.
*/
#if defined(TIOCGSIZE)
# define IOCTL_WINSIZE TIOCGSIZE
# define STRUCT_WINSIZE struct ttysize
# define WINSIZE_ROWS(n) (int)n.ts_lines
# define WINSIZE_COLS(n) (int)n.ts_cols
#elif defined(TIOCGWINSZ)
# define IOCTL_WINSIZE TIOCGWINSZ
# define STRUCT_WINSIZE struct winsize
# define WINSIZE_ROWS(n) (int)n.ws_row
# define WINSIZE_COLS(n) (int)n.ws_col
#else
# undef HAVE_SIZECHANGE
#endif
int
dlg_ttysize(int fd, int *high, int *wide)
{
int rc = -1;
#ifdef HAVE_SIZECHANGE
if (isatty(fd)) {
STRUCT_WINSIZE size;
if (ioctl(fd, IOCTL_WINSIZE, &size) >= 0) {
*high = WINSIZE_ROWS(size);
*wide = WINSIZE_COLS(size);
rc = 0;
}
}
#else
*high = 24;
*wide = 80;
#endif /* HAVE_SIZECHANGE */
return rc;
}