summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/ttyutils.h2
-rw-r--r--lib/ttyutils.c15
-rw-r--r--login-utils/login.c2
-rw-r--r--login-utils/su-common.c4
-rw-r--r--term-utils/write.c16
5 files changed, 20 insertions, 19 deletions
diff --git a/include/ttyutils.h b/include/ttyutils.h
index 200e9a565..7278d3615 100644
--- a/include/ttyutils.h
+++ b/include/ttyutils.h
@@ -51,7 +51,7 @@ struct chardata {
} while (0)
extern int get_terminal_width(int default_width);
-extern int get_terminal_name(int fd, const char **path, const char **name,
+extern int get_terminal_name(const char **path, const char **name,
const char **number);
#define UL_TTY_KEEPCFLAGS (1 << 1)
diff --git a/lib/ttyutils.c b/lib/ttyutils.c
index 4e62c20d6..06dd800f7 100644
--- a/lib/ttyutils.c
+++ b/lib/ttyutils.c
@@ -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;
diff --git a/login-utils/login.c b/login-utils/login.c
index 4d71f5424..7501f6d69 100644
--- a/login-utils/login.c
+++ b/login-utils/login.c
@@ -356,7 +356,7 @@ static void init_tty(struct login_context *cxt)
cxt->tty_mode = (mode_t) getlogindefs_num("TTYPERM", TTY_MODE);
- get_terminal_name(0, &cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
+ get_terminal_name(&cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
/*
* In case login is suid it was possible to use a hardlink as stdin
diff --git a/login-utils/su-common.c b/login-utils/su-common.c
index 78a734c67..1776b6b79 100644
--- a/login-utils/su-common.c
+++ b/login-utils/su-common.c
@@ -165,7 +165,7 @@ log_syslog(struct passwd const *pw, bool successful)
old_user = pwd ? pwd->pw_name : "";
}
- if (get_terminal_name(STDERR_FILENO, NULL, &tty, NULL) != 0 || !tty)
+ if (get_terminal_name(NULL, &tty, NULL) != 0 || !tty)
tty = "none";
openlog (program_invocation_short_name, 0 , LOG_AUTH);
@@ -192,7 +192,7 @@ static void log_btmp(struct passwd const *pw)
pw && pw->pw_name ? pw->pw_name : "(unknown)",
sizeof(ut.ut_user));
- get_terminal_name(STDERR_FILENO, NULL, &tty_name, &tty_num);
+ get_terminal_name(NULL, &tty_name, &tty_num);
if (tty_num)
xstrncpy(ut.ut_id, tty_num, sizeof(ut.ut_id));
if (tty_name)
diff --git a/term-utils/write.c b/term-utils/write.c
index be91218cf..6bd393441 100644
--- a/term-utils/write.c
+++ b/term-utils/write.c
@@ -296,7 +296,7 @@ static void do_write(const struct write_control *ctl)
int main(int argc, char **argv)
{
- int tty_writeable = 0, src_fd, c;
+ int tty_writeable = 0, c;
struct write_control ctl = { 0 };
static const struct option longopts[] = {
@@ -321,18 +321,8 @@ int main(int argc, char **argv)
usage(stderr);
}
- /* check that sender has write enabled */
- if (isatty(STDIN_FILENO))
- src_fd = STDIN_FILENO;
- else if (isatty(STDOUT_FILENO))
- src_fd = STDOUT_FILENO;
- else if (isatty(STDERR_FILENO))
- src_fd = STDERR_FILENO;
- else
- src_fd = -1;
-
- if (src_fd != -1 &&
- get_terminal_name(src_fd, &ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+ if (get_terminal_name(&ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
+ /* check that sender has write enabled */
if (check_tty(ctl.src_tty_path, &tty_writeable, NULL, 1))
exit(EXIT_FAILURE);
if (!tty_writeable)