Duplicate stdin fd before reading from it.

Otherwise, stdin will have an error state of EOF, leading to wgetch()
returning -1, which is unexpected, given that we polled the file
descriptor for data.
This commit is contained in:
Érico Nogueira 2022-08-12 14:15:54 -03:00
parent 8f38a81690
commit ec6014a0e5
1 changed files with 12 additions and 1 deletions

13
ef.c
View File

@ -66,7 +66,18 @@ int main(int argc, char **argv)
}
struct str_array entries = { 0 };
read_entries_from_stream(&entries, delim, stdin);
/* create our own stream for standard input,
* so stdin is fresh when we use it with our new file descriptor below */
int stdin_dup;
FILE *original_stdin;
if ((stdin_dup = dup(STDIN_FILENO)) < 0 ||
(original_stdin = fdopen(stdin_dup, "r")) == NULL) {
perror("stdin handling");
exit(1);
}
read_entries_from_stream(&entries, delim, original_stdin);
fclose(original_stdin);
/* fast exit cases */
if (entries.n == 0) exit(0);
if (entries.n == 1) {