aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/patch.c35
1 files changed, 29 insertions, 6 deletions
diff --git a/editors/patch.c b/editors/patch.c
index 4a9715144..580ee147c 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -78,12 +78,23 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
78 enum { 78 enum {
79 OPT_R = (1 << 2), 79 OPT_R = (1 << 2),
80 OPT_N = (1 << 3), 80 OPT_N = (1 << 3),
81 OPT_dry_run = (1 << 4) * ENABLE_LONG_OPTS,
81 }; 82 };
82 83
83 xfunc_error_retval = 2; 84 xfunc_error_retval = 2;
84 { 85 {
85 const char *p = "-1"; 86 const char *p = "-1";
86 const char *i = "-"; /* compat */ 87 const char *i = "-"; /* compat */
88#if ENABLE_LONG_OPTS
89 static const char patch_longopts[] ALIGN1 =
90 "strip\0" Required_argument "p"
91 "input\0" Required_argument "i"
92 "reverse\0" No_argument "R"
93 "forward\0" No_argument "N"
94 "dry-run\0" No_argument "\xff"
95 ;
96 applet_long_options = patch_longopts;
97#endif
87 opt = getopt32(argv, "p:i:RN", &p, &i); 98 opt = getopt32(argv, "p:i:RN", &p, &i);
88 if (opt & OPT_R) 99 if (opt & OPT_R)
89 plus = '-'; 100 plus = '-';
@@ -97,7 +108,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
97 FILE *dst_stream; 108 FILE *dst_stream;
98 //char *old_filename; 109 //char *old_filename;
99 char *new_filename; 110 char *new_filename;
100 char *backup_filename; 111 char *backup_filename = NULL;
101 unsigned src_cur_line = 1; 112 unsigned src_cur_line = 1;
102 unsigned dst_cur_line = 0; 113 unsigned dst_cur_line = 0;
103 unsigned dst_beg_line; 114 unsigned dst_beg_line;
@@ -131,16 +142,21 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
131 bb_make_directory(new_filename, -1, FILEUTILS_RECUR); 142 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
132 *slash = '/'; 143 *slash = '/';
133 } 144 }
134 backup_filename = NULL;
135 src_stream = NULL; 145 src_stream = NULL;
136 saved_stat.st_mode = 0644; 146 saved_stat.st_mode = 0644;
137 } else { 147 } else if (!(opt & OPT_dry_run)) {
138 backup_filename = xasprintf("%s.orig", new_filename); 148 backup_filename = xasprintf("%s.orig", new_filename);
139 xrename(new_filename, backup_filename); 149 xrename(new_filename, backup_filename);
140 src_stream = xfopen_for_read(backup_filename); 150 src_stream = xfopen_for_read(backup_filename);
151 } else
152 src_stream = xfopen_for_read(new_filename);
153
154 if (opt & OPT_dry_run) {
155 dst_stream = xfopen_for_write("/dev/null");
156 } else {
157 dst_stream = xfopen_for_write(new_filename);
158 fchmod(fileno(dst_stream), saved_stat.st_mode);
141 } 159 }
142 dst_stream = xfopen_for_write(new_filename);
143 fchmod(fileno(dst_stream), saved_stat.st_mode);
144 160
145 printf("patching file %s\n", new_filename); 161 printf("patching file %s\n", new_filename);
146 162
@@ -189,6 +205,11 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
189 patch_line = xmalloc_fgets(patch_file); 205 patch_line = xmalloc_fgets(patch_file);
190 if (patch_line == NULL) 206 if (patch_line == NULL)
191 break; /* EOF */ 207 break; /* EOF */
208 if (!*patch_line) {
209 /* whitespace-damaged patch with "" lines */
210 free(patch_line);
211 patch_line = xstrdup(" ");
212 }
192 if ((*patch_line != '-') && (*patch_line != '+') 213 if ((*patch_line != '-') && (*patch_line != '+')
193 && (*patch_line != ' ') 214 && (*patch_line != ' ')
194 ) { 215 ) {
@@ -244,7 +265,9 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
244 if (backup_filename) { 265 if (backup_filename) {
245 unlink(backup_filename); 266 unlink(backup_filename);
246 } 267 }
247 if ((dst_cur_line == 0) || (dst_beg_line == 0)) { 268 if (!(opt & OPT_dry_run)
269 && ((dst_cur_line == 0) || (dst_beg_line == 0))
270 ) {
248 /* The new patched file is empty, remove it */ 271 /* The new patched file is empty, remove it */
249 xunlink(new_filename); 272 xunlink(new_filename);
250 // /* old_filename and new_filename may be the same file */ 273 // /* old_filename and new_filename may be the same file */