2a2c77d
diff -Burp sed-4.1.5/doc/sed.1 sed-4.1.5-f+c/doc/sed.1
2a2c77d
--- sed-4.1.5/doc/sed.1	2006-02-03 10:27:35.000000000 +0100
2a2c77d
+++ sed-4.1.5-f+c/doc/sed.1	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -1,5 +1,5 @@
2a2c77d
 .\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.28.
2a2c77d
-.TH SED "1" "February 2006" "sed version 4.1.4" "User Commands"
2a2c77d
+.TH SED "1" "June 2006" "sed version 4.1.5" "User Commands"
2a2c77d
 .SH NAME
2a2c77d
 sed \- stream editor for filtering and transforming text
2a2c77d
 .SH SYNOPSIS
2a2c77d
@@ -36,6 +36,11 @@ add the contents of script-file to the c
2a2c77d
 .IP
2a2c77d
 edit files in place (makes backup if extension supplied)
2a2c77d
 .HP
2a2c77d
+\fB\-c\fR, \fB\-\-copy\fR
2a2c77d
+.IP
2a2c77d
+use copy instead of rename when shuffling files in \fB\-i\fR mode
2a2c77d
+(avoids change of input file ownership)
2a2c77d
+.HP
2a2c77d
 \fB\-l\fR N, \fB\-\-line\-length\fR=\fIN\fR
2a2c77d
 .IP
