tReopen tty input to stdin in case input was a pipe - sacc - sacc (saccomys): simple gopher client.
Log
Files
Refs
LICENSE
---
commit 078d0408656082f0cff4dd7f2b39dc988bc2391e
parent f2c03523d69f506dd4f40f4258ed8e37c5f008ca
Author: Quentin Rameau 
Date:   Tue, 29 Aug 2017 14:38:32 +0200

Reopen tty input to stdin in case input was a pipe

Thanks to trqx for reporting it and to Hiltjo for the fix suggestion!

Diffstat:
  sacc.c                              |       8 ++++++++

1 file changed, 8 insertions(+), 0 deletions(-)
---
diff --git a/sacc.c b/sacc.c
t@@ -744,8 +744,16 @@ cleanup(void)
 static void
 setup(void)
 {
+        int fd;
+
         setenv("PAGER", "more", 0);
         atexit(cleanup);
+        /* reopen stdin in case we're reading from a pipe */
+        if ((fd = open("/dev/tty", O_RDONLY)) < 0)
+                die("open: /dev/tty: %s", strerror(errno));
+        if (dup2(fd, 0) < 0)
+                die("dup2: /dev/tty, stdin: %s", strerror(errno));
+        close(fd);
         if ((devnullfd = open("/dev/null", O_WRONLY)) < 0)
                 die("open: /dev/null: %s", strerror(errno));
         if (mkdir("/tmp/sacc", S_IRWXU) < 0 && errno != EEXIST)