85742b
diff -urN sed-4.2.2/sed/execute.c sed-4.2.2.new00/sed/execute.c
85742b
--- sed-4.2.2/sed/execute.c	2012-03-16 10:13:31.000000000 +0100
85742b
+++ sed-4.2.2.new00/sed/execute.c	2014-02-10 14:40:25.603629422 +0100
85742b
@@ -703,11 +703,13 @@
85742b
       if (strcmp(in_place_extension, "*") != 0)
85742b
         {
85742b
           char *backup_file_name = get_backup_file_name(target_name);
85742b
-	  ck_rename (target_name, backup_file_name, input->out_file_name);
85742b
+	  (copy_instead_of_rename?ck_fccopy:ck_rename)
85742b
+            (target_name, backup_file_name, input->out_file_name);
85742b
           free (backup_file_name);
85742b
 	}
85742b
 
85742b
-      ck_rename (input->out_file_name, target_name, input->out_file_name);
85742b
+      (copy_instead_of_rename?ck_fcmove:ck_rename)
85742b
+        (input->out_file_name, target_name, input->out_file_name);
85742b
       free (input->out_file_name);
85742b
     }
85742b
   else
85742b
diff -urN sed-4.2.2/sed/sed.c sed-4.2.2.new00/sed/sed.c
85742b
--- sed-4.2.2/sed/sed.c	2012-03-16 10:13:31.000000000 +0100
85742b
+++ sed-4.2.2.new00/sed/sed.c	2014-02-10 17:37:19.381273509 +0100
85742b
@@ -56,6 +56,10 @@
85742b
 /* How do we edit files in-place? (we don't if NULL) */
85742b
 char *in_place_extension = NULL;
85742b
 
85742b
+/* Do we use copy or rename when in in-place edit mode? (boolean
85742b
+   value, non-zero for copy, zero for rename).*/
85742b
+int copy_instead_of_rename = 0;
85742b
+
85742b
 /* The mode to use to read/write files, either "r"/"w" or "rb"/"wb".  */
85742b
 char *read_mode = "r";
85742b
 char *write_mode = "w";
85742b
@@ -117,10 +121,17 @@
85742b
 #endif
