| 1 |
diff -Burp sed-4.1.5/doc/sed.1 sed-4.1.5-f+c/doc/sed.1
|
| 2 |
--- sed-4.1.5/doc/sed.1 2006-02-03 10:27:35.000000000 +0100
|
| 3 |
+++ sed-4.1.5-f+c/doc/sed.1 2006-12-08 16:42:59.000000000 +0100
|
| 4 |
@@ -1,5 +1,5 @@
|
| 5 |
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.28.
|
| 6 |
-.TH SED "1" "February 2006" "sed version 4.1.4" "User Commands"
|
| 7 |
+.TH SED "1" "June 2006" "sed version 4.1.5" "User Commands"
|
| 8 |
.SH NAME
|
| 9 |
sed \- stream editor for filtering and transforming text
|
| 10 |
.SH SYNOPSIS
|
| 11 |
@@ -36,6 +36,11 @@ add the contents of script-file to the c
|
| 12 |
.IP
|
| 13 |
edit files in place (makes backup if extension supplied)
|
| 14 |
.HP
|
| 15 |
+\fB\-c\fR, \fB\-\-copy\fR
|
| 16 |
+.IP
|
| 17 |
+use copy instead of rename when shuffling files in \fB\-i\fR mode
|
| 18 |
+(avoids change of input file ownership)
|
| 19 |
+.HP
|
| 20 |
\fB\-l\fR N, \fB\-\-line\-length\fR=\fIN\fR
|
| 21 |
.IP
|
| 22 |
specify the desired line-wrap length for the `l' command
|
| 23 |
diff -Burp sed-4.1.5/lib/utils.c sed-4.1.5-f+c/lib/utils.c
|
| 24 |
--- sed-4.1.5/lib/utils.c 2006-12-08 16:41:41.000000000 +0100
|
| 25 |
+++ sed-4.1.5-f+c/lib/utils.c 2006-12-08 16:42:59.000000000 +0100
|
| 26 |
@@ -405,6 +406,55 @@ _unlink_if_fail (rd, unlink_if_fail)
|
| 27 |
return rd != -1;
|
| 28 |
}
|
| 29 |
|
| 30 |
+/* Copy contents between files. */
|
| 31 |
+static int
|
| 32 |
+_copy (from, to)
|
| 33 |
+ const char *from, *to;
|
| 34 |
+{
|
| 35 |
+ static size_t bufsize = 1024;
|
| 36 |
+ FILE *infile, *outfile;
|
| 37 |
+ int retval = 0;
|
| 38 |
+ char * buf;
|
| 39 |
+
|
| 40 |
+ errno = 0;
|
| 41 |
+
|
| 42 |
+ infile = fopen (from, "r");
|
| 43 |
+ if (infile == NULL)
|
| 44 |
+ return -1;
|
| 45 |
+
|
| 46 |
+ outfile = fopen (to, "w");
|
| 47 |
+ if (outfile == NULL)
|
| 48 |
+ {
|
| 49 |
+ fclose (infile);
|
| 50 |
+ return -1;
|
| 51 |
+ }
|
| 52 |
+
|
| 53 |
+ buf = alloca (bufsize);
|
| 54 |
+ while (1)
|
| 55 |
+ {
|
| 56 |
+ size_t bytes_in = fread (buf, 1, bufsize, infile);
|
| 57 |
+ size_t bytes_out;
|
| 58 |
+ if (bytes_in == 0)
|
| 59 |
+ {
|
| 60 |
+ if (ferror (infile))
|
| 61 |
+ retval = -1;
|
| 62 |
+ break;
|
| 63 |
+ }
|
| 64 |
+
|
| 65 |
+ bytes_out = fwrite (buf, 1, bytes_in, outfile);
|
| 66 |
+ if (bytes_out != bytes_in)
|
| 67 |
+ {
|
| 68 |
+ retval = -1;
|
| 69 |
+ break;
|
| 70 |
+ }
|
| 71 |
+ }
|
| 72 |
+
|
| 73 |
+ fclose (outfile);
|
| 74 |
+ fclose (infile);
|
| 75 |
+
|
| 76 |
+ return retval;
|
| 77 |
+}
|
| 78 |
+
|
| 79 |
/* Panic on failing rename */
|
| 80 |
void
|
| 81 |
ck_rename (from, to, unlink_if_fail)
|
| 82 |
@@ -415,6 +465,26 @@ ck_rename (from, to, unlink_if_fail)
|
| 83 |
panic (_("cannot rename %s: %s"), from, strerror (errno));
|
| 84 |
}
|
| 85 |
|
| 86 |
+/* Attempt to copy file contents between the files. */
|
| 87 |
+void
|
| 88 |
+ck_fcmove (from, to, unlink_if_fail)
|
| 89 |
+ const char *from, *to;
|
| 90 |
+ const char *unlink_if_fail;
|
| 91 |
+{
|
| 92 |
+ if (!_unlink_if_fail (_copy (from, to), unlink_if_fail))
|
| 93 |
+ panic (_("cannot copy %s to %s: %s"), from, to, strerror (errno));
|
| 94 |
+}
|
| 95 |
+
|
| 96 |
+/* Copy contents between files, and then unlink the source. */
|
| 97 |
+void
|
| 98 |
+ck_fcopy (from, to, unlink_if_fail)
|
| 99 |
+ const char *from, *to;
|
| 100 |
+ const char *unlink_if_fail;
|
| 101 |
+{
|
| 102 |
+ ck_fcmove (from, to, unlink_if_fail);
|
| 103 |
+ ck_unlink (from);
|
| 104 |
+}
|
| 105 |
+
|
| 106 |
|
| 107 |
|
| 108 |
/* Panic on failing malloc */
|
| 109 |
diff -Burp sed-4.1.5/lib/utils.h sed-4.1.5-f+c/lib/utils.h
|
| 110 |
--- sed-4.1.5/lib/utils.h 2006-12-08 16:41:41.000000000 +0100
|
| 111 |
+++ sed-4.1.5-f+c/lib/utils.h 2006-12-08 16:42:59.000000000 +0100
|
| 112 |
@@ -31,6 +31,8 @@ size_t ck_getline P_((char **text, size_
|
| 113 |
FILE * ck_mkstemp P_((char **p_filename, char *tmpdir, char *base));
|
| 114 |
const char* ck_follow_symlink P_((const char * fname));
|
| 115 |
void ck_rename P_((const char *from, const char *to, const char *unlink_if_fail));
|
| 116 |
+void ck_fcopy P_((const char *from, const char *to, const char *unlink_if_fail));
|
| 117 |
+void ck_fcmove P_((const char *from, const char *to, const char *unlink_if_fail));
|
| 118 |
|
| 119 |
VOID *ck_malloc P_((size_t size));
|
| 120 |
VOID *xmalloc P_((size_t size));
|
| 121 |
diff -Burp sed-4.1.5/sed/execute.c sed-4.1.5-f+c/sed/execute.c
|
| 122 |
--- sed-4.1.5/sed/execute.c 2006-12-08 16:41:41.000000000 +0100
|
| 123 |
+++ sed-4.1.5-f+c/sed/execute.c 2006-12-08 16:42:59.000000000 +0100
|
| 124 |
@@ -716,12 +716,14 @@ closedown(input)
|
| 125 |
ck_fclose (output_file.fp);
|
| 126 |
if (strcmp(in_place_extension, "*") != 0)
|
| 127 |
{
|
| 128 |
- char *backup_file_name = get_backup_file_name(target_name);
|
| 129 |
- ck_rename (target_name, backup_file_name, input->out_file_name);
|
| 130 |
+ char *backup_file_name = get_backup_file_name(target_name);
|
| 131 |
+ (copy_instead_of_rename?ck_fcmove:ck_rename)
|
| 132 |
+ (target_name, backup_file_name, input->out_file_name);
|
| 133 |
free (backup_file_name);
|
| 134 |
}
|
| 135 |
|
| 136 |
- ck_rename (input->out_file_name, target_name, input->out_file_name);
|
| 137 |
+ (copy_instead_of_rename?ck_fcopy:ck_rename)
|
| 138 |
+ (input->out_file_name, target_name, input->out_file_name);
|
| 139 |
free (input->out_file_name);
|
| 140 |
free (target_name);
|
| 141 |
}
|
| 142 |
diff -Burp sed-4.1.5/sed/sed.c sed-4.1.5-f+c/sed/sed.c
|
| 143 |
--- sed-4.1.5/sed/sed.c 2005-06-21 16:09:47.000000000 +0200
|
| 144 |
+++ sed-4.1.5-f+c/sed/sed.c 2006-12-08 16:42:59.000000000 +0100
|
| 145 |
@@ -73,6 +73,10 @@ bool separate_files = false;
|
| 146 |
/* How do we edit files in-place? (we don't if NULL) */
|
| 147 |
char *in_place_extension = NULL;
|
| 148 |
|
| 149 |
+/* Do we use copy or rename when in in-place edit mode? (boolean
|
| 150 |
+ value, non-zero for copy, zero for rename).*/
|
| 151 |
+int copy_instead_of_rename = 0;
|
| 152 |
+
|
| 153 |
/* Do we need to be pedantically POSIX compliant? */
|
| 154 |
enum posixicity_types posixicity;
|
| 155 |
|
| 156 |
@@ -107,6 +111,9 @@ Usage: %s [OPTION]... {script-only-if-no
|
| 157 |
add the contents of script-file to the commands to be executed\n"));
|
| 158 |
fprintf(out, _(" -i[SUFFIX], --in-place[=SUFFIX]\n\
|
| 159 |
edit files in place (makes backup if extension supplied)\n"));
|
| 160 |
+ fprintf(out, _(" -c, --copy\n\
|
| 161 |
+ use copy instead of rename when shuffling files in -i mode\n\
|
| 162 |
+ (avoids change of input file ownership)\n"));
|
| 163 |
fprintf(out, _(" -l N, --line-length=N\n\
|
| 164 |
specify the desired line-wrap length for the `l' command\n"));
|
| 165 |
fprintf(out, _(" --posix\n\
|
| 166 |
@@ -142,9 +149,9 @@ main(argc, argv)
|
| 167 |
char **argv;
|
| 168 |
{
|
| 169 |
#ifdef REG_PERL
|
| 170 |
-#define SHORTOPTS "snrRue:f:l:i::V:"
|
| 171 |
+#define SHORTOPTS "csnrRue:f:l:i::V:"
|
| 172 |
#else
|
| 173 |
-#define SHORTOPTS "snrue:f:l:i::V:"
|
| 174 |
+#define SHORTOPTS "csnrue:f:l:i::V:"
|
| 175 |
#endif
|
| 176 |
|
| 177 |
static struct option longopts[] = {
|
| 178 |
@@ -155,6 +162,7 @@ main(argc, argv)
|
| 179 |
{"expression", 1, NULL, 'e'},
|
| 180 |
{"file", 1, NULL, 'f'},
|
| 181 |
{"in-place", 2, NULL, 'i'},
|
| 182 |
+ {"copy", 0, NULL, 'c'},
|
| 183 |
{"line-length", 1, NULL, 'l'},
|
| 184 |
{"quiet", 0, NULL, 'n'},
|
| 185 |
{"posix", 0, NULL, 'p'},
|
| 186 |
@@ -215,6 +223,10 @@ main(argc, argv)
|
| 187 |
the_program = compile_file(the_program, optarg);
|
| 188 |
break;
|
| 189 |
|
| 190 |
+ case 'c':
|
| 191 |
+ copy_instead_of_rename = true;
|
| 192 |
+ break;
|
| 193 |
+
|
| 194 |
case 'i':
|
| 195 |
separate_files = true;
|
| 196 |
if (optarg == NULL)
|
| 197 |
@@ -284,6 +296,12 @@ to the extent permitted by law.\n\
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
+ if (copy_instead_of_rename && in_place_extension == NULL)
|
| 202 |
+ {
|
| 203 |
+ fprintf (stderr, _("Error: -c used without -i.\n"));
|
| 204 |
+ usage(4);
|
| 205 |
+ }
|
| 206 |
+
|
| 207 |
if (!the_program)
|
| 208 |
{
|
| 209 |
if (optind < argc)
|
| 210 |
diff -Burp sed-4.1.5/sed/sed.h sed-4.1.5-f+c/sed/sed.h
|
| 211 |
--- sed-4.1.5/sed/sed.h 2006-12-08 16:41:41.000000000 +0100
|
| 212 |
+++ sed-4.1.5-f+c/sed/sed.h 2006-12-08 16:42:59.000000000 +0100
|
| 213 |
@@ -228,6 +228,10 @@ extern countT lcmd_out_line_len;
|
| 214 |
/* How do we edit files in-place? (we don't if NULL) */
|
| 215 |
extern char *in_place_extension;
|
| 216 |
|
| 217 |
+/* Do we use copy or rename when in in-place edit mode? (boolean
|
| 218 |
+ value, non-zero for copy, zero for rename).*/
|
| 219 |
+extern int copy_instead_of_rename;
|
| 220 |
+
|
| 221 |
/* Should we use EREs? */
|
| 222 |
extern bool use_extended_syntax_p;
|
| 223 |
|