From 049e4a5db33882d78ffed666554d56f1fbdf8a0f Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Tue, 10 Aug 2004 23:05:59 +0000
Subject: [PATCH] per DB's patch, simplify the saving of emergency files in
 die(), die_save_file(), and get_next_filename()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1889 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  8 ++++++++
 src/files.c | 18 ++++++++++--------
 src/nano.c  | 12 +++---------
 3 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4869071d..93b4a7e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -84,6 +84,9 @@ CVS code -
 	  when compiling with every option manually turned on, including
 	  NANO_SMALL. (David Benbennick)
 - files.c:
+  get_next_filename()
+	- Tweak for efficiency, and add the ".save" suffix to the file
+	  here. (David Benbennick)
   close_open_file()
 	- Tweak to no longer rely on the return values of
 	  open_(prev|next)file(). (DLR)
@@ -107,6 +110,11 @@ CVS code -
   thanks_for_all_the_fish()
 	- Delete topwin, edit, and bottomwin. (David Benbennick)
 - nano.c:
+  die()
+	- Don't add the ".save" suffix to a saved file here anymore, 
+	  since get_next_filename() does that now. (David Benbennick)
+  die_save_file()
+	- Tweak for efficiency. (David Benbennick)
   help_init()
 	- Fix the display of the translated key descriptions "Up" and
 	  "Space" under all circumstances, and make the help browser
diff --git a/src/files.c b/src/files.c
index a4246f02..59a54a96 100644
--- a/src/files.c
+++ b/src/files.c
@@ -394,19 +394,22 @@ bool open_file(const char *filename, int insert, int quiet)
 }
 
 /* This function will return the name of the first available extension
- * of a filename (starting with the filename, then filename.1, etc).
- * Memory is allocated for the return value.  If no writable extension
- * exists, we return "". */
+ * of a filename (starting with the filename.save, then filename.save.1,
+ * etc).  Memory is allocated for the return value.  If no writable
+ * extension exists, we return "". */
 char *get_next_filename(const char *name)
 {
     int i = 0;
-    char *buf = NULL;
-    struct stat fs;
+    char *buf;
+    size_t namelen = strlen(name);
 
-    buf = charalloc(strlen(name) + num_of_digits(INT_MAX) + 2);
+    buf = charalloc(namelen + num_of_digits(INT_MAX) + 7);
     strcpy(buf, name);
+    strcpy(buf + namelen, ".save");
+    namelen += 5;
 
     while (TRUE) {
+	struct stat fs;
 
 	if (stat(buf, &fs) == -1)
 	    return buf;
@@ -414,8 +417,7 @@ char *get_next_filename(const char *name)
 	    break;
 
 	i++;
-	strcpy(buf, name);
-	sprintf(&buf[strlen(name)], ".%d", i);
+	sprintf(buf + namelen, ".%d", i);
     }
 
     /* We get here only if there is no possible save file. */
diff --git a/src/nano.c b/src/nano.c
index 2d02fa0f..0f5fce45 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -104,7 +104,7 @@ void finish(void)
     exit(0);
 }
 
-/* Die (gracefully?) */
+/* Die (gracefully?). */
 void die(const char *msg, ...)
 {
     va_list ap;
@@ -170,15 +170,9 @@ void die_save_file(const char *die_filename)
     /* If we can't save, we have REAL bad problems, but we might as well
        TRY. */
     if (die_filename[0] == '\0')
-	ret = get_next_filename("nano.save");
-    else {
-	char *buf = charalloc(strlen(die_filename) + 6);
+	die_filename = "nano";
 
-	strcpy(buf, die_filename);
-	strcat(buf, ".save");
-	ret = get_next_filename(buf);
-	free(buf);
-    }
+    ret = get_next_filename(die_filename);
     if (ret[0] != '\0')
 	failed = -1 == write_file(ret, TRUE, FALSE, TRUE);
 
-- 
GitLab