From 1164453a32238755613a454ef55ae853ff3f4c96 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Tue, 28 Sep 2021 18:05:15 +0200
Subject: [PATCH 29/34] shutdown: when dying, do not install/restore a handler
 for Ctrl+C

First, we don't want the writing of an emergency file to be interrupted
by the user.  But more important: the routine for restoring the handler
also disables SIGINT, which would leave the terminal with a non-working
Ctrl+C.

Saving an emergency file calls write_file() in a unique manner: with
thefile == NULL, fullbuffer == FALSE (even though the entire buffer
will be saved, of course) and tmp == TRUE (even though it is not a
temporary file, as it will persist after nano exits).  But in fact
we want the handler for Ctrl+C installed only for normal files, not
for temporary files and not for emergency files -- the user should
not be able to interrupt the writing of those.

This fixes https://savannah.gnu.org/bugs/?61237.

Bug existed since version 4.3, commit 8550c6bd.
---
 src/files.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/files.c b/src/files.c
index 271099d0..ab9957c9 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1814,7 +1814,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
 
 #ifndef NANO_TINY
 		block_sigwinch(TRUE);
-		install_handler_for_Ctrl_C();
+		if (!tmp)
+			install_handler_for_Ctrl_C();
 #endif
 
 		/* Now open the file.  Use O_EXCL for an emergency file. */
@@ -1822,7 +1823,8 @@ bool write_file(const char *name, FILE *thefile, bool tmp,
 					O_APPEND : (tmp ? O_EXCL : O_TRUNC)), permissions);
 
 #ifndef NANO_TINY
-		restore_handler_for_Ctrl_C();
+		if (!tmp)
+			restore_handler_for_Ctrl_C();
 		block_sigwinch(FALSE);
 #endif
 
-- 
2.29.3

