]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/ttyutils.c
2 * No copyright is claimed. This code is in the public domain; do with
5 * Written by Karel Zak <kzak@redhat.com>
18 # ifndef DEFAULT_VCTERM
19 # define DEFAULT_VCTERM "linux"
21 # if defined (__s390__) || defined (__s390x__)
22 # define DEFAULT_TTYS0 "dumb"
23 # define DEFAULT_TTY32 "ibm327x"
24 # define DEFAULT_TTYS1 "vt220"
26 # ifndef DEFAULT_STERM
27 # define DEFAULT_STERM "vt102"
29 #elif defined(__GNU__)
30 # ifndef DEFAULT_VCTERM
31 # define DEFAULT_VCTERM "hurd"
33 # ifndef DEFAULT_STERM
34 # define DEFAULT_STERM "vt102"
37 # ifndef DEFAULT_VCTERM
38 # define DEFAULT_VCTERM "vt100"
40 # ifndef DEFAULT_STERM
41 # define DEFAULT_STERM "vt100"
45 static int get_env_int(const char *name
)
47 const char *cp
= getenv(name
);
54 x
= strtol(cp
, &end
, 10);
56 if (errno
== 0 && end
&& *end
== '\0' && end
> cp
&&
57 x
> 0 && x
<= INT_MAX
)
64 int get_terminal_dimension(int *cols
, int *lines
)
68 #if defined(TIOCGWINSZ)
70 if (ioctl (STDOUT_FILENO
, TIOCGWINSZ
, &w_win
) == 0) {
74 #elif defined(TIOCGSIZE)
76 if (ioctl (STDOUT_FILENO
, TIOCGSIZE
, &t_win
) == 0) {
83 c
= get_env_int("COLUMNS");
88 l
= get_env_int("LINES");
94 int get_terminal_width(int default_width
)
98 get_terminal_dimension(&width
, NULL
);
100 return width
> 0 ? width
: default_width
;
103 int get_terminal_stdfd(void)
105 if (isatty(STDIN_FILENO
))
107 if (isatty(STDOUT_FILENO
))
108 return STDOUT_FILENO
;
109 if (isatty(STDERR_FILENO
))
110 return STDERR_FILENO
;
115 int get_terminal_name(const char **path
,
131 fd
= get_terminal_stdfd();
133 return fd
; /* error */
142 tty
= strncmp(tty
, "/dev/", 5) == 0 ? tty
+ 5 : tty
;
146 for (p
= tty
; p
&& *p
; p
++) {
156 int get_terminal_type(const char **type
)
158 *type
= getenv("TERM");
164 char *get_terminal_default_type(const char *ttyname
, int is_serial
)
167 #ifdef HAVE_GETTTYNAM
168 struct ttyent
*ent
= getttynam(ttyname
);
170 if (ent
&& ent
->ty_type
)
171 return strdup(ent
->ty_type
);
174 #if defined (__s390__) || defined (__s390x__)
176 * Special terminal on first serial line on a S/390(x) which
177 * is due legacy reasons a block terminal of type 3270 or
178 * higher. Whereas the second serial line on a S/390(x) is
179 * a real character terminal which is compatible with VT220.
181 if (strcmp(ttyname
, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */
182 return strdup(DEFAULT_TTYS0
);
183 else if (strncmp(ttyname
, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */
184 return strdup(DEFAULT_TTY32
);
185 else if (strcmp(ttyname
, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */
186 return strdup(DEFAULT_TTYS1
);
190 return strdup(is_serial
? DEFAULT_STERM
: DEFAULT_VCTERM
);
193 #ifdef TEST_PROGRAM_TTYUTILS
197 const char *path
, *name
, *num
;
200 if (get_terminal_name(&path
, &name
, &num
) == 0) {
203 fprintf(stderr
, "tty path: %s\n", path
);
204 fprintf(stderr
, "tty name: %s\n", name
);
205 fprintf(stderr
, "tty number: %s\n", num
);
207 fprintf(stderr
, "tty term: %s\n", getenv("TERM"));
209 term
= get_terminal_default_type(name
, 0);
210 fprintf(stderr
, "tty dflt term: %s\n", term
);
213 get_terminal_dimension(&c
, &l
);
214 fprintf(stderr
, "tty cols: %d\n", c
);
215 fprintf(stderr
, "tty lines: %d\n", l
);
220 #endif /* TEST_PROGRAM_TTYUTILS */