85742b
   fprintf(out, _("  -i[SUFFIX], --in-place[=SUFFIX]\n\
85742b
                  edit files in place (makes backup if SUFFIX supplied)\n"));
85742b
-#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__)
85742b
-  fprintf(out, _("  -b, --binary\n\
85742b
-                 open files in binary mode (CR+LFs are not processed specially)\n"));
85742b
+  fprintf(out, _("  -c, --copy\n\
85742b
+                 use copy instead of rename when shuffling files in -i mode\n"));
85742b
+  fprintf(out, _("  -b, --binary\n"
85742b
+#if ! ( defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__) )
85742b
+"                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (\n"
85742b
+#endif
85742b
+"                 open files in binary mode (CR+LFs are not treated specially)"
85742b
+#if ! ( defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__) )
85742b
+                 ")"
85742b
 #endif
85742b
+                 "\n"));
85742b
   fprintf(out, _("  -l N, --line-length=N\n\
85742b
                  specify the desired line-wrap length for the `l' command\n"));
85742b
   fprintf(out, _("  --posix\n\
85742b
@@ -138,8 +149,10 @@
85742b
                  the output buffers more often\n"));
85742b
   fprintf(out, _("  -z, --null-data\n\
85742b
                  separate lines by NUL characters\n"));
85742b
-  fprintf(out, _("      --help     display this help and exit\n"));
85742b
-  fprintf(out, _("      --version  output version information and exit\n"));
85742b
+  fprintf(out, _("  --help\n\
85742b
+                 display this help and exit\n"));
85742b
+  fprintf(out, _("  --version\n\
85742b
+                 output version information and exit\n"));
85742b
   fprintf(out, _("\n\
85742b
 If no -e, --expression, -f, or --file option is given, then the first\n\
85742b
 non-option argument is taken as the sed script to interpret.  All\n\
85742b
@@ -158,9 +171,9 @@
85742b
   char **argv;
85742b
 {
85742b
 #ifdef REG_PERL
85742b
-#define SHORTOPTS "bsnrzRuEe:f:l:i::V:"
85742b
+#define SHORTOPTS "bcsnrzRuEe:f:l:i::"
85742b
 #else
85742b
-#define SHORTOPTS "bsnrzuEe:f:l:i::V:"
85742b
+#define SHORTOPTS "bcsnrzuEe:f:l:i::"
85742b
 #endif
85742b
 
85742b
   static struct option longopts[] = {
85742b
@@ -172,6 +185,7 @@
85742b
     {"expression", 1, NULL, 'e'},
85742b
     {"file", 1, NULL, 'f'},
85742b
     {"in-place", 2, NULL, 'i'},
85742b
+    {"copy", 0, NULL, 'c'},
85742b
     {"line-length", 1, NULL, 'l'},
85742b
     {"null-data", 0, NULL, 'z'},
85742b
     {"zero-terminated", 0, NULL, 'z'},
85742b
@@ -246,6 +260,10 @@
85742b
 	  follow_symlinks = true;
85742b
 	  break;
85742b
 
85742b
+	case 'c':
85742b
+	  copy_instead_of_rename = true;
85742b
+	  break;
85742b
+
85742b
 	case 'i':
85742b
 	  separate_files = true;
85742b
 	  if (optarg == NULL)
85742b
@@ -272,9 +290,11 @@
85742b
 	  posixicity = POSIXLY_BASIC;
85742b
 	  break;
85742b
 
85742b
-        case 'b':
85742b
+	case 'b':
85742b
+#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__)
85742b
 	  read_mode = "rb";
85742b
 	  write_mode = "wb";
85742b
+#endif
85742b
 	  break;
85742b
 
85742b
 	/* Undocumented, for compatibility with BSD sed.  */
85742b
@@ -314,6 +334,12 @@
85742b
 	}
85742b
     }
85742b
 
85742b
+  if (copy_instead_of_rename && in_place_extension == NULL)
85742b
+    {
85742b
+      fprintf (stderr, _("Error: -c used without -i.\n"));
85742b
+      usage(4);
85742b
+    }
85742b
+
85742b
   if (!the_program)
85742b
     {
85742b
       if (optind < argc)
85742b
diff -urN sed-4.2.2/sed/sed.h sed-4.2.2.new00/sed/sed.h
85742b
--- sed-4.2.2/sed/sed.h	2012-07-25 12:33:09.000000000 +0200
85742b
+++ sed-4.2.2.new00/sed/sed.h	2014-02-10 14:40:25.602629419 +0100
85742b
@@ -230,6 +230,10 @@
85742b
 /* How do we edit files in-place? (we don't if NULL) */
85742b
 extern char *in_place_extension;
85742b
 
85742b
+/* Do we use copy or rename when in in-place edit mode? (boolean
85742b
+   value, non-zero for copy, zero for rename).*/
85742b
+extern int copy_instead_of_rename;
85742b
+
85742b
 /* The mode to use to read and write files, either "rt"/"w" or "rb"/"wb".  */
85742b
 extern char *read_mode;
85742b
 extern char *write_mode;
85742b
diff -urN sed-4.2.2/sed/utils.c sed-4.2.2.new00/sed/utils.c
85742b
--- sed-4.2.2/sed/utils.c	2012-03-16 10:13:31.000000000 +0100
85742b
+++ sed-4.2.2.new00/sed/utils.c	2014-02-10 14:40:25.603629422 +0100
85742b
@@ -27,6 +27,7 @@
85742b
 #include <sys/stat.h>
85742b
 #include <unistd.h>
85742b
 #include <limits.h>
85742b
+#include <fcntl.h>
85742b
 
85742b
 #include "utils.h"
85742b
 #include "pathmax.h"
85742b
@@ -410,33 +411,109 @@
85742b
   return fname;
85742b
 #endif /* ENABLE_FOLLOW_SYMLINKS */
85742b
 }
85742b
+
85742b
 
85742b
-/* Panic on failing rename */
85742b
+/* Panic on failing unlink */
85742b
 void
85742b
-ck_rename (from, to, unlink_if_fail)
85742b
-  const char *from, *to;
85742b
-  const char *unlink_if_fail;
85742b
+ck_unlink (name)
85742b
+  const char *name;
85742b
 {
85742b
-  int rd = rename (from, to);
85742b
-  if (rd != -1)
85742b
-    return;
85742b
+  if (unlink (name) == -1)
85742b
+    panic (_("cannot remove %s: %s"), name, strerror (errno));
85742b
+}
85742b
 
85742b
-  if (unlink_if_fail)
85742b
+/* Attempt to unlink denoted file if operation rd failed. */
85742b
+static int
85742b
+_unlink_if_fail (rd, unlink_if_fail)
85742b
+  int rd;
85742b
+  const char *unlink_if_fail;
85742b
+{
85742b
+  if (rd == -1 && unlink_if_fail)
85742b
     {
85742b
       int save_errno = errno;
85742b
+      ck_unlink (unlink_if_fail);
85742b
+      errno = save_errno;
85742b
+    }
85742b
+
85742b
+  return rd != -1;
85742b
+}
85742b
+
85742b
+/* Copy contents between files. */
85742b
+static int
85742b
+_copy (from, to)
85742b
+  const char *from, *to;
85742b
+{
85742b
+  static char buf[4096];
85742b
+
85742b
+  FILE *infile, *outfile;
85742b
+  int c, retval = 0;
85742b
       errno = 0;
85742b
-      unlink (unlink_if_fail);
85742b
 
85742b
-      /* Failure to remove the temporary file is more severe, so trigger it first.  */
85742b
-      if (errno != 0)
85742b
-        panic (_("cannot remove %s: %s"), unlink_if_fail, strerror (errno));
85742b
+  infile = fopen (from, "r");
85742b
+  if (infile == NULL)
85742b
+    return -1;
85742b
 
85742b
-      errno = save_errno;
85742b
+  outfile = fopen (to, "w");
85742b
+  if (outfile == NULL)
85742b
+    {
85742b
+      fclose (infile);
85742b
+      return -1;
85742b
+    }
85742b
+
85742b
+  while (1)
85742b
+    {
85742b
+      size_t bytes_in = fread (buf, 1, sizeof (buf), infile);
85742b
+      size_t bytes_out;
85742b
+      if (bytes_in == 0)
85742b
+	{
85742b
+	  if (ferror (infile))
85742b
+	    retval = -1;
85742b
+	  break;
85742b
+	}
85742b
+
85742b
+      bytes_out = fwrite (buf, 1, bytes_in, outfile);
85742b
+      if (bytes_out != bytes_in)
85742b
+	{
85742b
+	  retval = -1;
85742b
+	  break;
85742b
+	}
85742b
     }
85742b
 
85742b
+  fclose (outfile);
85742b
+  fclose (infile);
85742b
+
85742b
+  return retval;
85742b
+}
85742b
+
85742b
+/* Panic on failing rename */
85742b
+void
85742b
+ck_rename (from, to, unlink_if_fail)
85742b
+  const char *from, *to;
85742b
+  const char *unlink_if_fail;
85742b
+{
85742b
+  if (!_unlink_if_fail (rename (from, to), unlink_if_fail))
85742b
   panic (_("cannot rename %s: %s"), from, strerror (errno));
85742b
 }
85742b
 
85742b
+/* Attempt to copy file contents between the files. */
85742b
+void
85742b
+ck_fccopy (from, to, unlink_if_fail)
85742b
+  const char *from, *to;
85742b
+  const char *unlink_if_fail;
85742b
+{
85742b
+  if (!_unlink_if_fail (_copy (from, to), unlink_if_fail))
85742b
+    panic (_("cannot copy %s to %s: %s"), from, to, strerror (errno));
85742b
+}
85742b
+
85742b
+/* Copy contents between files, and then unlink the source. */
85742b
+void
85742b
+ck_fcmove (from, to, unlink_if_fail)
85742b
+  const char *from, *to;
85742b
+  const char *unlink_if_fail;
85742b
+{
85742b
+  ck_fccopy (from, to, unlink_if_fail);
85742b
+  ck_unlink (from);
85742b
+}
85742b
 
85742b
 
85742b
 
85742b
diff -urN sed-4.2.2/sed/utils.h sed-4.2.2.new00/sed/utils.h
85742b
--- sed-4.2.2/sed/utils.h	2012-03-16 10:13:31.000000000 +0100
85742b
+++ sed-4.2.2.new00/sed/utils.h	2014-02-10 14:40:25.603629422 +0100
85742b
@@ -33,6 +33,8 @@
85742b
 FILE * ck_mkstemp (char **p_filename, const char *tmpdir, const char *base,
85742b
 		   const char *mode);
85742b
 void ck_rename (const char *from, const char *to, const char *unlink_if_fail);
85742b
+void ck_fccopy (const char *from, const char *to, const char *unlink_if_fail);
85742b
+void ck_fcmove (const char *from, const char *to, const char *unlink_if_fail);
85742b
 
85742b
 void *ck_malloc (size_t size);
85742b
 void *xmalloc (size_t size);