32888bf
diff -up patch-2.5.4/util.c.posix-backup patch-2.5.4/util.c
32888bf
--- patch-2.5.4/util.c.posix-backup	2008-06-16 10:22:52.000000000 +0100
32888bf
+++ patch-2.5.4/util.c	2008-06-16 11:30:27.000000000 +0100
32888bf
@@ -109,37 +109,40 @@ move_file (char const *from, int volatil
32888bf
 	    memory_fatal ();
32888bf
 	}
32888bf
 
32888bf
-      if (to_errno)
32888bf
+      if (strcmp (bakname, "/dev/null") != 0)
32888bf
 	{
32888bf
-	  int fd;
32888bf
+	  if (to_errno)
32888bf
+	    {
32888bf
+	      int fd;
32888bf
 
32888bf
-	  if (debug & 4)
32888bf
-	    say ("Creating empty unreadable file %s\n", quotearg (bakname));
32888bf
+	      if (debug & 4)
32888bf
+		say ("Creating empty unreadable file %s\n", quotearg (bakname));
32888bf
 
32888bf
-	  try_makedirs_errno = ENOENT;
32888bf
-	  unlink (bakname);
32888bf
-	  while ((fd = creat (bakname, 0)) < 0)
32888bf
-	    {
32888bf
-	      if (errno != try_makedirs_errno)
32888bf
-		pfatal ("Can't create file %s", quotearg (bakname));
32888bf
-	      makedirs (bakname);
32888bf
-	      try_makedirs_errno = 0;
32888bf
+	      try_makedirs_errno = ENOENT;
32888bf
+	      unlink (bakname);
32888bf
+	      while ((fd = creat (bakname, 0)) < 0)
32888bf
+		{
32888bf
+		  if (errno != try_makedirs_errno)
32888bf
+		    pfatal ("Can't create file %s", quotearg (bakname));
32888bf
+		  makedirs (bakname);
32888bf
+		  try_makedirs_errno = 0;
32888bf
+		}
32888bf
+	      if (close (fd) != 0)
32888bf
+		pfatal ("Can't close file %s", quotearg (bakname));
32888bf
 	    }
32888bf
-	  if (close (fd) != 0)
32888bf
-	    pfatal ("Can't close file %s", quotearg (bakname));
32888bf
-	}
32888bf
-      else
32888bf
-	{
32888bf
-	  if (debug & 4)
32888bf
-	    say ("Renaming file %s to %s\n",
32888bf
-		 quotearg_n (0, to), quotearg_n (1, bakname));
32888bf
-	  while (rename (to, bakname) != 0)
32888bf
+	  else
32888bf
 	    {
32888bf
-	      if (errno != try_makedirs_errno)
32888bf
-		pfatal ("Can't rename file %s to %s",
32888bf
-			quotearg_n (0, to), quotearg_n (1, bakname));
32888bf
-	      makedirs (bakname);
32888bf
-	      try_makedirs_errno = 0;
32888bf
+	      if (debug & 4)
32888bf
+		say ("Renaming file %s to %s\n",
32888bf
+		     quotearg_n (0, to), quotearg_n (1, bakname));
32888bf
+	      while (rename (to, bakname) != 0)
32888bf
+		{
32888bf
+		  if (errno != try_makedirs_errno)
32888bf
+		    pfatal ("Can't rename file %s to %s",
32888bf
+			    quotearg_n (0, to), quotearg_n (1, bakname));
32888bf
+		  makedirs (bakname);
32888bf
+		  try_makedirs_errno = 0;
32888bf
+		}
32888bf
 	    }
32888bf
 	}
32888bf
 
32888bf
diff -up patch-2.5.4/backupfile.c.posix-backup patch-2.5.4/backupfile.c
32888bf
--- patch-2.5.4/backupfile.c.posix-backup	2008-06-16 11:27:55.000000000 +0100
32888bf
+++ patch-2.5.4/backupfile.c	2008-06-16 11:44:05.000000000 +0100
32888bf
@@ -23,6 +23,8 @@
32888bf
 # include <config.h>
32888bf
 #endif
32888bf
 
32888bf
+#define XTERN extern
32888bf
+#include <common.h>
32888bf
 #include <argmatch.h>
32888bf
 #include <backupfile.h>
32888bf
 
32888bf
@@ -118,11 +120,15 @@ static int version_number PARAMS ((const
32888bf
 char *
32888bf
 find_backup_file_name (const char *file, enum backup_type backup_type)
32888bf
 {
32888bf
+  static char **previous_files = NULL;
32888bf
+  static int previous_files_allocated = 0;
32888bf
+
32888bf
   size_t backup_suffix_size_max;
32888bf
   size_t file_len = strlen (file);
32888bf
   size_t numbered_suffix_size_max = INT_STRLEN_BOUND (int) + 4;
32888bf
   char *s;
32888bf
   const char *suffix = simple_backup_suffix;
32888bf
+  int used_version = 0;
32888bf
 
32888bf
   /* Allow room for simple or `.~N~' backups.  */
32888bf
   backup_suffix_size_max = strlen (simple_backup_suffix) + 1;
32888bf
@@ -147,12 +153,66 @@ find_backup_file_name (const char *file,
32888bf
 	      char *numbered_suffix = s + (file_len + backup_suffix_size_max);
32888bf
 	      sprintf (numbered_suffix, ".~%d~", highest_backup + 1);
32888bf
 	      suffix = numbered_suffix;
32888bf
+	      used_version = 1;
32888bf
 	    }
32888bf
 	  strcpy (s, file);
32888bf
 	}
32888bf
 #endif /* HAVE_DIR */
32888bf
 
32888bf
-      addext (s, suffix, '~');
32888bf
+      if (used_version == 0)
32888bf
+	{
32888bf
+	  /* If we have already written a ".orig" backup file during
32888bf
+	     this run, don't overwrite it. */
32888bf
+	  if (previous_files_allocated != 0)
32888bf
+	    {
32888bf
+	      int i;
32888bf
+	      for (i = 0; previous_files[i] != NULL; i++)
32888bf
+		{
32888bf
+		  if (!strcmp (previous_files[i], s))
32888bf
+		    {
32888bf
+		      strcpy (s, "/dev/null");
32888bf
+		      break;
32888bf
+		    }
32888bf
+		}
32888bf
+
32888bf
+	      if (previous_files[i] == NULL)
32888bf
+		{
32888bf
+		  if (i == previous_files_allocated - 1)
32888bf
+		    {
32888bf
+		      char **old_previous_files = previous_files;
32888bf
+		      previous_files = realloc (previous_files,
32888bf
+						2 * previous_files_allocated *
32888bf
+						sizeof (const char *));
32888bf
+		      if (previous_files)
32888bf
+			previous_files_allocated *= 2;
32888bf
+		      else
32888bf
+			{
32888bf
+			  for (i = 0; old_previous_files[i] != NULL; i++)
32888bf
+			    free (old_previous_files[i]);
32888bf
+			  free (old_previous_files);
32888bf
+			  previous_files_allocated = 0;
32888bf
+			}
32888bf
+		    }
32888bf
+
32888bf
+		  if (i < previous_files_allocated - 1)
32888bf
+		    {
32888bf
+		      previous_files[i] = strdup (s);
32888bf
+		      previous_files[i + 1] = NULL;
32888bf
+		    }
32888bf
+		}
32888bf
+	    }
32888bf
+	  else
32888bf
+	    {
32888bf
+	      previous_files_allocated = 2;
32888bf
+	      previous_files = malloc (previous_files_allocated *
32888bf
+				       sizeof (const char *));
32888bf
+	      previous_files[0] = strdup (s);
32888bf
+	      previous_files[1] = NULL;
32888bf
+	    }
32888bf
+	}
32888bf
+
32888bf
+      if (strcmp (s, "/dev/null") != 0)
32888bf
+	addext (s, suffix, '~');
32888bf
     }
32888bf
   return s;
32888bf
 }