From ec6014a0e55eb1bb67b72bb9b2448a5352f99e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Fri, 12 Aug 2022 14:15:54 -0300 Subject: [PATCH] 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. --- ef.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ef.c b/ef.c index 8a172df..719c4a5 100644 --- a/ef.c +++ b/ef.c @@ -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) {