From a0ce79777593df684230de9fb38a0ea73f297e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Fri, 12 Aug 2022 05:36:41 -0300 Subject: [PATCH] Use /dev/tty instead of stderr for interactivity. --- ef.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ef.c b/ef.c index 918b534..ef3e975 100644 --- a/ef.c +++ b/ef.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -49,16 +50,18 @@ int main() sort_entries(&entries); filter_entries(&entries, NULL); - int tmp_fd; - /* use stderr for input instead of stdin, since we get the entries from stdin */ - if (dup2(STDERR_FILENO, STDIN_FILENO) != STDIN_FILENO || - /* use stderr for output as well, since we should only print the result to stdout */ - (tmp_fd = dup(STDOUT_FILENO)) < 0 || - (stdout_save = fdopen(tmp_fd, "w")) == NULL || - dup2(STDERR_FILENO, STDOUT_FILENO) != STDOUT_FILENO) { + /* use the controlling terminal as new stdin/stdout, + * since the process ones are used for input and output */ + int tty_fd, stdout_save_fd; + if ((tty_fd = open("/dev/tty", O_RDWR | O_CLOEXEC)) < 0 || + (stdout_save_fd = dup(STDOUT_FILENO)) < 0 || + (stdout_save = fdopen(stdout_save_fd, "we")) == NULL || + dup2(tty_fd, STDIN_FILENO) != STDIN_FILENO || + dup2(tty_fd, STDOUT_FILENO) != STDOUT_FILENO) { perror("fd dance"); exit(1); } + close(tty_fd); atexit(print_name);