From db9d983646c6369201baf37e44039531bab257d7 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Wed, 14 Apr 2010 06:03:12 +0000
Subject: [PATCH] 2010-04-14 Chris Allegretta <chrisa@asty.org>         *
 files.c (wirte_file): Don't set current_stat when tmp == TRUE, check         
  whether current_stat is set when trying to use it, and don't do the         
  modification check if the filename changed, since we have no way          
 of knowing about it in that case.  Fixes Savannah bug 29392, reported        
   by Mike Frysinger.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4500 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  7 ++++++-
 src/files.c | 17 +++++++++--------
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 03b19c06..47a64321 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,11 @@
-2010-04-09 Chris Allegretta <chrisa@asty.org>
+2010-04-14 Chris Allegretta <chrisa@asty.org>
 	* text.c (do_alt_speller): Skip invoking the alt speller if the file size 
 	  is 0 bytes.  Fixes Savannah bug 29393 reported by Mike Frysinger.
+	* files.c (wirte_file): Don't set current_stat when tmp == TRUE, check
+	  whether current_stat is set when trying to use it, and don't do the
+	  modification check if the filename changed, since we have no way
+	  of knowing about it in that case.  Fixes Savannah bug 29392, reported
+	  by Mike Frysinger.
 
 2010-04-13 Felipe Bugno <necron@bol.com.br>
 	* doc/syntax/cmake.nanorc: Added cmake syntax highlighting file.
diff --git a/src/files.c b/src/files.c
index bbb06977..07915ce8 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1501,11 +1501,11 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
 
 #ifndef NANO_TINY
     /* if we have not stat()d this file before (say, the user just
-     * specified it interactively), use the info we just got from
-     * stat()ing or else we will chase null pointers when we do
+     * specified it interactively), stat and save the value
+     * or else we will chase null pointers when we do
      * modtime checks, preserve file times, etc. during backup */
-    if (openfile->current_stat == NULL && realexists)
-	openfile->current_stat = &st;
+    if (openfile->current_stat == NULL && !tmp && realexists)
+	stat(realname, openfile->current_stat);
 
     /* We backup only if the backup toggle is set, the file isn't
      * temporary, and the file already exists.  Furthermore, if we
@@ -1513,8 +1513,8 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
      * only if the file has not been modified by someone else since nano
      * opened it. */
     if (ISSET(BACKUP_FILE) && !tmp && realexists && ((append !=
-	OVERWRITE || openfile->mark_set) ||
-	openfile->current_stat->st_mtime == st.st_mtime)) {
+	OVERWRITE || openfile->mark_set) || (openfile->current_stat &&
+	openfile->current_stat->st_mtime == st.st_mtime))) {
 	int backup_fd;
 	FILE *backup_file;
 	char *backupname;
@@ -2141,8 +2141,9 @@ bool do_writeout(bool exiting)
 		    }
 		}
 #ifndef NANO_TINY
-
-		if (name_exists && openfile->current_stat && (openfile->current_stat->st_mtime < st.st_mtime ||
+		/* Complain if the file exists, the name hasn't changed, and the
+		    stat information we had before does not match what we have now */
+		else if (name_exists && openfile->current_stat && (openfile->current_stat->st_mtime < st.st_mtime ||
                     openfile->current_stat->st_dev != st.st_dev || openfile->current_stat->st_ino != st.st_ino)) {
 		    i = do_yesno_prompt(FALSE,
 			_("File was modified since you opened it, continue saving ? "));
-- 
2.39.5