2a2c77d
 specify the desired line-wrap length for the `l' command
2a2c77d
diff -Burp sed-4.1.5/lib/utils.c sed-4.1.5-f+c/lib/utils.c
2a2c77d
--- sed-4.1.5/lib/utils.c	2006-12-08 16:41:41.000000000 +0100
2a2c77d
+++ sed-4.1.5-f+c/lib/utils.c	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -405,6 +406,55 @@ _unlink_if_fail (rd, unlink_if_fail)
2a2c77d
   return rd != -1;
2a2c77d
 }
2a2c77d
 
2a2c77d
+/* Copy contents between files. */
2a2c77d
+static int
2a2c77d
+_copy (from, to)
2a2c77d
+  const char *from, *to;
2a2c77d
+{
2a2c77d
+  static size_t bufsize = 1024;
2a2c77d
+  FILE *infile, *outfile;
2a2c77d
+  int retval = 0;
2a2c77d
+  char * buf;
2a2c77d
+
2a2c77d
+  errno = 0;
2a2c77d
+
2a2c77d
+  infile = fopen (from, "r");
2a2c77d
+  if (infile == NULL)
2a2c77d
+    return -1;
2a2c77d
+
2a2c77d
+  outfile = fopen (to, "w");
2a2c77d
+  if (outfile == NULL)
2a2c77d
+    {
2a2c77d
+      fclose (infile);
2a2c77d
+      return -1;
2a2c77d
+    }
2a2c77d
+
2a2c77d
+  buf = alloca (bufsize);
2a2c77d
+  while (1)
2a2c77d
+    {
2a2c77d
+      size_t bytes_in = fread (buf, 1, bufsize, infile);
2a2c77d
+      size_t bytes_out;
2a2c77d
+      if (bytes_in == 0)
2a2c77d
+	{
2a2c77d
+	  if (ferror (infile))
2a2c77d
+	    retval = -1;
2a2c77d
+	  break;
2a2c77d
+	}
2a2c77d
+
2a2c77d
+      bytes_out = fwrite (buf, 1, bytes_in, outfile);
2a2c77d
+      if (bytes_out != bytes_in)
2a2c77d
+	{
2a2c77d
+	  retval = -1;
2a2c77d
+	  break;
2a2c77d
+	}
2a2c77d
+    }
2a2c77d
+
2a2c77d
+  fclose (outfile);
2a2c77d
+  fclose (infile);
2a2c77d
+
2a2c77d
+  return retval;
2a2c77d
+}
2a2c77d
+
2a2c77d
 /* Panic on failing rename */
2a2c77d
 void
2a2c77d
 ck_rename (from, to, unlink_if_fail)
2a2c77d
@@ -415,6 +465,26 @@ ck_rename (from, to, unlink_if_fail)
2a2c77d
   panic (_("cannot rename %s: %s"), from, strerror (errno));
2a2c77d
 }
2a2c77d
 
2a2c77d
+/* Attempt to copy file contents between the files. */
2a2c77d
+void
2a2c77d
+ck_fcmove (from, to, unlink_if_fail)
2a2c77d
+  const char *from, *to;
2a2c77d
+  const char *unlink_if_fail;
2a2c77d
+{
2a2c77d
+  if (!_unlink_if_fail (_copy (from, to), unlink_if_fail))
2a2c77d
+    panic (_("cannot copy %s to %s: %s"), from, to, strerror (errno));
2a2c77d
+}
2a2c77d
+
2a2c77d
+/* Copy contents between files, and then unlink the source. */
2a2c77d
+void
2a2c77d
+ck_fcopy (from, to, unlink_if_fail)
2a2c77d
+  const char *from, *to;
2a2c77d
+  const char *unlink_if_fail;
2a2c77d
+{
2a2c77d
+  ck_fcmove (from, to, unlink_if_fail);
2a2c77d
+  ck_unlink (from);
2a2c77d
+}
2a2c77d
+
2a2c77d
 
2a2c77d
 
2a2c77d
 /* Panic on failing malloc */
2a2c77d
diff -Burp sed-4.1.5/lib/utils.h sed-4.1.5-f+c/lib/utils.h
2a2c77d
--- sed-4.1.5/lib/utils.h	2006-12-08 16:41:41.000000000 +0100
2a2c77d
+++ sed-4.1.5-f+c/lib/utils.h	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -31,6 +31,8 @@ size_t ck_getline P_((char **text, size_
2a2c77d
 FILE * ck_mkstemp P_((char **p_filename, char *tmpdir, char *base));
2a2c77d
 const char* ck_follow_symlink P_((const char * fname));
2a2c77d
 void ck_rename P_((const char *from, const char *to, const char *unlink_if_fail));
2a2c77d
+void ck_fcopy P_((const char *from, const char *to, const char *unlink_if_fail));
2a2c77d
+void ck_fcmove P_((const char *from, const char *to, const char *unlink_if_fail));
2a2c77d
 
2a2c77d
 VOID *ck_malloc P_((size_t size));
2a2c77d
 VOID *xmalloc P_((size_t size));
2a2c77d
diff -Burp sed-4.1.5/sed/execute.c sed-4.1.5-f+c/sed/execute.c
2a2c77d
--- sed-4.1.5/sed/execute.c	2006-12-08 16:41:41.000000000 +0100
2a2c77d
+++ sed-4.1.5-f+c/sed/execute.c	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -716,12 +716,14 @@ closedown(input)
2a2c77d
       ck_fclose (output_file.fp);
2a2c77d
       if (strcmp(in_place_extension, "*") != 0)
2a2c77d
         {
2a2c77d
-          char *backup_file_name = get_backup_file_name(target_name);
2a2c77d
-	  ck_rename (target_name, backup_file_name, input->out_file_name);
2a2c77d
+          char *backup_file_name = get_backup_file_name(target_name);
2a2c77d
+	  (copy_instead_of_rename?ck_fcmove:ck_rename)
2a2c77d
+	    (target_name, backup_file_name, input->out_file_name);
2a2c77d
           free (backup_file_name);
2a2c77d
 	}
2a2c77d
 
2a2c77d
-      ck_rename (input->out_file_name, target_name, input->out_file_name);
2a2c77d
+      (copy_instead_of_rename?ck_fcopy:ck_rename)
2a2c77d
+	(input->out_file_name, target_name, input->out_file_name);
2a2c77d
       free (input->out_file_name);
2a2c77d
       free (target_name);
2a2c77d
     }
2a2c77d
diff -Burp sed-4.1.5/sed/sed.c sed-4.1.5-f+c/sed/sed.c
2a2c77d
--- sed-4.1.5/sed/sed.c	2005-06-21 16:09:47.000000000 +0200
2a2c77d
+++ sed-4.1.5-f+c/sed/sed.c	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -73,6 +73,10 @@ bool separate_files = false;
2a2c77d
 /* How do we edit files in-place? (we don't if NULL) */
2a2c77d
 char *in_place_extension = NULL;
2a2c77d
 
2a2c77d
+/* Do we use copy or rename when in in-place edit mode? (boolean
2a2c77d
+   value, non-zero for copy, zero for rename).*/
2a2c77d
+int copy_instead_of_rename = 0;
2a2c77d
+
2a2c77d
 /* Do we need to be pedantically POSIX compliant? */
2a2c77d
 enum posixicity_types posixicity;
2a2c77d
 
2a2c77d
@@ -107,6 +111,9 @@ Usage: %s [OPTION]... {script-only-if-no
2a2c77d
                  add the contents of script-file to the commands to be executed\n"));
2a2c77d
   fprintf(out, _("  -i[SUFFIX], --in-place[=SUFFIX]\n\
2a2c77d
                  edit files in place (makes backup if extension supplied)\n"));
2a2c77d
+  fprintf(out, _("  -c, --copy\n\
2a2c77d
+                 use copy instead of rename when shuffling files in -i mode\n\
2a2c77d
+		 (avoids change of input file ownership)\n"));
2a2c77d
   fprintf(out, _("  -l N, --line-length=N\n\
2a2c77d
                  specify the desired line-wrap length for the `l' command\n"));
