From c1a484270b36e0adf0e213f45af628d814e72ee0 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sun, 27 Nov 2016 18:21:04 +0100
Subject: [PATCH] rcfile: actually avoid opening directories and devices

Achieve this elegantly by factoring out the reading of one rcfile.
---
 src/nano.c   |  2 +-
 src/proto.h  |  2 +-
 src/rcfile.c | 43 +++++++++++++++++++++----------------------
 3 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index 7204b45d..573f8219 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2360,7 +2360,7 @@ int main(int argc, char **argv)
 	alt_speller = NULL;
 #endif
 
-	do_rcfile();
+	do_rcfiles();
 
 #ifdef DEBUG
 	fprintf(stderr, "After rebinding keys...\n");
diff --git a/src/proto.h b/src/proto.h
index 5de93ecb..f3c9b0ee 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -527,7 +527,7 @@ bool parse_color_names(char *combostr, short *fg, short *bg, bool *bright);
 void grab_and_store(const char *kind, char *ptr, regexlisttype **storage);
 #endif
 void parse_rcfile(FILE *rcstream, bool syntax_only);
-void do_rcfile(void);
+void do_rcfiles(void);
 #endif /* !DISABLE_NANORC */
 
 /* All functions in search.c. */
diff --git a/src/rcfile.c b/src/rcfile.c
index 2cbc4523..1ae8863d 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -1232,25 +1232,36 @@ void parse_rcfile(FILE *rcstream, bool syntax_only)
     return;
 }
 
-/* First read the system-wide rcfile, then the user's rcfile. */
-void do_rcfile(void)
+/* Read and interpret one of the two nanorc files. */
+void parse_one_nanorc(void)
 {
     FILE *rcstream;
 
-    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
-
-    /* Warn about directories, character files, or block files. */
+    /* Don't try to open directories nor devices. */
     if (!is_good_file(nanorc))
-	;
+	return;
 
 #ifdef DEBUG
-    fprintf(stderr, "Parsing file \"%s\"\n", nanorc);
+    fprintf(stderr, "Going to parse file \"%s\"\n", nanorc);
 #endif
 
-    /* Try to open the system-wide nanorc. */
     rcstream = fopen(nanorc, "rb");
+
+    /* If opening the file succeeded, parse it.  Otherwise, only
+     * complain if the file actually exists. */
     if (rcstream != NULL)
 	parse_rcfile(rcstream, FALSE);
+    else if (errno != ENOENT)
+	rcfile_error(N_("Error reading %s: %s"), nanorc, strerror(errno));
+}
+
+/* First read the system-wide rcfile, then the user's rcfile. */
+void do_rcfiles(void)
+{
+    nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
+
+    /* Process the system-wide nanorc. */
+    parse_one_nanorc();
 
     /* When configured with --disable-wrapping-as-root, turn wrapping off
      * for root, so that only root's .nanorc or --fill can turn it on. */
@@ -1267,25 +1278,13 @@ void do_rcfile(void)
 	nanorc = charealloc(nanorc, strlen(homedir) + strlen(RCFILE_NAME) + 2);
 	sprintf(nanorc, "%s/%s", homedir, RCFILE_NAME);
 
-	/* Warn about directories, character files, or block files. */
-	if (!is_good_file(nanorc))
-	    ;
-
-	/* Try to open the current user's nanorc. */
-	rcstream = fopen(nanorc, "rb");
-	if (rcstream == NULL) {
-	    /* Don't complain about the file's not existing. */
-	    if (errno != ENOENT)
-		rcfile_error(N_("Error reading %s: %s"), nanorc,
-				strerror(errno));
-	} else
-	    parse_rcfile(rcstream, FALSE);
+	/* Process the current user's nanorc. */
+	parse_one_nanorc();
     }
 
     check_vitals_mapped();
 
     free(nanorc);
-    nanorc = NULL;
 
     if (errors && !ISSET(QUIET)) {
 	errors = FALSE;
-- 
GitLab