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