2a2c77d
   fprintf(out, _("  --posix\n\
2a2c77d
@@ -142,9 +149,9 @@ main(argc, argv)
2a2c77d
   char **argv;
2a2c77d
 {
2a2c77d
 #ifdef REG_PERL
2a2c77d
-#define SHORTOPTS "snrRue:f:l:i::V:"
2a2c77d
+#define SHORTOPTS "csnrRue:f:l:i::V:"
2a2c77d
 #else
2a2c77d
-#define SHORTOPTS "snrue:f:l:i::V:"
2a2c77d
+#define SHORTOPTS "csnrue:f:l:i::V:"
2a2c77d
 #endif
2a2c77d
 
2a2c77d
   static struct option longopts[] = {
2a2c77d
@@ -155,6 +162,7 @@ main(argc, argv)
2a2c77d
     {"expression", 1, NULL, 'e'},
2a2c77d
     {"file", 1, NULL, 'f'},
2a2c77d
     {"in-place", 2, NULL, 'i'},
2a2c77d
+    {"copy", 0, NULL, 'c'},
2a2c77d
     {"line-length", 1, NULL, 'l'},
2a2c77d
     {"quiet", 0, NULL, 'n'},
2a2c77d
     {"posix", 0, NULL, 'p'},
2a2c77d
@@ -215,6 +223,10 @@ main(argc, argv)
2a2c77d
 	  the_program = compile_file(the_program, optarg);
2a2c77d
 	  break;
2a2c77d
 
2a2c77d
+	case 'c':
2a2c77d
+	  copy_instead_of_rename = true;
2a2c77d
+	  break;
2a2c77d
+
2a2c77d
 	case 'i':
2a2c77d
 	  separate_files = true;
2a2c77d
 	  if (optarg == NULL)
2a2c77d
@@ -284,6 +296,12 @@ to the extent permitted by law.\n\
2a2c77d
 	}
2a2c77d
     }
2a2c77d
 
2a2c77d
+  if (copy_instead_of_rename && in_place_extension == NULL)
2a2c77d
+    {
2a2c77d
+      fprintf (stderr, _("Error: -c used without -i.\n"));
2a2c77d
+      usage(4);
2a2c77d
+    }
2a2c77d
+
2a2c77d
   if (!the_program)
2a2c77d
     {
2a2c77d
       if (optind < argc)
2a2c77d
diff -Burp sed-4.1.5/sed/sed.h sed-4.1.5-f+c/sed/sed.h
2a2c77d
--- sed-4.1.5/sed/sed.h	2006-12-08 16:41:41.000000000 +0100
2a2c77d
+++ sed-4.1.5-f+c/sed/sed.h	2006-12-08 16:42:59.000000000 +0100
2a2c77d
@@ -228,6 +228,10 @@ extern countT lcmd_out_line_len;
2a2c77d
 /* How do we edit files in-place? (we don't if NULL) */
2a2c77d
 extern char *in_place_extension;
2a2c77d
 
2a2c77d
+/* Do we use copy or rename when in in-place edit mode? (boolean
2a2c77d
+   value, non-zero for copy, zero for rename).*/
2a2c77d
+extern int copy_instead_of_rename;
2a2c77d
+
2a2c77d
 /* Should we use EREs? */
2a2c77d
 extern bool use_extended_syntax_p;
2a2c77d