diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-26 13:32:30 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-26 13:32:30 +0000 |
commit | 08ec67bc62242503f77d9503fdbf820c9c12d856 (patch) | |
tree | 101a86c0fe86a07f786670ceca36bfcbdc2794ed /editors/patch.c | |
parent | cc2965fd236a85e5cac4fffa1c34057997780385 (diff) | |
download | busybox-w32-08ec67bc62242503f77d9503fdbf820c9c12d856.tar.gz busybox-w32-08ec67bc62242503f77d9503fdbf820c9c12d856.tar.bz2 busybox-w32-08ec67bc62242503f77d9503fdbf820c9c12d856.zip |
patch: add support for -R. ~ +110 byte. By Pascal Bellard <pascal.bellard AT ads-lu.com>
fbsplash: new applet by Michele Sanges <michele.sanges AT otomelara.it
function old new delta
fbsplash_main - 1525 +1525
fb_drawfullrectangle - 118 +118
static.param_value - 100 +100
packed_usage 23776 23872 +96
applet_names 1843 1852 +9
applet_main 1120 1124 +4
read_line_input 3156 3158 +2
applet_nameofs 560 562 +2
applet_install_loc 140 141 +1
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 6/0 up/down: 1857/0) Total: 1857 bytes
text data bss dec hex filename
799233 641 7380 807254 c5156 busybox_old
801202 641 7380 809223 c5907 busybox_unstripped
Diffstat (limited to 'editors/patch.c')
-rw-r--r-- | editors/patch.c | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/editors/patch.c b/editors/patch.c index 2a2b130b9..6f42b835c 100644 --- a/editors/patch.c +++ b/editors/patch.c | |||
@@ -21,7 +21,7 @@ | |||
21 | 21 | ||
22 | #include "libbb.h" | 22 | #include "libbb.h" |
23 | 23 | ||
24 | static unsigned copy_lines(FILE *src_stream, FILE *dest_stream, unsigned lines_count) | 24 | static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count) |
25 | { | 25 | { |
26 | while (src_stream && lines_count) { | 26 | while (src_stream && lines_count) { |
27 | char *line; | 27 | char *line; |
@@ -29,7 +29,7 @@ static unsigned copy_lines(FILE *src_stream, FILE *dest_stream, unsigned lines_c | |||
29 | if (line == NULL) { | 29 | if (line == NULL) { |
30 | break; | 30 | break; |
31 | } | 31 | } |
32 | if (fputs(line, dest_stream) == EOF) { | 32 | if (fputs(line, dst_stream) == EOF) { |
33 | bb_perror_msg_and_die("error writing to new file"); | 33 | bb_perror_msg_and_die("error writing to new file"); |
34 | } | 34 | } |
35 | free(line); | 35 | free(line); |
@@ -73,12 +73,14 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
73 | FILE *patch_file; | 73 | FILE *patch_file; |
74 | int patch_level; | 74 | int patch_level; |
75 | int ret = 0; | 75 | int ret = 0; |
76 | char plus = '+'; | ||
76 | 77 | ||
77 | xfunc_error_retval = 2; | 78 | xfunc_error_retval = 2; |
78 | { | 79 | { |
79 | const char *p = "-1"; | 80 | const char *p = "-1"; |
80 | const char *i = "-"; /* compat */ | 81 | const char *i = "-"; /* compat */ |
81 | getopt32(argv, "p:i:", &p, &i); | 82 | if (getopt32(argv, "p:i:R", &p, &i) & 4) |
83 | plus = '-'; | ||
82 | patch_level = xatoi(p); /* can be negative! */ | 84 | patch_level = xatoi(p); /* can be negative! */ |
83 | patch_file = xfopen_stdin(i); | 85 | patch_file = xfopen_stdin(i); |
84 | } | 86 | } |
@@ -91,8 +93,8 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
91 | char *new_filename; | 93 | char *new_filename; |
92 | char *backup_filename; | 94 | char *backup_filename; |
93 | unsigned src_cur_line = 1; | 95 | unsigned src_cur_line = 1; |
94 | unsigned dest_cur_line = 0; | 96 | unsigned dst_cur_line = 0; |
95 | unsigned dest_beg_line; | 97 | unsigned dst_beg_line; |
96 | unsigned bad_hunk_count = 0; | 98 | unsigned bad_hunk_count = 0; |
97 | unsigned hunk_count = 0; | 99 | unsigned hunk_count = 0; |
98 | smallint copy_trailing_lines_flag = 0; | 100 | smallint copy_trailing_lines_flag = 0; |
@@ -143,15 +145,26 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
143 | unsigned src_beg_line; | 145 | unsigned src_beg_line; |
144 | unsigned hunk_offset_start; | 146 | unsigned hunk_offset_start; |
145 | unsigned src_last_line = 1; | 147 | unsigned src_last_line = 1; |
148 | unsigned dst_last_line = 1; | ||
146 | 149 | ||
147 | if ((sscanf(patch_line, "@@ -%d,%d +%d", &src_beg_line, &src_last_line, &dest_beg_line) != 3) | 150 | if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3) |
148 | && (sscanf(patch_line, "@@ -%d +%d", &src_beg_line, &dest_beg_line) != 2) | 151 | && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2) |
149 | ) { /* No more hunks for this file */ | 152 | ) { |
153 | /* No more hunks for this file */ | ||
150 | break; | 154 | break; |
151 | } | 155 | } |
156 | if (plus != '+') { | ||
157 | /* reverse patch */ | ||
158 | unsigned tmp = src_last_line; | ||
159 | src_last_line = dst_last_line; | ||
160 | dst_last_line = tmp; | ||
161 | tmp = src_beg_line; | ||
162 | src_beg_line = dst_beg_line; | ||
163 | dst_beg_line = tmp; | ||
164 | } | ||
152 | hunk_count++; | 165 | hunk_count++; |
153 | 166 | ||
154 | if (src_beg_line && dest_beg_line) { | 167 | if (src_beg_line && dst_beg_line) { |
155 | /* Copy unmodified lines upto start of hunk */ | 168 | /* Copy unmodified lines upto start of hunk */ |
156 | /* src_beg_line will be 0 if it's a new file */ | 169 | /* src_beg_line will be 0 if it's a new file */ |
157 | count = src_beg_line - src_cur_line; | 170 | count = src_beg_line - src_cur_line; |
@@ -159,14 +172,15 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
159 | bb_error_msg_and_die("bad src file"); | 172 | bb_error_msg_and_die("bad src file"); |
160 | } | 173 | } |
161 | src_cur_line += count; | 174 | src_cur_line += count; |
162 | dest_cur_line += count; | 175 | dst_cur_line += count; |
163 | copy_trailing_lines_flag = 1; | 176 | copy_trailing_lines_flag = 1; |
164 | } | 177 | } |
165 | src_last_line += hunk_offset_start = src_cur_line; | 178 | src_last_line += hunk_offset_start = src_cur_line; |
179 | dst_last_line += dst_cur_line; | ||
166 | 180 | ||
167 | while (1) { | 181 | while (1) { |
168 | free(patch_line); | 182 | free(patch_line); |
169 | patch_line = xmalloc_fgets(patch_file); | 183 | patch_line = xmalloc_fgets(patch_file); |
170 | if (patch_line == NULL) | 184 | if (patch_line == NULL) |
171 | break; /* EOF */ | 185 | break; /* EOF */ |
172 | if ((*patch_line != '-') && (*patch_line != '+') | 186 | if ((*patch_line != '-') && (*patch_line != '+') |
@@ -174,7 +188,7 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
174 | ) { | 188 | ) { |
175 | break; /* End of hunk */ | 189 | break; /* End of hunk */ |
176 | } | 190 | } |
177 | if (*patch_line != '+') { /* '-', ' ' or '\n' */ | 191 | if (*patch_line != plus) { /* '-' or ' ' */ |
178 | char *src_line = NULL; | 192 | char *src_line = NULL; |
179 | if (src_cur_line == src_last_line) | 193 | if (src_cur_line == src_last_line) |
180 | break; | 194 | break; |
@@ -184,7 +198,8 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
184 | int diff = strcmp(src_line, patch_line + 1); | 198 | int diff = strcmp(src_line, patch_line + 1); |
185 | src_cur_line++; | 199 | src_cur_line++; |
186 | free(src_line); | 200 | free(src_line); |
187 | if (diff) src_line = NULL; | 201 | if (diff) |
202 | src_line = NULL; | ||
188 | } | 203 | } |
189 | } | 204 | } |
190 | if (!src_line) { | 205 | if (!src_line) { |
@@ -192,12 +207,14 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
192 | bad_hunk_count++; | 207 | bad_hunk_count++; |
193 | break; | 208 | break; |
194 | } | 209 | } |
195 | if (*patch_line == '-') { | 210 | if (*patch_line != ' ') { /* '-' */ |
196 | continue; | 211 | continue; |
197 | } | 212 | } |
198 | } | 213 | } |
214 | if (dst_cur_line == dst_last_line) | ||
215 | break; | ||
199 | fputs(patch_line + 1, dst_stream); | 216 | fputs(patch_line + 1, dst_stream); |
200 | dest_cur_line++; | 217 | dst_cur_line++; |
201 | } /* end of while loop handling one hunk */ | 218 | } /* end of while loop handling one hunk */ |
202 | } /* end of while loop handling one file */ | 219 | } /* end of while loop handling one file */ |
203 | 220 | ||
@@ -217,7 +234,7 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
217 | if (backup_filename) { | 234 | if (backup_filename) { |
218 | unlink(backup_filename); | 235 | unlink(backup_filename); |
219 | } | 236 | } |
220 | if ((dest_cur_line == 0) || (dest_beg_line == 0)) { | 237 | if ((dst_cur_line == 0) || (dst_beg_line == 0)) { |
221 | /* The new patched file is empty, remove it */ | 238 | /* The new patched file is empty, remove it */ |
222 | xunlink(new_filename); | 239 | xunlink(new_filename); |
223 | // /* old_filename and new_filename may be the same file */ | 240 | // /* old_filename and new_filename may be the same file */ |
@@ -228,8 +245,7 @@ int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
228 | //free(old_filename); | 245 | //free(old_filename); |
229 | free(new_filename); | 246 | free(new_filename); |
230 | } /* end of "while there are patch lines" */ | 247 | } /* end of "while there are patch lines" */ |
231 | quit: | 248 | quit: |
232 | |||
233 | /* 0 = SUCCESS | 249 | /* 0 = SUCCESS |
234 | * 1 = Some hunks failed | 250 | * 1 = Some hunks failed |
235 | * 2 = More serious problems (exited earlier) | 251 | * 2 = More serious problems (exited earlier) |