} 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)
* Written by Karel Zak <kzak@redhat.com>
*/
#include <ctype.h>
+#include <unistd.h>
#include "c.h"
#include "ttyutils.h"
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;
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;
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
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);
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)
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[] = {
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)