diff options
author | Karel Zak | 2017-06-12 11:38:38 +0200 |
---|---|---|
committer | Karel Zak | 2017-06-12 11:38:38 +0200 |
commit | f46a8d7e6636b7cc1e56bd0010c3950e2a11cbd2 (patch) | |
tree | a76ed8ae42b214c6477e30b82a7fa43ca46f1720 /lib | |
parent | docs: update TODO, remove auto,noauto (diff) | |
download | kernel-qcow2-util-linux-f46a8d7e6636b7cc1e56bd0010c3950e2a11cbd2.tar.gz kernel-qcow2-util-linux-f46a8d7e6636b7cc1e56bd0010c3950e2a11cbd2.tar.xz kernel-qcow2-util-linux-f46a8d7e6636b7cc1e56bd0010c3950e2a11cbd2.zip |
lib/ttyutils: return terminal lines too
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ttyutils.c | 68 |
1 files changed, 49 insertions, 19 deletions
diff --git a/lib/ttyutils.c b/lib/ttyutils.c index 613e5af65..6a1e0e0f3 100644 --- a/lib/ttyutils.c +++ b/lib/ttyutils.c @@ -10,35 +10,61 @@ #include "c.h" #include "ttyutils.h" -int get_terminal_width(int default_width) + +static int get_env_int(const char *name) { - int width = 0; + const char *cp = getenv(name); + + if (cp) { + char *end = NULL; + long x; + + errno = 0; + x = strtol(cp, &end, 10); + + if (errno == 0 && end && *end == '\0' && end > cp && + x > 0 && x <= INT_MAX) + return x; + } + + return -1; +} + +int get_terminal_dimension(int *cols, int *lines) +{ + int c = 0, l = 0; #if defined(TIOCGWINSZ) struct winsize w_win; - if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w_win) == 0) - width = w_win.ws_col; + if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w_win) == 0) { + c = w_win.ws_col; + l = w_win.ws_row; + } #elif defined(TIOCGSIZE) struct ttysize t_win; - if (ioctl (STDOUT_FILENO, TIOCGSIZE, &t_win) == 0) - width = t_win.ts_cols; + if (ioctl (STDOUT_FILENO, TIOCGSIZE, &t_win) == 0) { + c = t_win.ts_cols; + l = t_win.ts_lines; + } #endif - if (width <= 0) { - const char *cp = getenv("COLUMNS"); + if (cols && c <= 0) + c = get_env_int("COLUMNS"); + if (lines && l <= 0) + l = get_env_int("LINES"); - if (cp) { - char *end = NULL; - long c; + if (cols) + *cols = c; + if (lines) + *lines = l; + return 0; +} - errno = 0; - c = strtol(cp, &end, 10); +int get_terminal_width(int default_width) +{ + int width = 0; - if (errno == 0 && end && *end == '\0' && end > cp && - c > 0 && c <= INT_MAX) - width = c; - } - } + get_terminal_dimension(&width, NULL); return width > 0 ? width : default_width; } @@ -93,13 +119,17 @@ int get_terminal_name(const char **path, int main(void) { const char *path, *name, *num; + int c, l; if (get_terminal_name(&path, &name, &num) == 0) { fprintf(stderr, "tty path: %s\n", path); fprintf(stderr, "tty name: %s\n", name); fprintf(stderr, "tty number: %s\n", num); } - fprintf(stderr, "tty width: %d\n", get_terminal_width(0)); + get_terminal_dimension(&c, &l); + fprintf(stderr, "tty cols: %d\n", c); + fprintf(stderr, "tty lines: %d\n", l); + return EXIT_SUCCESS; } |