lib: try to find tty in get_terminal_name()

Try all standard terminal input/output file descriptors when finding tty
name in get_germinal_name().  This should make all invocations of the
function as robust as they can get.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2016-05-14 19:50:41 +01:00
parent a58b90255c
commit 285c1f3a3e
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
5 changed files with 20 additions and 19 deletions

View file

@ -5,6 +5,7 @@
* Written by Karel Zak <kzak@redhat.com>
*/
#include <ctype.h>
#include <unistd.h>
#include "c.h"
#include "ttyutils.h"
@ -42,13 +43,14 @@ int get_terminal_width(int default_width)
return width > 0 ? width : default_width;
}
int get_terminal_name(int fd,
const char **path,
int get_terminal_name(const char **path,
const char **name,
const char **number)
{
const char *tty;
const char *p;
int fd;
if (name)
*name = NULL;
@ -57,6 +59,15 @@ int get_terminal_name(int fd,
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;
tty = ttyname(fd);
if (!tty)
return -1;