blob: a51b7a5025a468e6b6fca029eaaba91058fe0d31 [file] [log] [blame]
Rob Landley1bbc0cd2010-08-13 15:50:26 +02001/* vi: set sw=4 ts=4:
2 *
Denys Vlasenkob82ae982010-11-21 05:53:34 +01003 * Apply a "universal" diff.
4 * Adapted from toybox's patch implementation.
Rob Landley1bbc0cd2010-08-13 15:50:26 +02005 *
6 * Copyright 2007 Rob Landley <rob@landley.net>
7 *
8 * see http://www.opengroup.org/onlinepubs/009695399/utilities/patch.html
9 * (But only does -u, because who still cares about "ed"?)
10 *
11 * TODO:
12 * -b backup
13 * -l treat all whitespace as a single space
Rob Landley1bbc0cd2010-08-13 15:50:26 +020014 * -d chdir first
15 * -D define wrap #ifdef and #ifndef around changes
16 * -o outfile output here instead of in place
17 * -r rejectfile write rejected hunks to this file
Denys Vlasenko416e9782011-06-19 01:40:31 +020018 * --dry-run (regression!)
Rob Landley1bbc0cd2010-08-13 15:50:26 +020019 *
Rob Landley1bbc0cd2010-08-13 15:50:26 +020020 * -f force (no questions asked)
21 * -F fuzz (number, default 2)
22 * [file] which file to patch
Denys Vlasenkob82ae982010-11-21 05:53:34 +010023 */
Denys Vlasenkob82ae982010-11-21 05:53:34 +010024//config:config PATCH
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020025//config: bool "patch (9.1 kb)"
Denys Vlasenkob82ae982010-11-21 05:53:34 +010026//config: default y
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Apply a unified diff formatted patch.
Rob Landley1bbc0cd2010-08-13 15:50:26 +020029
Denys Vlasenko416e9782011-06-19 01:40:31 +020030//applet:IF_PATCH(APPLET(patch, BB_DIR_USR_BIN, BB_SUID_DROP))
31
32//kbuild:lib-$(CONFIG_PATCH) += patch.o
33
Denys Vlasenkob82ae982010-11-21 05:53:34 +010034//usage:#define patch_trivial_usage
35//usage: "[OPTIONS] [ORIGFILE [PATCHFILE]]"
36//usage:#define patch_full_usage "\n\n"
37//usage: IF_LONG_OPTS(
38//usage: " -p,--strip N Strip N leading components from file names"
39//usage: "\n -i,--input DIFF Read DIFF instead of stdin"
40//usage: "\n -R,--reverse Reverse patch"
41//usage: "\n -N,--forward Ignore already applied patches"
Denys Vlasenko416e9782011-06-19 01:40:31 +020042/*usage: "\n --dry-run Don't actually change files" - TODO */
Denys Vlasenkob82ae982010-11-21 05:53:34 +010043//usage: "\n -E,--remove-empty-files Remove output files if they become empty"
44//usage: )
45//usage: IF_NOT_LONG_OPTS(
46//usage: " -p N Strip N leading components from file names"
47//usage: "\n -i DIFF Read DIFF instead of stdin"
48//usage: "\n -R Reverse patch"
49//usage: "\n -N Ignore already applied patches"
50//usage: "\n -E Remove output files if they become empty"
51//usage: )
Denys Vlasenko416e9782011-06-19 01:40:31 +020052/* -u "interpret as unified diff" is supported but not documented: this info is not useful for --help */
53/* -x "debug" is supported but does nothing */
Denys Vlasenkob82ae982010-11-21 05:53:34 +010054//usage:
55//usage:#define patch_example_usage
56//usage: "$ patch -p1 < example.diff\n"
57//usage: "$ patch -p0 -i example.diff"
Rob Landley1bbc0cd2010-08-13 15:50:26 +020058
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000059#include "libbb.h"
Glenn L McGrath655d8142003-06-22 15:32:41 +000060
Denys Vlasenko400ff222010-11-21 05:54:28 +010061
62// libbb candidate?
63
Rob Landley1bbc0cd2010-08-13 15:50:26 +020064struct double_list {
65 struct double_list *next;
66 struct double_list *prev;
67 char *data;
68};
69
Rob Landley1bbc0cd2010-08-13 15:50:26 +020070// Free all the elements of a linked list
Denys Vlasenko400ff222010-11-21 05:54:28 +010071// Call freeit() on each element before freeing it.
Rob Landleyc5f78472011-08-22 04:58:32 +020072static void dlist_free(struct double_list *list, void (*freeit)(void *data))
Glenn L McGrath655d8142003-06-22 15:32:41 +000073{
Rob Landley1bbc0cd2010-08-13 15:50:26 +020074 while (list) {
Denys Vlasenko400ff222010-11-21 05:54:28 +010075 void *pop = list;
76 list = list->next;
77 freeit(pop);
78 // Bail out also if list is circular.
79 if (list == pop) break;
Glenn L McGrath655d8142003-06-22 15:32:41 +000080 }
Glenn L McGrath655d8142003-06-22 15:32:41 +000081}
82
Denys Vlasenko400ff222010-11-21 05:54:28 +010083// Add an entry before "list" element in (circular) doubly linked list
Rob Landleyc5f78472011-08-22 04:58:32 +020084static struct double_list *dlist_add(struct double_list **list, char *data)
Rob Landley1bbc0cd2010-08-13 15:50:26 +020085{
Denys Vlasenko400ff222010-11-21 05:54:28 +010086 struct double_list *llist;
87 struct double_list *line = xmalloc(sizeof(*line));
Rob Landley1bbc0cd2010-08-13 15:50:26 +020088
89 line->data = data;
Denys Vlasenko400ff222010-11-21 05:54:28 +010090 llist = *list;
91 if (llist) {
92 struct double_list *p;
93 line->next = llist;
94 p = line->prev = llist->prev;
95 // (list is circular, we assume p is never NULL)
96 p->next = line;
97 llist->prev = line;
98 } else
99 *list = line->next = line->prev = line;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200100
101 return line;
102}
103
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200104
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200105struct globals {
106 char *infile;
107 long prefix;
108
109 struct double_list *current_hunk;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100110
Rob Landley760d0eb2010-08-13 16:40:21 +0200111 long oldline, oldlen, newline, newlen;
112 long linenum;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100113 int context, state, hunknum;
114 int filein, fileout;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200115 char *tempname;
116
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200117 int exitval;
118};
119#define TT (*ptr_to_globals)
120#define INIT_TT() do { \
121 SET_PTR_TO_GLOBALS(xzalloc(sizeof(TT))); \
122} while (0)
123
124
Lukas Huba08187352010-10-21 00:43:00 +0200125#define FLAG_STR "Rup:i:NEx"
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200126/* FLAG_REVERSE must be == 1! Code uses this fact. */
127#define FLAG_REVERSE (1 << 0)
128#define FLAG_u (1 << 1)
129#define FLAG_PATHLEN (1 << 2)
130#define FLAG_INPUT (1 << 3)
Denys Vlasenkoa4160e12010-08-16 01:33:57 +0200131#define FLAG_IGNORE (1 << 4)
Lukas Huba08187352010-10-21 00:43:00 +0200132#define FLAG_RMEMPTY (1 << 5)
Denys Vlasenko416e9782011-06-19 01:40:31 +0200133/* Enable this bit and use -x for debug output: */
134#define FLAG_DEBUG (0 << 6)
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200135
136// Dispose of a line of input, either by writing it out or discarding it.
137
138// state < 2: just free
139// state = 2: write whole line to stderr
140// state = 3: write whole line to fileout
141// state > 3: write line+1 to fileout when *line != state
142
143#define PATCH_DEBUG (option_mask32 & FLAG_DEBUG)
144
145static void do_line(void *data)
146{
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100147 struct double_list *dlist = data;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200148
149 if (TT.state>1 && *dlist->data != TT.state)
150 fdprintf(TT.state == 2 ? 2 : TT.fileout,
151 "%s\n", dlist->data+(TT.state>3 ? 1 : 0));
152
153 if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
154
155 free(dlist->data);
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100156 free(dlist);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200157}
158
159static void finish_oldfile(void)
160{
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100161 if (TT.tempname) {
162 // Copy the rest of the data and replace the original with the copy.
163 char *temp;
164
165 if (TT.filein != -1) {
166 bb_copyfd_eof(TT.filein, TT.fileout);
167 xclose(TT.filein);
168 }
169 xclose(TT.fileout);
170
171 temp = xstrdup(TT.tempname);
172 temp[strlen(temp) - 6] = '\0';
173 rename(TT.tempname, temp);
174 free(temp);
175
176 free(TT.tempname);
177 TT.tempname = NULL;
178 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200179 TT.fileout = TT.filein = -1;
180}
181
182static void fail_hunk(void)
183{
184 if (!TT.current_hunk) return;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200185
186 fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
187 TT.exitval = 1;
188
189 // If we got to this point, we've seeked to the end. Discard changes to
190 // this file and advance to next file.
191
192 TT.state = 2;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100193 TT.current_hunk->prev->next = NULL;
194 dlist_free(TT.current_hunk, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200195 TT.current_hunk = NULL;
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100196
197 // Abort the copy and delete the temporary file.
198 close(TT.filein);
199 close(TT.fileout);
200 unlink(TT.tempname);
201 free(TT.tempname);
202 TT.tempname = NULL;
203
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200204 TT.state = 0;
205}
206
207// Given a hunk of a unified diff, make the appropriate change to the file.
208// This does not use the location information, but instead treats a hunk
209// as a sort of regex. Copies data from input to output until it finds
210// the change to be made, then outputs the changed data and returns.
211// (Finding EOF first is an error.) This is a single pass operation, so
212// multiple hunks must occur in order in the file.
213
214static int apply_one_hunk(void)
215{
216 struct double_list *plist, *buf = NULL, *check;
217 int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0;
Denys Vlasenkocda81592010-08-17 01:31:40 +0200218 /* Do we try "dummy" revert to check whether
219 * to silently skip this hunk? Used to implement -N.
220 */
221 int dummy_revert = 0;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200222
223 // Break doubly linked list so we can use singly linked traversal function.
224 TT.current_hunk->prev->next = NULL;
225
226 // Match EOF if there aren't as many ending context lines as beginning
227 for (plist = TT.current_hunk; plist; plist = plist->next) {
228 if (plist->data[0]==' ') matcheof++;
229 else matcheof = 0;
230 if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
231 }
Rob Landleyc5f78472011-08-22 04:58:32 +0200232 matcheof = !matcheof || matcheof < TT.context;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200233
234 if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
235
236 // Loop through input data searching for this hunk. Match all context
237 // lines and all lines to be removed until we've found the end of a
238 // complete hunk.
239 plist = TT.current_hunk;
240 buf = NULL;
Rob Landley8027a202010-11-29 03:24:51 +0100241 if (reverse ? TT.oldlen : TT.newlen) for (;;) {
Denys Vlasenko80c5b682011-05-08 21:21:10 +0200242 char *data = xmalloc_reads(TT.filein, NULL);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200243
244 TT.linenum++;
245
246 // Figure out which line of hunk to compare with next. (Skip lines
247 // of the hunk we'd be adding.)
248 while (plist && *plist->data == "+-"[reverse]) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100249 if (data && strcmp(data, plist->data+1) == 0) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200250 if (!backwarn) {
Rob Landley9d113ca2010-10-04 00:49:48 +0200251 backwarn = TT.linenum;
Denys Vlasenkocda81592010-08-17 01:31:40 +0200252 if (option_mask32 & FLAG_IGNORE) {
253 dummy_revert = 1;
254 reverse ^= 1;
255 continue;
256 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200257 }
258 }
259 plist = plist->next;
260 }
261
262 // Is this EOF?
263 if (!data) {
264 if (PATCH_DEBUG) fdprintf(2, "INEOF\n");
265
266 // Does this hunk need to match EOF?
267 if (!plist && matcheof) break;
268
Rob Landley9d113ca2010-10-04 00:49:48 +0200269 if (backwarn)
270 fdprintf(2,"Possibly reversed hunk %d at %ld\n",
271 TT.hunknum, TT.linenum);
272
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200273 // File ended before we found a place for this hunk.
274 fail_hunk();
275 goto done;
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100276 }
277
278 if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200279 check = dlist_add(&buf, data);
280
281 // Compare this line with next expected line of hunk.
282 // todo: teach the strcmp() to ignore whitespace.
283
284 // A match can fail because the next line doesn't match, or because
285 // we hit the end of a hunk that needed EOF, and this isn't EOF.
286
287 // If match failed, flush first line of buffered data and
288 // recheck buffered data for a new match until we find one or run
289 // out of buffer.
290
291 for (;;) {
Aaro Koskinencb810c42016-11-22 00:19:51 +0200292 while (plist && *plist->data == "+-"[reverse]) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100293 if (strcmp(check->data, plist->data+1) == 0
294 && !backwarn
295 ) {
Aaro Koskinencb810c42016-11-22 00:19:51 +0200296 backwarn = TT.linenum;
297 if (option_mask32 & FLAG_IGNORE) {
298 dummy_revert = 1;
299 reverse ^= 1;
300 }
301 }
302 plist = plist->next;
303 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200304 if (!plist || strcmp(check->data, plist->data+1)) {
305 // Match failed. Write out first line of buffered data and
306 // recheck remaining buffered data for a new match.
307
308 if (PATCH_DEBUG)
Aaro Koskinen2eff5962016-11-22 00:19:50 +0200309 fdprintf(2, "NOT: %s\n", plist ? plist->data : "EOF");
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200310
311 TT.state = 3;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100312 check = buf;
313 buf = buf->next;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200314 check->prev->next = buf;
315 buf->prev = check->prev;
316 do_line(check);
317 plist = TT.current_hunk;
318
319 // If we've reached the end of the buffer without confirming a
320 // match, read more lines.
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100321 if (check == buf) {
322 buf = NULL;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200323 break;
324 }
325 check = buf;
326 } else {
327 if (PATCH_DEBUG)
328 fdprintf(2, "MAYBE: %s\n", plist->data);
329 // This line matches. Advance plist, detect successful match.
330 plist = plist->next;
331 if (!plist && !matcheof) goto out;
332 check = check->next;
333 if (check == buf) break;
334 }
335 }
336 }
337out:
338 // We have a match. Emit changed data.
Denys Vlasenkocda81592010-08-17 01:31:40 +0200339 TT.state = "-+"[reverse ^ dummy_revert];
Denys Vlasenko400ff222010-11-21 05:54:28 +0100340 dlist_free(TT.current_hunk, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200341 TT.current_hunk = NULL;
342 TT.state = 1;
343done:
344 if (buf) {
345 buf->prev->next = NULL;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100346 dlist_free(buf, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200347 }
348
349 return TT.state;
350}
351
352// Read a patch file and find hunks, opening/creating/deleting files.
353// Call apply_one_hunk() on each hunk.
354
355// state 0: Not in a hunk, look for +++.
356// state 1: Found +++ file indicator, look for @@
357// state 2: In hunk: counting initial context lines
358// state 3: In hunk: getting body
Denys Vlasenkofe8b5802015-03-11 18:01:34 +0100359// Like GNU patch, we don't require a --- line before the +++, and
360// also allow the --- after the +++ line.
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200361
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000362int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000363int patch_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath655d8142003-06-22 15:32:41 +0000364{
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200365 int opts;
366 int reverse, state = 0;
367 char *oldname = NULL, *newname = NULL;
368 char *opt_p, *opt_i;
Rob Landley8027a202010-11-29 03:24:51 +0100369 long oldlen = oldlen; /* for compiler */
370 long newlen = newlen; /* for compiler */
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000371
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200372 INIT_TT();
373
374 opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200375 argv += optind;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200376 reverse = opts & FLAG_REVERSE;
377 TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative!
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200378 TT.filein = TT.fileout = -1;
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200379 if (opts & FLAG_INPUT) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100380 xmove_fd(xopen_stdin(opt_i), STDIN_FILENO);
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200381 } else {
382 if (argv[0] && argv[1]) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100383 xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200384 }
385 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200386
387 // Loop through the lines in the patch
388 for(;;) {
389 char *patchline;
390
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100391 patchline = xmalloc_fgetline(stdin);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200392 if (!patchline) break;
393
394 // Other versions of patch accept damaged patches,
395 // so we need to also.
396 if (!*patchline) {
397 free(patchline);
398 patchline = xstrdup(" ");
399 }
400
401 // Are we assembling a hunk?
402 if (state >= 2) {
403 if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
404 dlist_add(&TT.current_hunk, patchline);
405
Rob Landley8027a202010-11-29 03:24:51 +0100406 if (*patchline != '+') oldlen--;
407 if (*patchline != '-') newlen--;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200408
409 // Context line?
410 if (*patchline==' ' && state==2) TT.context++;
411 else state=3;
412
413 // If we've consumed all expected hunk lines, apply the hunk.
414
Rob Landley8027a202010-11-29 03:24:51 +0100415 if (!oldlen && !newlen) state = apply_one_hunk();
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200416 continue;
417 }
418 fail_hunk();
419 state = 0;
420 continue;
421 }
422
423 // Open a new file?
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100424 if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200425 char *s, **name = reverse ? &newname : &oldname;
426 int i;
427
428 if (*patchline == '+') {
429 name = reverse ? &oldname : &newname;
430 state = 1;
431 }
432
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200433 finish_oldfile();
434
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200435 if (!argv[0]) {
436 free(*name);
437 // Trim date from end of filename (if any). We don't care.
438 for (s = patchline+4; *s && *s!='\t'; s++)
439 if (*s=='\\' && s[1]) s++;
440 i = atoi(s);
441 if (i>1900 && i<=1970)
442 *name = xstrdup("/dev/null");
443 else {
444 *s = 0;
445 *name = xstrdup(patchline+4);
446 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200447 }
448
449 // We defer actually opening the file because svn produces broken
450 // patches that don't signal they want to create a new file the
451 // way the patch man page says, so you have to read the first hunk
452 // and _guess_.
453
Rob Landley760d0eb2010-08-13 16:40:21 +0200454 // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
455 // but a missing ,value means the value is 1.
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100456 } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200457 int i;
Rob Landley760d0eb2010-08-13 16:40:21 +0200458 char *s = patchline+4;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200459
Rob Landley760d0eb2010-08-13 16:40:21 +0200460 // Read oldline[,oldlen] +newline[,newlen]
461
Rob Landley8027a202010-11-29 03:24:51 +0100462 TT.oldlen = oldlen = TT.newlen = newlen = 1;
Rob Landley760d0eb2010-08-13 16:40:21 +0200463 TT.oldline = strtol(s, &s, 10);
Rob Landley8027a202010-11-29 03:24:51 +0100464 if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10);
Rob Landley760d0eb2010-08-13 16:40:21 +0200465 TT.newline = strtol(s+2, &s, 10);
Rob Landley8027a202010-11-29 03:24:51 +0100466 if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10);
467
468 if (oldlen < 1 && newlen < 1)
469 bb_error_msg_and_die("Really? %s", patchline);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200470
471 TT.context = 0;
472 state = 2;
473
Denys Vlasenkofe8b5802015-03-11 18:01:34 +0100474 // If the --- line is missing or malformed, either oldname
475 // or (for -R) newname could be NULL -- but not both. Like
476 // GNU patch, proceed based on the +++ line, and avoid SEGVs.
477 if (!oldname)
478 oldname = xstrdup("MISSING_FILENAME");
479 if (!newname)
480 newname = xstrdup("MISSING_FILENAME");
481
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200482 // If this is the first hunk, open the file.
483 if (TT.filein == -1) {
Lukas Huba08187352010-10-21 00:43:00 +0200484 int oldsum, newsum, empty = 0;
Rob Landley760d0eb2010-08-13 16:40:21 +0200485 char *name;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200486
Rob Landley8027a202010-11-29 03:24:51 +0100487 oldsum = TT.oldline + oldlen;
488 newsum = TT.newline + newlen;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200489
490 name = reverse ? oldname : newname;
491
492 // We're deleting oldname if new file is /dev/null (before -p)
493 // or if new hunk is empty (zero context) after patching
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100494 if (strcmp(name, "/dev/null") == 0 || !(reverse ? oldsum : newsum)) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200495 name = reverse ? newname : oldname;
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100496 empty = 1;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200497 }
498
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100499 // Handle -p path truncation.
Rob Landley39ec6a22011-10-10 19:59:38 +0200500 for (i = 0, s = name; *s;) {
501 if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i)
502 break;
503 if (*s++ != '/')
504 continue;
505 while (*s == '/')
506 s++;
507 i++;
508 name = s;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200509 }
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100510 // If "patch FILE_TO_PATCH", completely ignore name from patch
511 if (argv[0])
512 name = argv[0];
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200513
Lukas Huba08187352010-10-21 00:43:00 +0200514 if (empty) {
515 // File is empty after the patches have been applied
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200516 state = 0;
Lukas Huba08187352010-10-21 00:43:00 +0200517 if (option_mask32 & FLAG_RMEMPTY) {
518 // If flag -E or --remove-empty-files is set
519 printf("removing %s\n", name);
520 xunlink(name);
521 } else {
522 printf("patching file %s\n", name);
523 xclose(xopen(name, O_WRONLY | O_TRUNC));
524 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200525 // If we've got a file to open, do so.
526 } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100527 struct stat statbuf;
528
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200529 // If the old file was null, we're creating a new one.
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100530 if (strcmp(oldname, "/dev/null") == 0 || !oldsum) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200531 printf("creating %s\n", name);
532 s = strrchr(name, '/');
533 if (s) {
534 *s = 0;
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100535 bb_make_directory(name, -1, FILEUTILS_RECUR);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200536 *s = '/';
537 }
Denys Vlasenkoc05387d2010-10-18 02:38:27 +0200538 TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200539 } else {
540 printf("patching file %s\n", name);
Rob Landley9d113ca2010-10-04 00:49:48 +0200541 TT.filein = xopen(name, O_RDONLY);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200542 }
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100543
544 TT.tempname = xasprintf("%sXXXXXX", name);
545 TT.fileout = xmkstemp(TT.tempname);
546 // Set permissions of output file
547 fstat(TT.filein, &statbuf);
548 fchmod(TT.fileout, statbuf.st_mode);
549
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200550 TT.linenum = 0;
551 TT.hunknum = 0;
552 }
553 }
554
555 TT.hunknum++;
556
557 continue;
558 }
559
560 // If we didn't continue above, discard this line.
561 free(patchline);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000562 }
563
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200564 finish_oldfile();
Glenn L McGrath655d8142003-06-22 15:32:41 +0000565
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200566 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200567 free(oldname);
568 free(newname);
569 }
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000570
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200571 return TT.exitval;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000572}