diff options
author | Karel Zak | 2019-05-27 12:49:12 +0200 |
---|---|---|
committer | Karel Zak | 2019-05-27 12:49:12 +0200 |
commit | a6d9d23b5f9e3c1d421c1f32a3f1dffe8452cb74 (patch) | |
tree | dfcba7aa547a7adeb25cce9ce4ab85a0e28458b6 /lib/ttyutils.c | |
parent | libblkid: remove unneeded fields from struct bcache_super_block (diff) | |
download | kernel-qcow2-util-linux-a6d9d23b5f9e3c1d421c1f32a3f1dffe8452cb74.tar.gz kernel-qcow2-util-linux-a6d9d23b5f9e3c1d421c1f32a3f1dffe8452cb74.tar.xz kernel-qcow2-util-linux-a6d9d23b5f9e3c1d421c1f32a3f1dffe8452cb74.zip |
lib/ttyutils: introduce get_terminal_stdfd()
Let's use separate function to detect what is the current terminal.
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/ttyutils.c')
-rw-r--r-- | lib/ttyutils.c | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/lib/ttyutils.c b/lib/ttyutils.c index 00a7903ca..166e49e11 100644 --- a/lib/ttyutils.c +++ b/lib/ttyutils.c @@ -69,6 +69,18 @@ int get_terminal_width(int default_width) return width > 0 ? width : default_width; } +int get_terminal_stdfd(void) +{ + if (isatty(STDIN_FILENO)) + return STDIN_FILENO; + else if (isatty(STDOUT_FILENO)) + return STDOUT_FILENO; + else if (isatty(STDERR_FILENO)) + return STDERR_FILENO; + + return -EINVAL; +} + int get_terminal_name(const char **path, const char **name, const char **number) @@ -85,21 +97,18 @@ int get_terminal_name(const char **path, if (number) *number = NULL; - if (isatty(STDIN_FILENO)) - fd = STDIN_FILENO; - else if (isatty(STDOUT_FILENO)) - fd = STDOUT_FILENO; - else if (isatty(STDERR_FILENO)) - fd = STDERR_FILENO; - else - return -1; + fd = get_terminal_stdfd(); + if (fd < 0) + return fd; /* error */ tty = ttyname(fd); if (!tty) return -1; + if (path) *path = tty; - tty = strncmp(tty, "/dev/", 5) == 0 ? tty + 5 : tty; + if (name || number) + tty = strncmp(tty, "/dev/", 5) == 0 ? tty + 5 : tty; if (name) *name = tty; if (number) { |