>From 32a0a344b3545638830b4986c63c40c5ee6c17ee Mon Sep 17 00:00:00 2001
From: Brand Huntsman
Date: Mon, 13 May 2019 23:00:22 -0600
Subject: [PATCH 3/3] files: check for an empty FIFO before blocking on it
Prevents nano from hanging when user accidentally opens an empty FIFO.
Signed-off-by: Brand Huntsman
---
src/files.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/files.c b/src/files.c
index 07403853..922cf801 100644
--- a/src/files.c
+++ b/src/files.c
@@ -29,6 +29,7 @@
#endif
#include
#include
+#include
#define LOCKBUFSIZE 8192
@@ -976,7 +977,17 @@ int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
}
/* Try opening the file. */
- fd = open(full_filename, O_RDONLY);
+ if (S_ISFIFO(fileinfo.st_mode)) {
+ fd = open(full_filename, O_RDONLY | O_NONBLOCK);
+ if (fd != -1) {
+ struct pollfd fds = { fd, POLLIN, 0 };
+ if (poll(&fds, 1, 0) == 1) {
+ close(fd);
+ fd = open(full_filename, O_RDONLY);
+ }
+ }
+ } else
+ fd = open(full_filename, O_RDONLY);
if (fd == -1)
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
--
2.21.0