]>
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>
14 static int get_env_int(const char *name
)
16 const char *cp
= getenv(name
);
23 x
= strtol(cp
, &end
, 10);
25 if (errno
== 0 && end
&& *end
== '\0' && end
> cp
&&
26 x
> 0 && x
<= INT_MAX
)
33 int get_terminal_dimension(int *cols
, int *lines
)
37 #if defined(TIOCGWINSZ)
39 if (ioctl (STDOUT_FILENO
, TIOCGWINSZ
, &w_win
) == 0) {
43 #elif defined(TIOCGSIZE)
45 if (ioctl (STDOUT_FILENO
, TIOCGSIZE
, &t_win
) == 0) {
52 c
= get_env_int("COLUMNS");
57 l
= get_env_int("LINES");
63 int get_terminal_width(int default_width
)
67 get_terminal_dimension(&width
, NULL
);
69 return width
> 0 ? width
: default_width
;
72 int get_terminal_stdfd(void)
74 if (isatty(STDIN_FILENO
))
76 else if (isatty(STDOUT_FILENO
))
78 else if (isatty(STDERR_FILENO
))
84 int get_terminal_name(const char **path
,
100 fd
= get_terminal_stdfd();
102 return fd
; /* error */
111 tty
= strncmp(tty
, "/dev/", 5) == 0 ? tty
+ 5 : tty
;
115 for (p
= tty
; p
&& *p
; p
++) {
125 int get_terminal_type(const char **type
)
127 *type
= getenv("TERM");
133 #ifdef TEST_PROGRAM_TTYUTILS
137 const char *path
, *name
, *num
;
140 if (get_terminal_name(&path
, &name
, &num
) == 0) {
141 fprintf(stderr
, "tty path: %s\n", path
);
142 fprintf(stderr
, "tty name: %s\n", name
);
143 fprintf(stderr
, "tty number: %s\n", num
);
145 get_terminal_dimension(&c
, &l
);
146 fprintf(stderr
, "tty cols: %d\n", c
);
147 fprintf(stderr
, "tty lines: %d\n", l
);
152 #endif /* TEST_PROGRAM_TTYUTILS */