Use /dev/tty instead of stderr for interactivity.

This commit is contained in:
Érico Nogueira 2022-08-12 05:36:41 -03:00
parent 4e174d9126
commit a0ce797775
1 changed files with 10 additions and 7 deletions

17
ef.c
View File

@ -3,6 +3,7 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
@ -49,16 +50,18 @@ int main()
sort_entries(&entries); sort_entries(&entries);
filter_entries(&entries, NULL); filter_entries(&entries, NULL);
int tmp_fd; /* use the controlling terminal as new stdin/stdout,
/* use stderr for input instead of stdin, since we get the entries from stdin */ * since the process ones are used for input and output */
if (dup2(STDERR_FILENO, STDIN_FILENO) != STDIN_FILENO || int tty_fd, stdout_save_fd;
/* use stderr for output as well, since we should only print the result to stdout */ if ((tty_fd = open("/dev/tty", O_RDWR | O_CLOEXEC)) < 0 ||
(tmp_fd = dup(STDOUT_FILENO)) < 0 || (stdout_save_fd = dup(STDOUT_FILENO)) < 0 ||
(stdout_save = fdopen(tmp_fd, "w")) == NULL || (stdout_save = fdopen(stdout_save_fd, "we")) == NULL ||
dup2(STDERR_FILENO, STDOUT_FILENO) != STDOUT_FILENO) { dup2(tty_fd, STDIN_FILENO) != STDIN_FILENO ||
dup2(tty_fd, STDOUT_FILENO) != STDOUT_FILENO) {
perror("fd dance"); perror("fd dance");
exit(1); exit(1);
} }
close(tty_fd);
atexit(print_name); atexit(print_name);