From e1ebaf3821b2afdf2ab608d2f565c0011410f940 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Sun, 7 Jan 2001 05:50:36 +0000
Subject: [PATCH] do_writeout() takes an arg now, and mallocstrcpy now gives up
 if src == dest

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@453 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog |  4 ++++
 files.c   | 18 +++++++++++-------
 nano.c    |  2 +-
 proto.h   |  2 +-
 utils.c   |  4 ++++
 5 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5675cd3d..85ee292b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -20,6 +20,9 @@ General -
 	- removed skipping . and .. when tabulating matches.
 	- Added the (char *) sizeof when allocating memory for the filename
 	  array (Rocco).
+  do_writeout()
+	- Now takes an argument so the string typed in can be retained
+	  when calling the browser.
   do_browser()
 	- Don't decrement longest by the length of path.  Fixes crashes
 	  on entering various dirs  (Rocco).
@@ -40,6 +43,7 @@ General -
   mallocstrcpy()
 	- Takes char pointers now instead of void (makes debugging a
 	  helluva lot easier)
+	- Duh, don't do anything if src == dest!
 - es.po:
 	- Updates for file browser (Jordi).
 
diff --git a/files.c b/files.c
index f325166b..1a1c2eee 100644
--- a/files.c
+++ b/files.c
@@ -374,7 +374,7 @@ int write_file(char *name, int tmp)
 	if (fd == -1) {
 	    if (!tmp && ISSET(TEMP_OPT)) {
 		UNSET(TEMP_OPT);
-		return do_writeout(1);
+		return do_writeout(filename, 1);
 	    }
 	    statusbar(_("Could not open file for writing: %s"),
 		      strerror(errno));
@@ -391,7 +391,7 @@ int write_file(char *name, int tmp)
 	if ((fd = mkstemp(buf)) == -1) {
 	    if (ISSET(TEMP_OPT)) {
 		UNSET(TEMP_OPT);
-		return do_writeout(1);
+		return do_writeout(filename, 1);
 	    }
 	    statusbar(_("Could not open file for writing: %s"),
 		      strerror(errno));
@@ -495,15 +495,16 @@ int write_file(char *name, int tmp)
     return 1;
 }
 
-int do_writeout(int exiting)
+int do_writeout(char *path, int exiting)
 {
     int i = 0;
 
 #ifdef NANO_EXTRA
     static int did_cred = 0;
 #endif
+fprintf(stderr, "answer = %s, path = %s\n", answer, path);
 
-    answer = mallocstrcpy(answer, filename);
+    answer = mallocstrcpy(answer, path);
 
     if ((exiting) && (ISSET(TEMP_OPT))) {
 	if (filename[0]) {
@@ -532,8 +533,11 @@ int do_writeout(int exiting)
 
 	    if (tmp != NULL)
 		answer = mallocstrcpy(answer, tmp);
-	    else
-		return do_writeout(exiting);
+	    else {
+fprintf(stderr, "Answer = %s\n", answer);
+
+		return do_writeout(answer, exiting);
+	    }
 	}
 #endif
 
@@ -572,7 +576,7 @@ int do_writeout(int exiting)
 
 int do_writeout_void(void)
 {
-    return do_writeout(0);
+    return do_writeout(filename, 0);
 }
 
 #ifndef DISABLE_TABCOMP
diff --git a/nano.c b/nano.c
index 8370de53..dc775d60 100644
--- a/nano.c
+++ b/nano.c
@@ -1421,7 +1421,7 @@ int do_exit(void)
 #endif
 
     if (i == 1) {
-	if (do_writeout(1) > 0)
+	if (do_writeout(filename, 1) > 0)
 	    finish(0);
     } else if (i == 0)
 	finish(0);
diff --git a/proto.h b/proto.h
index 96965dde..c9709701 100644
--- a/proto.h
+++ b/proto.h
@@ -81,7 +81,7 @@ int do_uncut_text(void);
 int no_help(void);
 int renumber_all(void);
 int open_file(char *filename, int insert, int quiet);
-int do_writeout(int exiting);
+int do_writeout(char *path, int exiting);
 int do_gotoline(long defline);
 int do_replace_loop(char *prevanswer, filestruct *begin, int *beginx,
 			int wholewords, int *i);
diff --git a/utils.c b/utils.c
index f624a103..a1396419 100644
--- a/utils.c
+++ b/utils.c
@@ -126,6 +126,10 @@ void *nrealloc(void *ptr, size_t howmuch)
 void *mallocstrcpy(char *dest, char *src)
 {
 
+
+    if (src == dest)
+	return src;
+
     if (dest != NULL)
 	free(dest);
 
-- 
GitLab