From bfe418febb1cd2a6087f92fefce5421b11f8a5ba Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 23 Apr 2016 18:13:43 +0200
Subject: [PATCH] files: when opening a buffer, make sure to expand a tilde in
 its name

When a tilde is used in the name given at the ^R or ^O prompts, nano
expands it, but /not/ when a tilde is given on the command line (in
such a way that the shell leaves it as is).  Correct that asymmetry.

This fixes https://savannah.gnu.org/bugs/?44929
and fixes https://savannah.gnu.org/bugs/?47702
and fixes https://savannah.gnu.org/bugs/?47771.
---
 src/files.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/files.c b/src/files.c
index 523e75dd..fceb7bc5 100644
--- a/src/files.c
+++ b/src/files.c
@@ -418,6 +418,8 @@ bool open_buffer(const char *filename, bool undoable)
 #endif
 	);
 	/* Whether we load into this buffer or a new one. */
+    char *realname;
+	/* The filename after tilde expansion. */
     FILE *f;
     int rc;
 	/* rc == -2 means that we have a new file.  -1 means that the
@@ -433,17 +435,20 @@ bool open_buffer(const char *filename, bool undoable)
     }
 #endif
 
+    realname = real_dir_from_tilde(filename);
+
     /* When the specified filename is not empty, and the thing exists,
      * verify that it is a normal file. */
     if (strcmp(filename, "") != 0) {
 	struct stat fileinfo;
 
-	if (stat(filename, &fileinfo) == 0 && !S_ISREG(fileinfo.st_mode)) {
+	if (stat(realname, &fileinfo) == 0 && !S_ISREG(fileinfo.st_mode)) {
 	    if (S_ISDIR(fileinfo.st_mode))
-		statusbar(_("\"%s\" is a directory"), filename);
+		statusbar(_("\"%s\" is a directory"), realname);
 	    else
-		statusbar(_("\"%s\" is not a normal file"), filename);
+		statusbar(_("\"%s\" is not a normal file"), realname);
 	    beep();
+	    free(realname);
 	    return FALSE;
 	}
     }
@@ -453,16 +458,17 @@ bool open_buffer(const char *filename, bool undoable)
     if (new_buffer) {
 	make_new_buffer();
 
-	if (!has_valid_path(filename))
+	if (!has_valid_path(realname))
 	    quiet = TRUE;
 #ifndef NANO_TINY
 	else {
 	    if (ISSET(LOCKING) && filename[0] != '\0') {
-		int lockstatus = do_lockfile(filename);
+		int lockstatus = do_lockfile(realname);
 		if (lockstatus < 0) {
 #ifndef DISABLE_MULTIBUFFER
 		    if (openfile->next) {
 			close_buffer(TRUE);
+			free(realname);
 			return FALSE;
 		    }
 #endif
@@ -476,20 +482,20 @@ bool open_buffer(const char *filename, bool undoable)
     /* If the filename isn't blank, and we are not in NOREAD_MODE,
      * open the file.  Otherwise, treat it as a new file. */
     rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ?
-		open_file(filename, new_buffer, quiet, &f) : -2;
+		open_file(realname, new_buffer, quiet, &f) : -2;
 
     /* If we have a file, and we're loading into a new buffer, update
      * the filename. */
     if (rc != -1 && new_buffer)
-	openfile->filename = mallocstrcpy(openfile->filename, filename);
+	openfile->filename = mallocstrcpy(openfile->filename, realname);
 
     /* If we have a non-new file, read it in.  Then, if the buffer has
      * no stat, update the stat, if applicable. */
     if (rc > 0) {
-	read_file(f, rc, filename, undoable, new_buffer);
+	read_file(f, rc, realname, undoable, new_buffer);
 #ifndef NANO_TINY
 	if (openfile->current_stat == NULL)
-	    stat_with_alloc(filename, &openfile->current_stat);
+	    stat_with_alloc(realname, &openfile->current_stat);
 #endif
     }
 
@@ -507,6 +513,7 @@ bool open_buffer(const char *filename, bool undoable)
     if (new_buffer)
 	color_update();
 #endif
+    free(realname);
     return TRUE;
 }
 
-- 
GitLab