From f0d368559178185943c0c1561da8e9b158ad0af1 Mon Sep 17 00:00:00 2001
From: Lion Yang <lion@aosc.io>
Date: Wed, 8 Nov 2017 13:49:48 +0800
Subject: [PATCH] input: ensure that standard input uses blocking mode

The function get_key_buffer() assumes waiting_mode = TRUE, but stdin
can be in non-blocking mode when a program (before nano) turned stdin
to non-blocking mode and did not change it back (possibly because it
crashed).  So, explicitly set stdin to blocking mode at startup.

Signed-off-by: Lion Yang <lion@aosc.io>
---
 src/nano.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/nano.c b/src/nano.c
index 15d8d546..018cd88d 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1887,7 +1887,7 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
 
 int main(int argc, char **argv)
 {
-    int optchr;
+    int stdin_flags, optchr;
 #if defined(ENABLED_WRAPORJUSTIFY) && defined(ENABLE_NANORC)
     bool fill_used = FALSE;
 	/* Was the fill option used on the command line? */
@@ -1978,6 +1978,11 @@ int main(int argc, char **argv)
     /* Back up the terminal settings so that they can be restored. */
     tcgetattr(0, &oldterm);
 
+    /* Get the state of standard input and ensure it uses blocking mode. */
+    stdin_flags = fcntl(0, F_GETFL, 0);
+    if (stdin_flags != -1)
+	fcntl(0, F_SETFL, stdin_flags & ~O_NONBLOCK);
+
 #ifdef ENABLE_UTF8
     /* If setting the locale is successful and it uses UTF-8, we need
      * to use the multibyte functions for text processing. */
-- 
GitLab