diff options
-rw-r--r-- | editors/patch.c | 825 | ||||
-rw-r--r-- | editors/patch_bbox.c | 306 | ||||
-rw-r--r-- | editors/patch_toybox.c | 164 | ||||
-rwxr-xr-x | testsuite/patch.tests | 13 | ||||
-rw-r--r-- | testsuite/testing.sh | 3 |
5 files changed, 969 insertions, 342 deletions
diff --git a/editors/patch.c b/editors/patch.c index 62477af16..7f3234e66 100644 --- a/editors/patch.c +++ b/editors/patch.c | |||
@@ -1,306 +1,591 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* Adapted from toybox's patch. */ |
2 | /* | 2 | |
3 | * busybox patch applet to handle the unified diff format. | 3 | /* vi: set sw=4 ts=4: |
4 | * Copyright (C) 2003 Glenn McGrath | 4 | * |
5 | * patch.c - Apply a "universal" diff. | ||
5 | * | 6 | * |
6 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 7 | * Copyright 2007 Rob Landley <rob@landley.net> |
7 | * | 8 | * |
8 | * This applet is written to work with patches generated by GNU diff, | 9 | * see http://www.opengroup.org/onlinepubs/009695399/utilities/patch.html |
9 | * where there is equivalent functionality busybox patch shall behave | 10 | * (But only does -u, because who still cares about "ed"?) |
10 | * as per GNU patch. | ||
11 | * | 11 | * |
12 | * There is a SUSv3 specification for patch, however it looks to be | 12 | * TODO: |
13 | * incomplete, it doesnt even mention unified diff format. | 13 | * -b backup |
14 | * http://www.opengroup.org/onlinepubs/007904975/utilities/patch.html | 14 | * -l treat all whitespace as a single space |
15 | * -N ignore already applied | ||
16 | * -d chdir first | ||
17 | * -D define wrap #ifdef and #ifndef around changes | ||
18 | * -o outfile output here instead of in place | ||
19 | * -r rejectfile write rejected hunks to this file | ||
15 | * | 20 | * |
16 | * Issues | 21 | * -E remove empty files --remove-empty-files |
17 | * - Non-interactive | 22 | * -f force (no questions asked) |
18 | * - Patches must apply cleanly or patch (not just one hunk) will fail. | 23 | * -F fuzz (number, default 2) |
19 | * - Reject file isnt saved | 24 | * [file] which file to patch |
20 | */ | 25 | |
26 | USE_PATCH(NEWTOY(patch, USE_TOYBOX_DEBUG("x")"up#i:R", TOYFLAG_USR|TOYFLAG_BIN)) | ||
27 | |||
28 | config PATCH | ||
29 | bool "patch" | ||
30 | default y | ||
31 | help | ||
32 | usage: patch [-i file] [-p depth] [-Ru] | ||
33 | |||
34 | Apply a unified diff to one or more files. | ||
21 | 35 | ||
36 | -i Input file (defaults=stdin) | ||
37 | -p number of '/' to strip from start of file paths (default=all) | ||
38 | -R Reverse patch. | ||
39 | -u Ignored (only handles "unified" diffs) | ||
40 | |||
41 | This version of patch only handles unified diffs, and only modifies | ||
42 | a file when all all hunks to that file apply. Patch prints failed | ||
43 | hunks to stderr, and exits with nonzero status if any hunks fail. | ||
44 | |||
45 | A file compared against /dev/null (or with a date <= the epoch) is | ||
46 | created/deleted as appropriate. | ||
47 | */ | ||
22 | #include "libbb.h" | 48 | #include "libbb.h" |
23 | 49 | ||
24 | static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count) | 50 | struct double_list { |
51 | struct double_list *next; | ||
52 | struct double_list *prev; | ||
53 | char *data; | ||
54 | }; | ||
55 | |||
56 | // Return the first item from the list, advancing the list (which must be called | ||
57 | // as &list) | ||
58 | static | ||
59 | void *TOY_llist_pop(void *list) | ||
25 | { | 60 | { |
26 | while (src_stream && lines_count) { | 61 | // I'd use a void ** for the argument, and even accept the typecast in all |
27 | char *line; | 62 | // callers as documentation you need the &, except the stupid compiler |
28 | line = xmalloc_fgets(src_stream); | 63 | // would then scream about type-punned pointers. Screw it. |
29 | if (line == NULL) { | 64 | void **llist = (void **)list; |
30 | break; | 65 | void **next = (void **)*llist; |
31 | } | 66 | *llist = *next; |
32 | if (fputs(line, dst_stream) == EOF) { | 67 | |
33 | bb_perror_msg_and_die("error writing to new file"); | 68 | return (void *)next; |
69 | } | ||
70 | |||
71 | // Free all the elements of a linked list | ||
72 | // if freeit!=NULL call freeit() on each element before freeing it. | ||
73 | static | ||
74 | void TOY_llist_free(void *list, void (*freeit)(void *data)) | ||
75 | { | ||
76 | while (list) { | ||
77 | void *pop = TOY_llist_pop(&list); | ||
78 | if (freeit) freeit(pop); | ||
79 | else free(pop); | ||
80 | |||
81 | // End doubly linked list too. | ||
82 | if (list==pop) break; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // Add an entry to the end off a doubly linked list | ||
87 | static | ||
88 | struct double_list *dlist_add(struct double_list **list, char *data) | ||
89 | { | ||
90 | struct double_list *line = xmalloc(sizeof(struct double_list)); | ||
91 | |||
92 | line->data = data; | ||
93 | if (*list) { | ||
94 | line->next = *list; | ||
95 | line->prev = (*list)->prev; | ||
96 | (*list)->prev->next = line; | ||
97 | (*list)->prev = line; | ||
98 | } else *list = line->next = line->prev = line; | ||
99 | |||
100 | return line; | ||
101 | } | ||
102 | |||
103 | // Ensure entire path exists. | ||
104 | // If mode != -1 set permissions on newly created dirs. | ||
105 | // Requires that path string be writable (for temporary null terminators). | ||
106 | static | ||
107 | void xmkpath(char *path, int mode) | ||
108 | { | ||
109 | char *p, old; | ||
110 | mode_t mask; | ||
111 | int rc; | ||
112 | struct stat st; | ||
113 | |||
114 | for (p = path; ; p++) { | ||
115 | if (!*p || *p == '/') { | ||
116 | old = *p; | ||
117 | *p = rc = 0; | ||
118 | if (stat(path, &st) || !S_ISDIR(st.st_mode)) { | ||
119 | if (mode != -1) { | ||
120 | mask = umask(0); | ||
121 | rc = mkdir(path, mode); | ||
122 | umask(mask); | ||
123 | } else rc = mkdir(path, 0777); | ||
124 | } | ||
125 | *p = old; | ||
126 | if(rc) bb_perror_msg_and_die("mkpath '%s'", path); | ||
34 | } | 127 | } |
35 | free(line); | 128 | if (!*p) break; |
36 | lines_count--; | 129 | } |
130 | } | ||
131 | |||
132 | // Slow, but small. | ||
133 | static | ||
134 | char *get_rawline(int fd, long *plen, char end) | ||
135 | { | ||
136 | char c, *buf = NULL; | ||
137 | long len = 0; | ||
138 | |||
139 | for (;;) { | ||
140 | if (1>read(fd, &c, 1)) break; | ||
141 | if (!(len & 63)) buf=xrealloc(buf, len+65); | ||
142 | if ((buf[len++]=c) == end) break; | ||
143 | } | ||
144 | if (buf) buf[len]=0; | ||
145 | if (plen) *plen = len; | ||
146 | |||
147 | return buf; | ||
148 | } | ||
149 | |||
150 | static | ||
151 | char *get_line(int fd) | ||
152 | { | ||
153 | long len; | ||
154 | char *buf = get_rawline(fd, &len, '\n'); | ||
155 | |||
156 | if (buf && buf[--len]=='\n') buf[len]=0; | ||
157 | |||
158 | return buf; | ||
159 | } | ||
160 | |||
161 | // Copy the rest of in to out and close both files. | ||
162 | static | ||
163 | void xsendfile(int in, int out) | ||
164 | { | ||
165 | long len; | ||
166 | char buf[4096]; | ||
167 | |||
168 | if (in<0) return; | ||
169 | for (;;) { | ||
170 | len = safe_read(in, buf, 4096); | ||
171 | if (len<1) break; | ||
172 | xwrite(out, buf, len); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | // Copy the rest of the data and replace the original with the copy. | ||
177 | static | ||
178 | void replace_tempfile(int fdin, int fdout, char **tempname) | ||
179 | { | ||
180 | char *temp = xstrdup(*tempname); | ||
181 | |||
182 | temp[strlen(temp)-6]=0; | ||
183 | if (fdin != -1) { | ||
184 | xsendfile(fdin, fdout); | ||
185 | xclose(fdin); | ||
37 | } | 186 | } |
38 | return lines_count; | 187 | xclose(fdout); |
188 | rename(*tempname, temp); | ||
189 | free(*tempname); | ||
190 | free(temp); | ||
191 | *tempname = NULL; | ||
192 | } | ||
193 | |||
194 | // Open a temporary file to copy an existing file into. | ||
195 | static | ||
196 | int copy_tempfile(int fdin, char *name, char **tempname) | ||
197 | { | ||
198 | struct stat statbuf; | ||
199 | int fd; | ||
200 | |||
201 | *tempname = xasprintf("%sXXXXXX", name); | ||
202 | fd = mkstemp(*tempname); | ||
203 | if(-1 == fd) bb_perror_msg_and_die("no temp file"); | ||
204 | |||
205 | // Set permissions of output file | ||
206 | fstat(fdin, &statbuf); | ||
207 | fchmod(fd, statbuf.st_mode); | ||
208 | |||
209 | return fd; | ||
210 | } | ||
211 | |||
212 | // Abort the copy and delete the temporary file. | ||
213 | static | ||
214 | void delete_tempfile(int fdin, int fdout, char **tempname) | ||
215 | { | ||
216 | close(fdin); | ||
217 | close(fdout); | ||
218 | unlink(*tempname); | ||
219 | free(*tempname); | ||
220 | *tempname = NULL; | ||
221 | } | ||
222 | |||
223 | |||
224 | |||
225 | struct globals { | ||
226 | char *infile; | ||
227 | long prefix; | ||
228 | |||
229 | struct double_list *current_hunk; | ||
230 | long oldline, oldlen, newline, newlen, linenum; | ||
231 | int context, state, filein, fileout, filepatch, hunknum; | ||
232 | char *tempname; | ||
233 | |||
234 | // was toys.foo: | ||
235 | int exitval; | ||
236 | }; | ||
237 | #define TT (*ptr_to_globals) | ||
238 | #define INIT_TT() do { \ | ||
239 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(TT))); \ | ||
240 | } while (0) | ||
241 | |||
242 | |||
243 | //bbox had: "p:i:RN" | ||
244 | #define FLAG_STR "Rup:i:x" | ||
245 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ | ||
246 | #define FLAG_REVERSE (1 << 0) | ||
247 | #define FLAG_u (1 << 1) | ||
248 | #define FLAG_PATHLEN (1 << 2) | ||
249 | #define FLAG_INPUT (1 << 3) | ||
250 | //non-standard: | ||
251 | #define FLAG_DEBUG (1 << 4) | ||
252 | |||
253 | // Dispose of a line of input, either by writing it out or discarding it. | ||
254 | |||
255 | // state < 2: just free | ||
256 | // state = 2: write whole line to stderr | ||
257 | // state = 3: write whole line to fileout | ||
258 | // state > 3: write line+1 to fileout when *line != state | ||
259 | |||
260 | #define PATCH_DEBUG (option_mask32 & FLAG_DEBUG) | ||
261 | |||
262 | static void do_line(void *data) | ||
263 | { | ||
264 | struct double_list *dlist = (struct double_list *)data; | ||
265 | |||
266 | if (TT.state>1 && *dlist->data != TT.state) | ||
267 | fdprintf(TT.state == 2 ? 2 : TT.fileout, | ||
268 | "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); | ||
269 | |||
270 | if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data); | ||
271 | |||
272 | free(dlist->data); | ||
273 | free(data); | ||
274 | } | ||
275 | |||
276 | static void finish_oldfile(void) | ||
277 | { | ||
278 | if (TT.tempname) replace_tempfile(TT.filein, TT.fileout, &TT.tempname); | ||
279 | TT.fileout = TT.filein = -1; | ||
39 | } | 280 | } |
40 | 281 | ||
41 | /* If patch_level is -1 it will remove all directory names | 282 | static void fail_hunk(void) |
42 | * char *line must be greater than 4 chars | ||
43 | * returns NULL if the file doesnt exist or error | ||
44 | * returns malloc'ed filename | ||
45 | * NB: frees 1st argument! | ||
46 | */ | ||
47 | static char *extract_filename(char *line, int patch_level, const char *pat) | ||
48 | { | 283 | { |
49 | char *temp = NULL, *filename_start_ptr = line + 4; | 284 | if (!TT.current_hunk) return; |
50 | 285 | TT.current_hunk->prev->next = 0; | |
51 | if (strncmp(line, pat, 4) == 0) { | 286 | |
52 | /* Terminate string at end of source filename */ | 287 | fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline); |
53 | line[strcspn(line, "\t\n\r")] = '\0'; | 288 | TT.exitval = 1; |
54 | 289 | ||
55 | /* Skip over (patch_level) number of leading directories */ | 290 | // If we got to this point, we've seeked to the end. Discard changes to |
56 | while (patch_level--) { | 291 | // this file and advance to next file. |
57 | temp = strchr(filename_start_ptr, '/'); | 292 | |
58 | if (!temp) | 293 | TT.state = 2; |
59 | break; | 294 | TOY_llist_free(TT.current_hunk, do_line); |
60 | filename_start_ptr = temp + 1; | 295 | TT.current_hunk = NULL; |
296 | delete_tempfile(TT.filein, TT.fileout, &TT.tempname); | ||
297 | TT.state = 0; | ||
298 | } | ||
299 | |||
300 | // Given a hunk of a unified diff, make the appropriate change to the file. | ||
301 | // This does not use the location information, but instead treats a hunk | ||
302 | // as a sort of regex. Copies data from input to output until it finds | ||
303 | // the change to be made, then outputs the changed data and returns. | ||
304 | // (Finding EOF first is an error.) This is a single pass operation, so | ||
305 | // multiple hunks must occur in order in the file. | ||
306 | |||
307 | static int apply_one_hunk(void) | ||
308 | { | ||
309 | struct double_list *plist, *buf = NULL, *check; | ||
310 | int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0; | ||
311 | |||
312 | // Break doubly linked list so we can use singly linked traversal function. | ||
313 | TT.current_hunk->prev->next = NULL; | ||
314 | |||
315 | // Match EOF if there aren't as many ending context lines as beginning | ||
316 | for (plist = TT.current_hunk; plist; plist = plist->next) { | ||
317 | if (plist->data[0]==' ') matcheof++; | ||
318 | else matcheof = 0; | ||
319 | if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data); | ||
320 | } | ||
321 | matcheof = matcheof < TT.context; | ||
322 | |||
323 | if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); | ||
324 | |||
325 | // Loop through input data searching for this hunk. Match all context | ||
326 | // lines and all lines to be removed until we've found the end of a | ||
327 | // complete hunk. | ||
328 | plist = TT.current_hunk; | ||
329 | buf = NULL; | ||
330 | if (TT.context) for (;;) { | ||
331 | char *data = get_line(TT.filein); | ||
332 | |||
333 | TT.linenum++; | ||
334 | |||
335 | // Figure out which line of hunk to compare with next. (Skip lines | ||
336 | // of the hunk we'd be adding.) | ||
337 | while (plist && *plist->data == "+-"[reverse]) { | ||
338 | if (data && !strcmp(data, plist->data+1)) { | ||
339 | if (!backwarn) { | ||
340 | fdprintf(2,"Possibly reversed hunk %d at %ld\n", | ||
341 | TT.hunknum, TT.linenum); | ||
342 | backwarn++; | ||
343 | } | ||
344 | } | ||
345 | plist = plist->next; | ||
61 | } | 346 | } |
62 | temp = xstrdup(filename_start_ptr); | 347 | |
348 | // Is this EOF? | ||
349 | if (!data) { | ||
350 | if (PATCH_DEBUG) fdprintf(2, "INEOF\n"); | ||
351 | |||
352 | // Does this hunk need to match EOF? | ||
353 | if (!plist && matcheof) break; | ||
354 | |||
355 | // File ended before we found a place for this hunk. | ||
356 | fail_hunk(); | ||
357 | goto done; | ||
358 | } else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data); | ||
359 | check = dlist_add(&buf, data); | ||
360 | |||
361 | // Compare this line with next expected line of hunk. | ||
362 | // todo: teach the strcmp() to ignore whitespace. | ||
363 | |||
364 | // A match can fail because the next line doesn't match, or because | ||
365 | // we hit the end of a hunk that needed EOF, and this isn't EOF. | ||
366 | |||
367 | // If match failed, flush first line of buffered data and | ||
368 | // recheck buffered data for a new match until we find one or run | ||
369 | // out of buffer. | ||
370 | |||
371 | for (;;) { | ||
372 | if (!plist || strcmp(check->data, plist->data+1)) { | ||
373 | // Match failed. Write out first line of buffered data and | ||
374 | // recheck remaining buffered data for a new match. | ||
375 | |||
376 | if (PATCH_DEBUG) | ||
377 | fdprintf(2, "NOT: %s\n", plist->data); | ||
378 | |||
379 | TT.state = 3; | ||
380 | check = TOY_llist_pop(&buf); | ||
381 | check->prev->next = buf; | ||
382 | buf->prev = check->prev; | ||
383 | do_line(check); | ||
384 | plist = TT.current_hunk; | ||
385 | |||
386 | // If we've reached the end of the buffer without confirming a | ||
387 | // match, read more lines. | ||
388 | if (check==buf) { | ||
389 | buf = 0; | ||
390 | break; | ||
391 | } | ||
392 | check = buf; | ||
393 | } else { | ||
394 | if (PATCH_DEBUG) | ||
395 | fdprintf(2, "MAYBE: %s\n", plist->data); | ||
396 | // This line matches. Advance plist, detect successful match. | ||
397 | plist = plist->next; | ||
398 | if (!plist && !matcheof) goto out; | ||
399 | check = check->next; | ||
400 | if (check == buf) break; | ||
401 | } | ||
402 | } | ||
403 | } | ||
404 | out: | ||
405 | // We have a match. Emit changed data. | ||
406 | TT.state = "-+"[reverse]; | ||
407 | TOY_llist_free(TT.current_hunk, do_line); | ||
408 | TT.current_hunk = NULL; | ||
409 | TT.state = 1; | ||
410 | done: | ||
411 | if (buf) { | ||
412 | buf->prev->next = NULL; | ||
413 | TOY_llist_free(buf, do_line); | ||
63 | } | 414 | } |
64 | free(line); | 415 | |
65 | return temp; | 416 | return TT.state; |
66 | } | 417 | } |
67 | 418 | ||
419 | // Read a patch file and find hunks, opening/creating/deleting files. | ||
420 | // Call apply_one_hunk() on each hunk. | ||
421 | |||
422 | // state 0: Not in a hunk, look for +++. | ||
423 | // state 1: Found +++ file indicator, look for @@ | ||
424 | // state 2: In hunk: counting initial context lines | ||
425 | // state 3: In hunk: getting body | ||
426 | |||
68 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 427 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
69 | int patch_main(int argc UNUSED_PARAM, char **argv) | 428 | int patch_main(int argc UNUSED_PARAM, char **argv) |
70 | { | 429 | { |
71 | struct stat saved_stat; | 430 | int opts; |
72 | char *patch_line; | 431 | int reverse, state = 0; |
73 | FILE *patch_file; | 432 | char *oldname = NULL, *newname = NULL; |
74 | int patch_level; | 433 | char *opt_p, *opt_i; |
75 | int ret = 0; | 434 | |
76 | char plus = '+'; | 435 | INIT_TT(); |
77 | unsigned opt; | 436 | |
78 | enum { | 437 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); |
79 | OPT_R = (1 << 2), | 438 | reverse = opts & FLAG_REVERSE; |
80 | OPT_N = (1 << 3), | 439 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! |
81 | /*OPT_f = (1 << 4), ignored */ | 440 | if (opts & FLAG_INPUT) TT.filepatch = xopen(opt_i, O_RDONLY); |
82 | /*OPT_E = (1 << 5), ignored, this is the default */ | 441 | TT.filein = TT.fileout = -1; |
83 | /*OPT_g = (1 << 6), ignored */ | ||
84 | OPT_dry_run = (1 << 7) * ENABLE_LONG_OPTS, | ||
85 | }; | ||
86 | |||
87 | xfunc_error_retval = 2; | ||
88 | { | ||
89 | const char *p = "-1"; | ||
90 | const char *i = "-"; /* compat */ | ||
91 | #if ENABLE_LONG_OPTS | ||
92 | static const char patch_longopts[] ALIGN1 = | ||
93 | "strip\0" Required_argument "p" | ||
94 | "input\0" Required_argument "i" | ||
95 | "reverse\0" No_argument "R" | ||
96 | "forward\0" No_argument "N" | ||
97 | /* "Assume user knows what [s]he is doing, do not ask any questions": */ | ||
98 | "force\0" No_argument "f" /*ignored*/ | ||
99 | # if ENABLE_DESKTOP | ||
100 | "remove-empty-files\0" No_argument "E" /*ignored*/ | ||
101 | /* "Controls actions when a file is under RCS or SCCS control, | ||
102 | * and does not exist or is read-only and matches the default version, | ||
103 | * or when a file is under ClearCase control and does not exist..." | ||
104 | * IOW: rather obscure option. | ||
105 | * But Gentoo's portage does use -g0 */ | ||
106 | "get\0" Required_argument "g" /*ignored*/ | ||
107 | # endif | ||
108 | "dry-run\0" No_argument "\xfd" | ||
109 | # if ENABLE_DESKTOP | ||
110 | "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/ | ||
111 | "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/ | ||
112 | # endif | ||
113 | ; | ||
114 | applet_long_options = patch_longopts; | ||
115 | #endif | ||
116 | /* -f,-E,-g are ignored */ | ||
117 | opt = getopt32(argv, "p:i:RN""fEg:", &p, &i, NULL); | ||
118 | if (opt & OPT_R) | ||
119 | plus = '-'; | ||
120 | patch_level = xatoi(p); /* can be negative! */ | ||
121 | patch_file = xfopen_stdin(i); | ||
122 | } | ||
123 | 442 | ||
124 | patch_line = xmalloc_fgetline(patch_file); | 443 | // Loop through the lines in the patch |
125 | while (patch_line) { | 444 | for(;;) { |
126 | FILE *src_stream; | 445 | char *patchline; |
127 | FILE *dst_stream; | 446 | |
128 | //char *old_filename; | 447 | patchline = get_line(TT.filepatch); |
129 | char *new_filename; | 448 | if (!patchline) break; |
130 | char *backup_filename = NULL; | 449 | |
131 | unsigned src_cur_line = 1; | 450 | // Other versions of patch accept damaged patches, |
132 | unsigned dst_cur_line = 0; | 451 | // so we need to also. |
133 | unsigned dst_beg_line; | 452 | if (!*patchline) { |
134 | unsigned bad_hunk_count = 0; | 453 | free(patchline); |
135 | unsigned hunk_count = 0; | 454 | patchline = xstrdup(" "); |
136 | smallint copy_trailing_lines_flag = 0; | ||
137 | |||
138 | /* Skip everything upto the "---" marker | ||
139 | * No need to parse the lines "Only in <dir>", and "diff <args>" | ||
140 | */ | ||
141 | do { | ||
142 | /* Extract the filename used before the patch was generated */ | ||
143 | new_filename = extract_filename(patch_line, patch_level, "--- "); | ||
144 | // was old_filename above | ||
145 | patch_line = xmalloc_fgetline(patch_file); | ||
146 | if (!patch_line) goto quit; | ||
147 | } while (!new_filename); | ||
148 | free(new_filename); // "source" filename is irrelevant | ||
149 | |||
150 | new_filename = extract_filename(patch_line, patch_level, "+++ "); | ||
151 | if (!new_filename) { | ||
152 | bb_error_msg_and_die("invalid patch"); | ||
153 | } | 455 | } |
154 | 456 | ||
155 | /* Get access rights from the file to be patched */ | 457 | // Are we assembling a hunk? |
156 | if (stat(new_filename, &saved_stat) != 0) { | 458 | if (state >= 2) { |
157 | char *slash = strrchr(new_filename, '/'); | 459 | if (*patchline==' ' || *patchline=='+' || *patchline=='-') { |
158 | if (slash) { | 460 | dlist_add(&TT.current_hunk, patchline); |
159 | /* Create leading directories */ | 461 | |
160 | *slash = '\0'; | 462 | if (*patchline != '+') TT.oldlen--; |
161 | bb_make_directory(new_filename, -1, FILEUTILS_RECUR); | 463 | if (*patchline != '-') TT.newlen--; |
162 | *slash = '/'; | 464 | |
465 | // Context line? | ||
466 | if (*patchline==' ' && state==2) TT.context++; | ||
467 | else state=3; | ||
468 | |||
469 | // If we've consumed all expected hunk lines, apply the hunk. | ||
470 | |||
471 | if (!TT.oldlen && !TT.newlen) state = apply_one_hunk(); | ||
472 | continue; | ||
163 | } | 473 | } |
164 | src_stream = NULL; | 474 | fail_hunk(); |
165 | saved_stat.st_mode = 0644; | 475 | state = 0; |
166 | } else if (!(opt & OPT_dry_run)) { | 476 | continue; |
167 | backup_filename = xasprintf("%s.orig", new_filename); | ||
168 | xrename(new_filename, backup_filename); | ||
169 | src_stream = xfopen_for_read(backup_filename); | ||
170 | } else | ||
171 | src_stream = xfopen_for_read(new_filename); | ||
172 | |||
173 | if (opt & OPT_dry_run) { | ||
174 | dst_stream = xfopen_for_write("/dev/null"); | ||
175 | } else { | ||
176 | dst_stream = xfopen_for_write(new_filename); | ||
177 | fchmod(fileno(dst_stream), saved_stat.st_mode); | ||
178 | } | 477 | } |
179 | 478 | ||
180 | printf("patching file %s\n", new_filename); | 479 | // Open a new file? |
181 | 480 | if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) { | |
182 | /* Handle all hunks for this file */ | 481 | char *s, **name = reverse ? &newname : &oldname; |
183 | patch_line = xmalloc_fgets(patch_file); | 482 | int i; |
184 | while (patch_line) { | 483 | |
185 | unsigned count; | 484 | if (*patchline == '+') { |
186 | unsigned src_beg_line; | 485 | name = reverse ? &oldname : &newname; |
187 | unsigned hunk_offset_start; | 486 | state = 1; |
188 | unsigned src_last_line = 1; | ||
189 | unsigned dst_last_line = 1; | ||
190 | |||
191 | if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3) | ||
192 | && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2) | ||
193 | ) { | ||
194 | /* No more hunks for this file */ | ||
195 | break; | ||
196 | } | ||
197 | if (plus != '+') { | ||
198 | /* reverse patch */ | ||
199 | unsigned tmp = src_last_line; | ||
200 | src_last_line = dst_last_line; | ||
201 | dst_last_line = tmp; | ||
202 | tmp = src_beg_line; | ||
203 | src_beg_line = dst_beg_line; | ||
204 | dst_beg_line = tmp; | ||
205 | } | 487 | } |
206 | hunk_count++; | 488 | |
207 | 489 | free(*name); | |
208 | if (src_beg_line && dst_beg_line) { | 490 | finish_oldfile(); |
209 | /* Copy unmodified lines upto start of hunk */ | 491 | |
210 | /* src_beg_line will be 0 if it's a new file */ | 492 | // Trim date from end of filename (if any). We don't care. |
211 | count = src_beg_line - src_cur_line; | 493 | for (s = patchline+4; *s && *s!='\t'; s++) |
212 | if (copy_lines(src_stream, dst_stream, count)) { | 494 | if (*s=='\\' && s[1]) s++; |
213 | bb_error_msg_and_die("bad src file"); | 495 | i = atoi(s); |
214 | } | 496 | if (i>1900 && i<=1970) |
215 | src_cur_line += count; | 497 | *name = xstrdup("/dev/null"); |
216 | dst_cur_line += count; | 498 | else { |
217 | copy_trailing_lines_flag = 1; | 499 | *s = 0; |
500 | *name = xstrdup(patchline+4); | ||
218 | } | 501 | } |
219 | src_last_line += hunk_offset_start = src_cur_line; | 502 | |
220 | dst_last_line += dst_cur_line; | 503 | // We defer actually opening the file because svn produces broken |
221 | 504 | // patches that don't signal they want to create a new file the | |
222 | while (1) { | 505 | // way the patch man page says, so you have to read the first hunk |
223 | free(patch_line); | 506 | // and _guess_. |
224 | patch_line = xmalloc_fgets(patch_file); | 507 | |
225 | if (patch_line == NULL) | 508 | // Start a new hunk? |
226 | break; /* EOF */ | 509 | } else if (state == 1 && !strncmp("@@ -", patchline, 4)) { |
227 | if (!*patch_line) { | 510 | int i; |
228 | /* whitespace-damaged patch with "" lines */ | 511 | |
229 | free(patch_line); | 512 | i = sscanf(patchline+4, "%ld,%ld +%ld,%ld", &TT.oldline, |
230 | patch_line = xstrdup(" "); | 513 | &TT.oldlen, &TT.newline, &TT.newlen); |
514 | if (i != 4) | ||
515 | bb_error_msg_and_die("corrupt hunk %d at %ld", TT.hunknum, TT.linenum); | ||
516 | |||
517 | TT.context = 0; | ||
518 | state = 2; | ||
519 | |||
520 | // If this is the first hunk, open the file. | ||
521 | if (TT.filein == -1) { | ||
522 | int oldsum, newsum, del = 0; | ||
523 | char *s, *name; | ||
524 | |||
525 | oldsum = TT.oldline + TT.oldlen; | ||
526 | newsum = TT.newline + TT.newlen; | ||
527 | |||
528 | name = reverse ? oldname : newname; | ||
529 | |||
530 | // We're deleting oldname if new file is /dev/null (before -p) | ||
531 | // or if new hunk is empty (zero context) after patching | ||
532 | if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) | ||
533 | { | ||
534 | name = reverse ? newname : oldname; | ||
535 | del++; | ||
231 | } | 536 | } |
232 | if ((*patch_line != '-') && (*patch_line != '+') | 537 | |
233 | && (*patch_line != ' ') | 538 | // handle -p path truncation. |
234 | ) { | 539 | for (i=0, s = name; *s;) { |
235 | break; /* End of hunk */ | 540 | if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i) break; |
541 | if (*(s++)=='/') { | ||
542 | name = s; | ||
543 | i++; | ||
544 | } | ||
236 | } | 545 | } |
237 | if (*patch_line != plus) { /* '-' or ' ' */ | 546 | |
238 | char *src_line = NULL; | 547 | if (del) { |
239 | if (src_cur_line == src_last_line) | 548 | printf("removing %s\n", name); |
240 | break; | 549 | xunlink(name); |
241 | if (src_stream) { | 550 | state = 0; |
242 | src_line = xmalloc_fgets(src_stream); | 551 | // If we've got a file to open, do so. |
243 | if (src_line) { | 552 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { |
244 | int diff = strcmp(src_line, patch_line + 1); | 553 | // If the old file was null, we're creating a new one. |
245 | src_cur_line++; | 554 | if (!strcmp(oldname, "/dev/null") || !oldsum) { |
246 | free(src_line); | 555 | printf("creating %s\n", name); |
247 | if (diff) | 556 | s = strrchr(name, '/'); |
248 | src_line = NULL; | 557 | if (s) { |
558 | *s = 0; | ||
559 | xmkpath(name, -1); | ||
560 | *s = '/'; | ||
249 | } | 561 | } |
562 | TT.filein = xopen3(name, O_CREAT|O_EXCL|O_RDWR, 0666); | ||
563 | } else { | ||
564 | printf("patching file %s\n", name); | ||
565 | TT.filein = xopen(name, O_RDWR); | ||
250 | } | 566 | } |
251 | /* Do not patch an already patched hunk with -N */ | 567 | TT.fileout = copy_tempfile(TT.filein, name, &TT.tempname); |
252 | if (src_line == 0 && (opt & OPT_N)) { | 568 | TT.linenum = 0; |
253 | continue; | 569 | TT.hunknum = 0; |
254 | } | ||
255 | if (!src_line) { | ||
256 | bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start); | ||
257 | bad_hunk_count++; | ||
258 | break; | ||
259 | } | ||
260 | if (*patch_line != ' ') { /* '-' */ | ||
261 | continue; | ||
262 | } | ||
263 | } | 570 | } |
264 | if (dst_cur_line == dst_last_line) | ||
265 | break; | ||
266 | fputs(patch_line + 1, dst_stream); | ||
267 | dst_cur_line++; | ||
268 | } /* end of while loop handling one hunk */ | ||
269 | } /* end of while loop handling one file */ | ||
270 | |||
271 | /* Cleanup last patched file */ | ||
272 | if (copy_trailing_lines_flag) { | ||
273 | copy_lines(src_stream, dst_stream, (unsigned)(-1)); | ||
274 | } | ||
275 | if (src_stream) { | ||
276 | fclose(src_stream); | ||
277 | } | ||
278 | fclose(dst_stream); | ||
279 | if (bad_hunk_count) { | ||
280 | ret = 1; | ||
281 | bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count); | ||
282 | } else { | ||
283 | /* It worked, we can remove the backup */ | ||
284 | if (backup_filename) { | ||
285 | unlink(backup_filename); | ||
286 | } | ||
287 | if (!(opt & OPT_dry_run) | ||
288 | && ((dst_cur_line == 0) || (dst_beg_line == 0)) | ||
289 | ) { | ||
290 | /* The new patched file is empty, remove it */ | ||
291 | xunlink(new_filename); | ||
292 | // /* old_filename and new_filename may be the same file */ | ||
293 | // unlink(old_filename); | ||
294 | } | 571 | } |
572 | |||
573 | TT.hunknum++; | ||
574 | |||
575 | continue; | ||
295 | } | 576 | } |
296 | free(backup_filename); | 577 | |
297 | //free(old_filename); | 578 | // If we didn't continue above, discard this line. |
298 | free(new_filename); | 579 | free(patchline); |
299 | } /* end of "while there are patch lines" */ | 580 | } |
300 | quit: | 581 | |
301 | /* 0 = SUCCESS | 582 | finish_oldfile(); |
302 | * 1 = Some hunks failed | 583 | |
303 | * 2 = More serious problems (exited earlier) | 584 | if (ENABLE_FEATURE_CLEAN_UP) { |
304 | */ | 585 | close(TT.filepatch); |
305 | return ret; | 586 | free(oldname); |
587 | free(newname); | ||
588 | } | ||
589 | |||
590 | return TT.exitval; | ||
306 | } | 591 | } |
diff --git a/editors/patch_bbox.c b/editors/patch_bbox.c new file mode 100644 index 000000000..62477af16 --- /dev/null +++ b/editors/patch_bbox.c | |||
@@ -0,0 +1,306 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * busybox patch applet to handle the unified diff format. | ||
4 | * Copyright (C) 2003 Glenn McGrath | ||
5 | * | ||
6 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
7 | * | ||
8 | * This applet is written to work with patches generated by GNU diff, | ||
9 | * where there is equivalent functionality busybox patch shall behave | ||
10 | * as per GNU patch. | ||
11 | * | ||
12 | * There is a SUSv3 specification for patch, however it looks to be | ||
13 | * incomplete, it doesnt even mention unified diff format. | ||
14 | * http://www.opengroup.org/onlinepubs/007904975/utilities/patch.html | ||
15 | * | ||
16 | * Issues | ||
17 | * - Non-interactive | ||
18 | * - Patches must apply cleanly or patch (not just one hunk) will fail. | ||
19 | * - Reject file isnt saved | ||
20 | */ | ||
21 | |||
22 | #include "libbb.h" | ||
23 | |||
24 | static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count) | ||
25 | { | ||
26 | while (src_stream && lines_count) { | ||
27 | char *line; | ||
28 | line = xmalloc_fgets(src_stream); | ||
29 | if (line == NULL) { | ||
30 | break; | ||
31 | } | ||
32 | if (fputs(line, dst_stream) == EOF) { | ||
33 | bb_perror_msg_and_die("error writing to new file"); | ||
34 | } | ||
35 | free(line); | ||
36 | lines_count--; | ||
37 | } | ||
38 | return lines_count; | ||
39 | } | ||
40 | |||
41 | /* If patch_level is -1 it will remove all directory names | ||
42 | * char *line must be greater than 4 chars | ||
43 | * returns NULL if the file doesnt exist or error | ||
44 | * returns malloc'ed filename | ||
45 | * NB: frees 1st argument! | ||
46 | */ | ||
47 | static char *extract_filename(char *line, int patch_level, const char *pat) | ||
48 | { | ||
49 | char *temp = NULL, *filename_start_ptr = line + 4; | ||
50 | |||
51 | if (strncmp(line, pat, 4) == 0) { | ||
52 | /* Terminate string at end of source filename */ | ||
53 | line[strcspn(line, "\t\n\r")] = '\0'; | ||
54 | |||
55 | /* Skip over (patch_level) number of leading directories */ | ||
56 | while (patch_level--) { | ||
57 | temp = strchr(filename_start_ptr, '/'); | ||
58 | if (!temp) | ||
59 | break; | ||
60 | filename_start_ptr = temp + 1; | ||
61 | } | ||
62 | temp = xstrdup(filename_start_ptr); | ||
63 | } | ||
64 | free(line); | ||
65 | return temp; | ||
66 | } | ||
67 | |||
68 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
69 | int patch_main(int argc UNUSED_PARAM, char **argv) | ||
70 | { | ||
71 | struct stat saved_stat; | ||
72 | char *patch_line; | ||
73 | FILE *patch_file; | ||
74 | int patch_level; | ||
75 | int ret = 0; | ||
76 | char plus = '+'; | ||
77 | unsigned opt; | ||
78 | enum { | ||
79 | OPT_R = (1 << 2), | ||
80 | OPT_N = (1 << 3), | ||
81 | /*OPT_f = (1 << 4), ignored */ | ||
82 | /*OPT_E = (1 << 5), ignored, this is the default */ | ||
83 | /*OPT_g = (1 << 6), ignored */ | ||
84 | OPT_dry_run = (1 << 7) * ENABLE_LONG_OPTS, | ||
85 | }; | ||
86 | |||
87 | xfunc_error_retval = 2; | ||
88 | { | ||
89 | const char *p = "-1"; | ||
90 | const char *i = "-"; /* compat */ | ||
91 | #if ENABLE_LONG_OPTS | ||
92 | static const char patch_longopts[] ALIGN1 = | ||
93 | "strip\0" Required_argument "p" | ||
94 | "input\0" Required_argument "i" | ||
95 | "reverse\0" No_argument "R" | ||
96 | "forward\0" No_argument "N" | ||
97 | /* "Assume user knows what [s]he is doing, do not ask any questions": */ | ||
98 | "force\0" No_argument "f" /*ignored*/ | ||
99 | # if ENABLE_DESKTOP | ||
100 | "remove-empty-files\0" No_argument "E" /*ignored*/ | ||
101 | /* "Controls actions when a file is under RCS or SCCS control, | ||
102 | * and does not exist or is read-only and matches the default version, | ||
103 | * or when a file is under ClearCase control and does not exist..." | ||
104 | * IOW: rather obscure option. | ||
105 | * But Gentoo's portage does use -g0 */ | ||
106 | "get\0" Required_argument "g" /*ignored*/ | ||
107 | # endif | ||
108 | "dry-run\0" No_argument "\xfd" | ||
109 | # if ENABLE_DESKTOP | ||
110 | "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/ | ||
111 | "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/ | ||
112 | # endif | ||
113 | ; | ||
114 | applet_long_options = patch_longopts; | ||
115 | #endif | ||
116 | /* -f,-E,-g are ignored */ | ||
117 | opt = getopt32(argv, "p:i:RN""fEg:", &p, &i, NULL); | ||
118 | if (opt & OPT_R) | ||
119 | plus = '-'; | ||
120 | patch_level = xatoi(p); /* can be negative! */ | ||
121 | patch_file = xfopen_stdin(i); | ||
122 | } | ||
123 | |||
124 | patch_line = xmalloc_fgetline(patch_file); | ||
125 | while (patch_line) { | ||
126 | FILE *src_stream; | ||
127 | FILE *dst_stream; | ||
128 | //char *old_filename; | ||
129 | char *new_filename; | ||
130 | char *backup_filename = NULL; | ||
131 | unsigned src_cur_line = 1; | ||
132 | unsigned dst_cur_line = 0; | ||
133 | unsigned dst_beg_line; | ||
134 | unsigned bad_hunk_count = 0; | ||
135 | unsigned hunk_count = 0; | ||
136 | smallint copy_trailing_lines_flag = 0; | ||
137 | |||
138 | /* Skip everything upto the "---" marker | ||
139 | * No need to parse the lines "Only in <dir>", and "diff <args>" | ||
140 | */ | ||
141 | do { | ||
142 | /* Extract the filename used before the patch was generated */ | ||
143 | new_filename = extract_filename(patch_line, patch_level, "--- "); | ||
144 | // was old_filename above | ||
145 | patch_line = xmalloc_fgetline(patch_file); | ||
146 | if (!patch_line) goto quit; | ||
147 | } while (!new_filename); | ||
148 | free(new_filename); // "source" filename is irrelevant | ||
149 | |||
150 | new_filename = extract_filename(patch_line, patch_level, "+++ "); | ||
151 | if (!new_filename) { | ||
152 | bb_error_msg_and_die("invalid patch"); | ||
153 | } | ||
154 | |||
155 | /* Get access rights from the file to be patched */ | ||
156 | if (stat(new_filename, &saved_stat) != 0) { | ||
157 | char *slash = strrchr(new_filename, '/'); | ||
158 | if (slash) { | ||
159 | /* Create leading directories */ | ||
160 | *slash = '\0'; | ||
161 | bb_make_directory(new_filename, -1, FILEUTILS_RECUR); | ||
162 | *slash = '/'; | ||
163 | } | ||
164 | src_stream = NULL; | ||
165 | saved_stat.st_mode = 0644; | ||
166 | } else if (!(opt & OPT_dry_run)) { | ||
167 | backup_filename = xasprintf("%s.orig", new_filename); | ||
168 | xrename(new_filename, backup_filename); | ||
169 | src_stream = xfopen_for_read(backup_filename); | ||
170 | } else | ||
171 | src_stream = xfopen_for_read(new_filename); | ||
172 | |||
173 | if (opt & OPT_dry_run) { | ||
174 | dst_stream = xfopen_for_write("/dev/null"); | ||
175 | } else { | ||
176 | dst_stream = xfopen_for_write(new_filename); | ||
177 | fchmod(fileno(dst_stream), saved_stat.st_mode); | ||
178 | } | ||
179 | |||
180 | printf("patching file %s\n", new_filename); | ||
181 | |||
182 | /* Handle all hunks for this file */ | ||
183 | patch_line = xmalloc_fgets(patch_file); | ||
184 | while (patch_line) { | ||
185 | unsigned count; | ||
186 | unsigned src_beg_line; | ||
187 | unsigned hunk_offset_start; | ||
188 | unsigned src_last_line = 1; | ||
189 | unsigned dst_last_line = 1; | ||
190 | |||
191 | if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3) | ||
192 | && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2) | ||
193 | ) { | ||
194 | /* No more hunks for this file */ | ||
195 | break; | ||
196 | } | ||
197 | if (plus != '+') { | ||
198 | /* reverse patch */ | ||
199 | unsigned tmp = src_last_line; | ||
200 | src_last_line = dst_last_line; | ||
201 | dst_last_line = tmp; | ||
202 | tmp = src_beg_line; | ||
203 | src_beg_line = dst_beg_line; | ||
204 | dst_beg_line = tmp; | ||
205 | } | ||
206 | hunk_count++; | ||
207 | |||
208 | if (src_beg_line && dst_beg_line) { | ||
209 | /* Copy unmodified lines upto start of hunk */ | ||
210 | /* src_beg_line will be 0 if it's a new file */ | ||
211 | count = src_beg_line - src_cur_line; | ||
212 | if (copy_lines(src_stream, dst_stream, count)) { | ||
213 | bb_error_msg_and_die("bad src file"); | ||
214 | } | ||
215 | src_cur_line += count; | ||
216 | dst_cur_line += count; | ||
217 | copy_trailing_lines_flag = 1; | ||
218 | } | ||
219 | src_last_line += hunk_offset_start = src_cur_line; | ||
220 | dst_last_line += dst_cur_line; | ||
221 | |||
222 | while (1) { | ||
223 | free(patch_line); | ||
224 | patch_line = xmalloc_fgets(patch_file); | ||
225 | if (patch_line == NULL) | ||
226 | break; /* EOF */ | ||
227 | if (!*patch_line) { | ||
228 | /* whitespace-damaged patch with "" lines */ | ||
229 | free(patch_line); | ||
230 | patch_line = xstrdup(" "); | ||
231 | } | ||
232 | if ((*patch_line != '-') && (*patch_line != '+') | ||
233 | && (*patch_line != ' ') | ||
234 | ) { | ||
235 | break; /* End of hunk */ | ||
236 | } | ||
237 | if (*patch_line != plus) { /* '-' or ' ' */ | ||
238 | char *src_line = NULL; | ||
239 | if (src_cur_line == src_last_line) | ||
240 | break; | ||
241 | if (src_stream) { | ||
242 | src_line = xmalloc_fgets(src_stream); | ||
243 | if (src_line) { | ||
244 | int diff = strcmp(src_line, patch_line + 1); | ||
245 | src_cur_line++; | ||
246 | free(src_line); | ||
247 | if (diff) | ||
248 | src_line = NULL; | ||
249 | } | ||
250 | } | ||
251 | /* Do not patch an already patched hunk with -N */ | ||
252 | if (src_line == 0 && (opt & OPT_N)) { | ||
253 | continue; | ||
254 | } | ||
255 | if (!src_line) { | ||
256 | bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start); | ||
257 | bad_hunk_count++; | ||
258 | break; | ||
259 | } | ||
260 | if (*patch_line != ' ') { /* '-' */ | ||
261 | continue; | ||
262 | } | ||
263 | } | ||
264 | if (dst_cur_line == dst_last_line) | ||
265 | break; | ||
266 | fputs(patch_line + 1, dst_stream); | ||
267 | dst_cur_line++; | ||
268 | } /* end of while loop handling one hunk */ | ||
269 | } /* end of while loop handling one file */ | ||
270 | |||
271 | /* Cleanup last patched file */ | ||
272 | if (copy_trailing_lines_flag) { | ||
273 | copy_lines(src_stream, dst_stream, (unsigned)(-1)); | ||
274 | } | ||
275 | if (src_stream) { | ||
276 | fclose(src_stream); | ||
277 | } | ||
278 | fclose(dst_stream); | ||
279 | if (bad_hunk_count) { | ||
280 | ret = 1; | ||
281 | bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count); | ||
282 | } else { | ||
283 | /* It worked, we can remove the backup */ | ||
284 | if (backup_filename) { | ||
285 | unlink(backup_filename); | ||
286 | } | ||
287 | if (!(opt & OPT_dry_run) | ||
288 | && ((dst_cur_line == 0) || (dst_beg_line == 0)) | ||
289 | ) { | ||
290 | /* The new patched file is empty, remove it */ | ||
291 | xunlink(new_filename); | ||
292 | // /* old_filename and new_filename may be the same file */ | ||
293 | // unlink(old_filename); | ||
294 | } | ||
295 | } | ||
296 | free(backup_filename); | ||
297 | //free(old_filename); | ||
298 | free(new_filename); | ||
299 | } /* end of "while there are patch lines" */ | ||
300 | quit: | ||
301 | /* 0 = SUCCESS | ||
302 | * 1 = Some hunks failed | ||
303 | * 2 = More serious problems (exited earlier) | ||
304 | */ | ||
305 | return ret; | ||
306 | } | ||
diff --git a/editors/patch_toybox.c b/editors/patch_toybox.c index 0e5c070e2..7f3234e66 100644 --- a/editors/patch_toybox.c +++ b/editors/patch_toybox.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* Adapted from toybox's patch. Currently unused */ | 1 | /* Adapted from toybox's patch. */ |
2 | 2 | ||
3 | /* vi: set sw=4 ts=4: | 3 | /* vi: set sw=4 ts=4: |
4 | * | 4 | * |
@@ -23,7 +23,7 @@ | |||
23 | * -F fuzz (number, default 2) | 23 | * -F fuzz (number, default 2) |
24 | * [file] which file to patch | 24 | * [file] which file to patch |
25 | 25 | ||
26 | USE_PATCH(NEWTOY(patch, "up#i:R", TOYFLAG_USR|TOYFLAG_BIN)) | 26 | USE_PATCH(NEWTOY(patch, USE_TOYBOX_DEBUG("x")"up#i:R", TOYFLAG_USR|TOYFLAG_BIN)) |
27 | 27 | ||
28 | config PATCH | 28 | config PATCH |
29 | bool "patch" | 29 | bool "patch" |
@@ -223,15 +223,16 @@ void delete_tempfile(int fdin, int fdout, char **tempname) | |||
223 | 223 | ||
224 | 224 | ||
225 | struct globals { | 225 | struct globals { |
226 | struct double_list *plines; | 226 | char *infile; |
227 | long linenum; | 227 | long prefix; |
228 | int context; | 228 | |
229 | int hunknum; | 229 | struct double_list *current_hunk; |
230 | int filein; | 230 | long oldline, oldlen, newline, newlen, linenum; |
231 | int fileout; | 231 | int context, state, filein, fileout, filepatch, hunknum; |
232 | int state; | ||
233 | char *tempname; | 232 | char *tempname; |
234 | smallint exitval; | 233 | |
234 | // was toys.foo: | ||
235 | int exitval; | ||
235 | }; | 236 | }; |
236 | #define TT (*ptr_to_globals) | 237 | #define TT (*ptr_to_globals) |
237 | #define INIT_TT() do { \ | 238 | #define INIT_TT() do { \ |
@@ -240,12 +241,14 @@ struct globals { | |||
240 | 241 | ||
241 | 242 | ||
242 | //bbox had: "p:i:RN" | 243 | //bbox had: "p:i:RN" |
243 | #define FLAG_STR "Rup:i:" | 244 | #define FLAG_STR "Rup:i:x" |
244 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ | 245 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ |
245 | #define FLAG_REVERSE (1 << 0) | 246 | #define FLAG_REVERSE (1 << 0) |
246 | #define FLAG_u (1 << 1) | 247 | #define FLAG_u (1 << 1) |
247 | #define FLAG_PATHLEN (1 << 2) | 248 | #define FLAG_PATHLEN (1 << 2) |
248 | #define FLAG_INPUT (1 << 3) | 249 | #define FLAG_INPUT (1 << 3) |
250 | //non-standard: | ||
251 | #define FLAG_DEBUG (1 << 4) | ||
249 | 252 | ||
250 | // Dispose of a line of input, either by writing it out or discarding it. | 253 | // Dispose of a line of input, either by writing it out or discarding it. |
251 | 254 | ||
@@ -254,6 +257,8 @@ struct globals { | |||
254 | // state = 3: write whole line to fileout | 257 | // state = 3: write whole line to fileout |
255 | // state > 3: write line+1 to fileout when *line != state | 258 | // state > 3: write line+1 to fileout when *line != state |
256 | 259 | ||
260 | #define PATCH_DEBUG (option_mask32 & FLAG_DEBUG) | ||
261 | |||
257 | static void do_line(void *data) | 262 | static void do_line(void *data) |
258 | { | 263 | { |
259 | struct double_list *dlist = (struct double_list *)data; | 264 | struct double_list *dlist = (struct double_list *)data; |
@@ -262,6 +267,8 @@ static void do_line(void *data) | |||
262 | fdprintf(TT.state == 2 ? 2 : TT.fileout, | 267 | fdprintf(TT.state == 2 ? 2 : TT.fileout, |
263 | "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); | 268 | "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); |
264 | 269 | ||
270 | if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data); | ||
271 | |||
265 | free(dlist->data); | 272 | free(dlist->data); |
266 | free(data); | 273 | free(data); |
267 | } | 274 | } |
@@ -274,94 +281,118 @@ static void finish_oldfile(void) | |||
274 | 281 | ||
275 | static void fail_hunk(void) | 282 | static void fail_hunk(void) |
276 | { | 283 | { |
277 | if (!TT.plines) return; | 284 | if (!TT.current_hunk) return; |
278 | TT.plines->prev->next = 0; | 285 | TT.current_hunk->prev->next = 0; |
279 | 286 | ||
280 | fdprintf(2, "Hunk %d FAILED.\n", TT.hunknum); | 287 | fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline); |
281 | TT.exitval = 1; | 288 | TT.exitval = 1; |
282 | 289 | ||
283 | // If we got to this point, we've seeked to the end. Discard changes to | 290 | // If we got to this point, we've seeked to the end. Discard changes to |
284 | // this file and advance to next file. | 291 | // this file and advance to next file. |
285 | 292 | ||
286 | TT.state = 2; | 293 | TT.state = 2; |
287 | TOY_llist_free(TT.plines, do_line); | 294 | TOY_llist_free(TT.current_hunk, do_line); |
288 | TT.plines = NULL; | 295 | TT.current_hunk = NULL; |
289 | delete_tempfile(TT.filein, TT.fileout, &TT.tempname); | 296 | delete_tempfile(TT.filein, TT.fileout, &TT.tempname); |
290 | TT.state = 0; | 297 | TT.state = 0; |
291 | } | 298 | } |
292 | 299 | ||
293 | static int apply_hunk(void) | 300 | // Given a hunk of a unified diff, make the appropriate change to the file. |
301 | // This does not use the location information, but instead treats a hunk | ||
302 | // as a sort of regex. Copies data from input to output until it finds | ||
303 | // the change to be made, then outputs the changed data and returns. | ||
304 | // (Finding EOF first is an error.) This is a single pass operation, so | ||
305 | // multiple hunks must occur in order in the file. | ||
306 | |||
307 | static int apply_one_hunk(void) | ||
294 | { | 308 | { |
295 | struct double_list *plist, *buf = NULL, *check; | 309 | struct double_list *plist, *buf = NULL, *check; |
296 | int i = 0, backwards = 0, matcheof = 0, | 310 | int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0; |
297 | reverse = option_mask32 & FLAG_REVERSE; | ||
298 | 311 | ||
299 | // Break doubly linked list so we can use singly linked traversal function. | 312 | // Break doubly linked list so we can use singly linked traversal function. |
300 | TT.plines->prev->next = NULL; | 313 | TT.current_hunk->prev->next = NULL; |
301 | 314 | ||
302 | // Match EOF if there aren't as many ending context lines as beginning | 315 | // Match EOF if there aren't as many ending context lines as beginning |
303 | for (plist = TT.plines; plist; plist = plist->next) { | 316 | for (plist = TT.current_hunk; plist; plist = plist->next) { |
304 | if (plist->data[0]==' ') i++; | 317 | if (plist->data[0]==' ') matcheof++; |
305 | else i = 0; | 318 | else matcheof = 0; |
319 | if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data); | ||
306 | } | 320 | } |
307 | if (i < TT.context) matcheof++; | 321 | matcheof = matcheof < TT.context; |
308 | 322 | ||
309 | // Search for a place to apply this hunk. Match all context lines and | 323 | if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); |
310 | // lines to be removed. | ||
311 | plist = TT.plines; | ||
312 | buf = NULL; | ||
313 | i = 0; | ||
314 | 324 | ||
315 | // Start of for loop | 325 | // Loop through input data searching for this hunk. Match all context |
326 | // lines and all lines to be removed until we've found the end of a | ||
327 | // complete hunk. | ||
328 | plist = TT.current_hunk; | ||
329 | buf = NULL; | ||
316 | if (TT.context) for (;;) { | 330 | if (TT.context) for (;;) { |
317 | char *data = get_line(TT.filein); | 331 | char *data = get_line(TT.filein); |
318 | 332 | ||
319 | TT.linenum++; | 333 | TT.linenum++; |
320 | 334 | ||
321 | // Skip lines of the hunk we'd be adding. | 335 | // Figure out which line of hunk to compare with next. (Skip lines |
336 | // of the hunk we'd be adding.) | ||
322 | while (plist && *plist->data == "+-"[reverse]) { | 337 | while (plist && *plist->data == "+-"[reverse]) { |
323 | if (data && !strcmp(data, plist->data+1)) { | 338 | if (data && !strcmp(data, plist->data+1)) { |
324 | if (++backwards == TT.context) | 339 | if (!backwarn) { |
325 | fdprintf(2,"Possibly reversed hunk %d at %ld\n", | 340 | fdprintf(2,"Possibly reversed hunk %d at %ld\n", |
326 | TT.hunknum, TT.linenum); | 341 | TT.hunknum, TT.linenum); |
327 | } else backwards=0; | 342 | backwarn++; |
343 | } | ||
344 | } | ||
328 | plist = plist->next; | 345 | plist = plist->next; |
329 | } | 346 | } |
330 | 347 | ||
331 | // Is this EOF? | 348 | // Is this EOF? |
332 | if (!data) { | 349 | if (!data) { |
350 | if (PATCH_DEBUG) fdprintf(2, "INEOF\n"); | ||
351 | |||
333 | // Does this hunk need to match EOF? | 352 | // Does this hunk need to match EOF? |
334 | if (!plist && matcheof) break; | 353 | if (!plist && matcheof) break; |
335 | 354 | ||
336 | // File ended before we found a place for this hunk. | 355 | // File ended before we found a place for this hunk. |
337 | fail_hunk(); | 356 | fail_hunk(); |
338 | goto done; | 357 | goto done; |
339 | } | 358 | } else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data); |
340 | check = dlist_add(&buf, data); | 359 | check = dlist_add(&buf, data); |
341 | 360 | ||
361 | // Compare this line with next expected line of hunk. | ||
342 | // todo: teach the strcmp() to ignore whitespace. | 362 | // todo: teach the strcmp() to ignore whitespace. |
343 | 363 | ||
344 | for (;;) { | 364 | // A match can fail because the next line doesn't match, or because |
345 | // If we hit the end of a hunk that needed EOF and this isn't EOF, | 365 | // we hit the end of a hunk that needed EOF, and this isn't EOF. |
346 | // or next line doesn't match, flush first line of buffered data and | 366 | |
347 | // recheck match until we find a new match or run out of buffer. | 367 | // If match failed, flush first line of buffered data and |
368 | // recheck buffered data for a new match until we find one or run | ||
369 | // out of buffer. | ||
348 | 370 | ||
371 | for (;;) { | ||
349 | if (!plist || strcmp(check->data, plist->data+1)) { | 372 | if (!plist || strcmp(check->data, plist->data+1)) { |
350 | // First line isn't a match, write it out. | 373 | // Match failed. Write out first line of buffered data and |
374 | // recheck remaining buffered data for a new match. | ||
375 | |||
376 | if (PATCH_DEBUG) | ||
377 | fdprintf(2, "NOT: %s\n", plist->data); | ||
378 | |||
351 | TT.state = 3; | 379 | TT.state = 3; |
352 | check = TOY_llist_pop(&buf); | 380 | check = TOY_llist_pop(&buf); |
353 | check->prev->next = buf; | 381 | check->prev->next = buf; |
354 | buf->prev = check->prev; | 382 | buf->prev = check->prev; |
355 | do_line(check); | 383 | do_line(check); |
356 | plist = TT.plines; | 384 | plist = TT.current_hunk; |
357 | 385 | ||
358 | // Out of buffered lines? | 386 | // If we've reached the end of the buffer without confirming a |
387 | // match, read more lines. | ||
359 | if (check==buf) { | 388 | if (check==buf) { |
360 | buf = 0; | 389 | buf = 0; |
361 | break; | 390 | break; |
362 | } | 391 | } |
363 | check = buf; | 392 | check = buf; |
364 | } else { | 393 | } else { |
394 | if (PATCH_DEBUG) | ||
395 | fdprintf(2, "MAYBE: %s\n", plist->data); | ||
365 | // This line matches. Advance plist, detect successful match. | 396 | // This line matches. Advance plist, detect successful match. |
366 | plist = plist->next; | 397 | plist = plist->next; |
367 | if (!plist && !matcheof) goto out; | 398 | if (!plist && !matcheof) goto out; |
@@ -371,10 +402,10 @@ static int apply_hunk(void) | |||
371 | } | 402 | } |
372 | } | 403 | } |
373 | out: | 404 | out: |
374 | // Got it. Emit changed data. | 405 | // We have a match. Emit changed data. |
375 | TT.state = "-+"[reverse]; | 406 | TT.state = "-+"[reverse]; |
376 | TOY_llist_free(TT.plines, do_line); | 407 | TOY_llist_free(TT.current_hunk, do_line); |
377 | TT.plines = NULL; | 408 | TT.current_hunk = NULL; |
378 | TT.state = 1; | 409 | TT.state = 1; |
379 | done: | 410 | done: |
380 | if (buf) { | 411 | if (buf) { |
@@ -385,6 +416,9 @@ done: | |||
385 | return TT.state; | 416 | return TT.state; |
386 | } | 417 | } |
387 | 418 | ||
419 | // Read a patch file and find hunks, opening/creating/deleting files. | ||
420 | // Call apply_one_hunk() on each hunk. | ||
421 | |||
388 | // state 0: Not in a hunk, look for +++. | 422 | // state 0: Not in a hunk, look for +++. |
389 | // state 1: Found +++ file indicator, look for @@ | 423 | // state 1: Found +++ file indicator, look for @@ |
390 | // state 2: In hunk: counting initial context lines | 424 | // state 2: In hunk: counting initial context lines |
@@ -397,24 +431,20 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
397 | int reverse, state = 0; | 431 | int reverse, state = 0; |
398 | char *oldname = NULL, *newname = NULL; | 432 | char *oldname = NULL, *newname = NULL; |
399 | char *opt_p, *opt_i; | 433 | char *opt_p, *opt_i; |
400 | int prefix; | ||
401 | |||
402 | long oldline = 0, oldlen = 0, newline = 0, newlen = 0; | ||
403 | 434 | ||
404 | INIT_TT(); | 435 | INIT_TT(); |
405 | 436 | ||
406 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); | 437 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); |
407 | reverse = opts & FLAG_REVERSE; | 438 | reverse = opts & FLAG_REVERSE; |
408 | 439 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! | |
409 | if (opts & FLAG_INPUT) xmove_fd(xopen(opt_i, O_RDONLY), STDIN_FILENO); | 440 | if (opts & FLAG_INPUT) TT.filepatch = xopen(opt_i, O_RDONLY); |
410 | prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! | ||
411 | TT.filein = TT.fileout = -1; | 441 | TT.filein = TT.fileout = -1; |
412 | 442 | ||
413 | // Loop through the lines in the patch | 443 | // Loop through the lines in the patch |
414 | for(;;) { | 444 | for(;;) { |
415 | char *patchline; | 445 | char *patchline; |
416 | 446 | ||
417 | patchline = get_line(STDIN_FILENO); | 447 | patchline = get_line(TT.filepatch); |
418 | if (!patchline) break; | 448 | if (!patchline) break; |
419 | 449 | ||
420 | // Other versions of patch accept damaged patches, | 450 | // Other versions of patch accept damaged patches, |
@@ -427,17 +457,18 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
427 | // Are we assembling a hunk? | 457 | // Are we assembling a hunk? |
428 | if (state >= 2) { | 458 | if (state >= 2) { |
429 | if (*patchline==' ' || *patchline=='+' || *patchline=='-') { | 459 | if (*patchline==' ' || *patchline=='+' || *patchline=='-') { |
430 | dlist_add(&TT.plines, patchline); | 460 | dlist_add(&TT.current_hunk, patchline); |
431 | 461 | ||
432 | if (*patchline != '+') oldlen--; | 462 | if (*patchline != '+') TT.oldlen--; |
433 | if (*patchline != '-') newlen--; | 463 | if (*patchline != '-') TT.newlen--; |
434 | 464 | ||
435 | // Context line? | 465 | // Context line? |
436 | if (*patchline==' ' && state==2) TT.context++; | 466 | if (*patchline==' ' && state==2) TT.context++; |
437 | else state=3; | 467 | else state=3; |
438 | 468 | ||
439 | // If we've consumed all expected hunk lines, apply the hunk. | 469 | // If we've consumed all expected hunk lines, apply the hunk. |
440 | if (!oldlen && !newlen) state = apply_hunk(); | 470 | |
471 | if (!TT.oldlen && !TT.newlen) state = apply_one_hunk(); | ||
441 | continue; | 472 | continue; |
442 | } | 473 | } |
443 | fail_hunk(); | 474 | fail_hunk(); |
@@ -447,11 +478,11 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
447 | 478 | ||
448 | // Open a new file? | 479 | // Open a new file? |
449 | if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) { | 480 | if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) { |
450 | char *s, **name = &oldname; | 481 | char *s, **name = reverse ? &newname : &oldname; |
451 | int i; | 482 | int i; |
452 | 483 | ||
453 | if (*patchline == '+') { | 484 | if (*patchline == '+') { |
454 | name = &newname; | 485 | name = reverse ? &oldname : &newname; |
455 | state = 1; | 486 | state = 1; |
456 | } | 487 | } |
457 | 488 | ||
@@ -462,7 +493,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
462 | for (s = patchline+4; *s && *s!='\t'; s++) | 493 | for (s = patchline+4; *s && *s!='\t'; s++) |
463 | if (*s=='\\' && s[1]) s++; | 494 | if (*s=='\\' && s[1]) s++; |
464 | i = atoi(s); | 495 | i = atoi(s); |
465 | if (i && i<=1970) | 496 | if (i>1900 && i<=1970) |
466 | *name = xstrdup("/dev/null"); | 497 | *name = xstrdup("/dev/null"); |
467 | else { | 498 | else { |
468 | *s = 0; | 499 | *s = 0; |
@@ -478,8 +509,8 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
478 | } else if (state == 1 && !strncmp("@@ -", patchline, 4)) { | 509 | } else if (state == 1 && !strncmp("@@ -", patchline, 4)) { |
479 | int i; | 510 | int i; |
480 | 511 | ||
481 | i = sscanf(patchline+4, "%ld,%ld +%ld,%ld", | 512 | i = sscanf(patchline+4, "%ld,%ld +%ld,%ld", &TT.oldline, |
482 | &oldline, &oldlen, &newline, &newlen); | 513 | &TT.oldlen, &TT.newline, &TT.newlen); |
483 | if (i != 4) | 514 | if (i != 4) |
484 | bb_error_msg_and_die("corrupt hunk %d at %ld", TT.hunknum, TT.linenum); | 515 | bb_error_msg_and_die("corrupt hunk %d at %ld", TT.hunknum, TT.linenum); |
485 | 516 | ||
@@ -491,22 +522,22 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
491 | int oldsum, newsum, del = 0; | 522 | int oldsum, newsum, del = 0; |
492 | char *s, *name; | 523 | char *s, *name; |
493 | 524 | ||
494 | oldsum = oldline + oldlen; | 525 | oldsum = TT.oldline + TT.oldlen; |
495 | newsum = newline + newlen; | 526 | newsum = TT.newline + TT.newlen; |
496 | 527 | ||
497 | name = reverse ? oldname : newname; | 528 | name = reverse ? oldname : newname; |
498 | 529 | ||
499 | // We're deleting oldname if new file is /dev/null (before -p) | 530 | // We're deleting oldname if new file is /dev/null (before -p) |
500 | // or if new hunk is empty (zero context) after patching | 531 | // or if new hunk is empty (zero context) after patching |
501 | if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) { | 532 | if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) |
533 | { | ||
502 | name = reverse ? newname : oldname; | 534 | name = reverse ? newname : oldname; |
503 | del++; | 535 | del++; |
504 | } | 536 | } |
505 | 537 | ||
506 | // handle -p path truncation. | 538 | // handle -p path truncation. |
507 | for (i=0, s = name; *s;) { | 539 | for (i=0, s = name; *s;) { |
508 | if ((option_mask32 & FLAG_PATHLEN) && prefix == i) | 540 | if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i) break; |
509 | break; | ||
510 | if (*(s++)=='/') { | 541 | if (*(s++)=='/') { |
511 | name = s; | 542 | name = s; |
512 | i++; | 543 | i++; |
@@ -518,7 +549,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
518 | xunlink(name); | 549 | xunlink(name); |
519 | state = 0; | 550 | state = 0; |
520 | // If we've got a file to open, do so. | 551 | // If we've got a file to open, do so. |
521 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= prefix) { | 552 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { |
522 | // If the old file was null, we're creating a new one. | 553 | // If the old file was null, we're creating a new one. |
523 | if (!strcmp(oldname, "/dev/null") || !oldsum) { | 554 | if (!strcmp(oldname, "/dev/null") || !oldsum) { |
524 | printf("creating %s\n", name); | 555 | printf("creating %s\n", name); |
@@ -551,6 +582,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
551 | finish_oldfile(); | 582 | finish_oldfile(); |
552 | 583 | ||
553 | if (ENABLE_FEATURE_CLEAN_UP) { | 584 | if (ENABLE_FEATURE_CLEAN_UP) { |
585 | close(TT.filepatch); | ||
554 | free(oldname); | 586 | free(oldname); |
555 | free(newname); | 587 | free(newname); |
556 | } | 588 | } |
diff --git a/testsuite/patch.tests b/testsuite/patch.tests index 749d936ea..6ee795dba 100755 --- a/testsuite/patch.tests +++ b/testsuite/patch.tests | |||
@@ -7,7 +7,7 @@ | |||
7 | # testing "test name" "options" "expected result" "file input" "stdin" | 7 | # testing "test name" "options" "expected result" "file input" "stdin" |
8 | 8 | ||
9 | testing "patch with old_file == new_file" \ | 9 | testing "patch with old_file == new_file" \ |
10 | 'patch; echo $?; cat input' \ | 10 | 'patch 2>&1; echo $?; cat input' \ |
11 | "\ | 11 | "\ |
12 | patching file input | 12 | patching file input |
13 | 0 | 13 | 0 |
@@ -29,7 +29,7 @@ zxc | |||
29 | " \ | 29 | " \ |
30 | 30 | ||
31 | testing "patch with nonexistent old_file" \ | 31 | testing "patch with nonexistent old_file" \ |
32 | 'patch; echo $?; cat input' \ | 32 | 'patch 2>&1; echo $?; cat input' \ |
33 | "\ | 33 | "\ |
34 | patching file input | 34 | patching file input |
35 | 0 | 35 | 0 |
@@ -51,7 +51,7 @@ zxc | |||
51 | " \ | 51 | " \ |
52 | 52 | ||
53 | testing "patch -R with nonexistent old_file" \ | 53 | testing "patch -R with nonexistent old_file" \ |
54 | 'patch -R; echo $?; cat input' \ | 54 | 'patch -R 2>&1; echo $?; cat input' \ |
55 | "\ | 55 | "\ |
56 | patching file input | 56 | patching file input |
57 | 0 | 57 | 0 |
@@ -75,9 +75,12 @@ zxc | |||
75 | testing "patch detects already applied hunk" \ | 75 | testing "patch detects already applied hunk" \ |
76 | 'patch 2>&1; echo $?; cat input' \ | 76 | 'patch 2>&1; echo $?; cat input' \ |
77 | "\ | 77 | "\ |
78 | Possibly reversed hunk 1 at 2 | ||
79 | Hunk 1 FAILED 1/1. | ||
80 | abc | ||
81 | +def | ||
82 | 123 | ||
78 | patching file input | 83 | patching file input |
79 | patch: hunk #1 FAILED at 1 | ||
80 | patch: 1 out of 1 hunk FAILED | ||
81 | 1 | 84 | 1 |
82 | abc | 85 | abc |
83 | def | 86 | def |
diff --git a/testsuite/testing.sh b/testsuite/testing.sh index f907deade..c7c9ca6af 100644 --- a/testsuite/testing.sh +++ b/testsuite/testing.sh | |||
@@ -1,4 +1,4 @@ | |||
1 | # Simple test harness infrastructurei for BusyBox | 1 | # Simple test harness infrastructure for BusyBox |
2 | # | 2 | # |
3 | # Copyright 2005 by Rob Landley | 3 | # Copyright 2005 by Rob Landley |
4 | # | 4 | # |
@@ -87,6 +87,7 @@ testing() | |||
87 | 87 | ||
88 | $ECHO -ne "$3" > expected | 88 | $ECHO -ne "$3" > expected |
89 | $ECHO -ne "$4" > input | 89 | $ECHO -ne "$4" > input |
90 | [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input" | ||
90 | [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2" | 91 | [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2" |
91 | $ECHO -ne "$5" | eval "$2" > actual | 92 | $ECHO -ne "$5" | eval "$2" > actual |
92 | RETVAL=$? | 93 | RETVAL=$? |