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