annotate toys/posix/patch.c @ 673:c102f31a753e

Missing line in patch -l. Oops.
author Rob Landley <rob@landley.net>
date Sun, 07 Oct 2012 23:53:25 -0500
parents e2ebaf211a0f
children 7e846e281e38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
2 *
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * patch.c - Apply a "universal" diff.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
5 * Copyright 2007 Rob Landley <rob@landley.net>
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
7 * see http://opengroup.org/onlinepubs/9699919799/utilities/patch.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
8 * (But only does -u, because who still cares about "ed"?)
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 *
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 * TODO:
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 * -b backup
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 * -l treat all whitespace as a single space
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 * -N ignore already applied
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 * -d chdir first
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 * -D define wrap #ifdef and #ifndef around changes
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 * -o outfile output here instead of in place
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 * -r rejectfile write rejected hunks to this file
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 *
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 * -E remove empty files --remove-empty-files
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 * -f force (no questions asked)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 * -F fuzz (number, default 2)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 * [file] which file to patch
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
23
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
24 USE_PATCH(NEWTOY(patch, USE_TOYBOX_DEBUG("x")"ulp#i:R", TOYFLAG_USR|TOYFLAG_BIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
25
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
26 config PATCH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
27 bool "patch"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
28 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
29 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
30 usage: patch [-i file] [-p depth] [-Ru]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
31
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
32 Apply a unified diff to one or more files.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
33
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
34 -i Input file (defaults=stdin)
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
35 -l Loose match (ignore whitespace)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
36 -p Number of '/' to strip from start of file paths (default=all)
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
37 -R Reverse patch.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
38 -u Ignored (only handles "unified" diffs)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
39
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
40 This version of patch only handles unified diffs, and only modifies
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
41 a file when all all hunks to that file apply. Patch prints failed
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
42 hunks to stderr, and exits with nonzero status if any hunks fail.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
43
325
5d0fbdb2fc86 The epoch can also show up as 1970-01-01 (depending on timezone), so treat any year >0 and <= 1970 as meaning "file does not exist".
Rob Landley <rob@landley.net>
parents: 315
diff changeset
44 A file compared against /dev/null (or with a date <= the epoch) is
280
d1a548bbd0dd Teach patch that a file dated 1969-12-31 means doesn't exist, and to fail
Rob Landley <rob@landley.net>
parents: 238
diff changeset
45 created/deleted as appropriate.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 219
diff changeset
46 */
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 #include "toys.h"
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
50 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
51 char *infile;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
52 long prefix;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
53
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
54 struct double_list *current_hunk;
377
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
55 long oldline, oldlen, newline, newlen;
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
56 long linenum;
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
57 int context, state, filein, fileout, filepatch, hunknum;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
58 char *tempname;
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
59 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
60
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
61 #define TT this.patch
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
63 #define FLAG_R (1<<0)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
64 #define FLAG_i (1<<1)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
65 #define FLAG_l (1<<2)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
66 #define FLAG_p (1<<3)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
67 #define FLAG_u (1<<4)
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
68
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
69 // Dispose of a line of input, either by writing it out or discarding it.
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
70
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
71 // state < 2: just free
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
72 // state = 2: write whole line to stderr
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
73 // state = 3: write whole line to fileout
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
74 // state > 3: write line+1 to fileout when *line != state
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
75
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
76 #define PATCH_DEBUG (CFG_TOYBOX_DEBUG && (toys.optflags & 32))
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
77
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 static void do_line(void *data)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 {
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 struct double_list *dlist = (struct double_list *)data;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
81
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
82 if (TT.state>1 && *dlist->data != TT.state) {
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
83 char *s = dlist->data+(TT.state>3 ? 1 : 0);
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
84 int i = TT.state == 2 ? 2 : TT.fileout;
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
85
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
86 xwrite(i, s, strlen(s));
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
87 xwrite(i, "\n", 1);
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
88 }
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
89
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
90 if (PATCH_DEBUG) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
91
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 free(dlist->data);
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
93 free(data);
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
96 static void finish_oldfile(void)
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
97 {
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
98 if (TT.tempname) replace_tempfile(TT.filein, TT.fileout, &TT.tempname);
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
99 TT.fileout = TT.filein = -1;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
100 }
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
101
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
102 static void fail_hunk(void)
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
103 {
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
104 if (!TT.current_hunk) return;
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
105 TT.current_hunk->prev->next = 0;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
106
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
107 fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n",
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
108 TT.hunknum, TT.oldline, TT.newline);
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
109 toys.exitval = 1;
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
110
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
111 // If we got to this point, we've seeked to the end. Discard changes to
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
112 // this file and advance to next file.
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
113
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
114 TT.state = 2;
624
1e8b9acdafeb Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents: 550
diff changeset
115 llist_traverse(TT.current_hunk, do_line);
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
116 TT.current_hunk = NULL;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
117 delete_tempfile(TT.filein, TT.fileout, &TT.tempname);
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
118 TT.state = 0;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
119 }
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
120
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
121 // Compare ignoring whitespace. Just returns
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
122 static int loosecmp(char *aa, char *bb)
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
123 {
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
124 int a = 0, b = 0;
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
125
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
126 for (;;) {
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
127 while (isspace(aa[a])) a++;
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
128 while (isspace(bb[b])) b++;
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
129 if (aa[a] != bb[b]) return 1;
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
130 if (!aa[a]) return 0;
673
c102f31a753e Missing line in patch -l. Oops.
Rob Landley <rob@landley.net>
parents: 670
diff changeset
131 a++, b++;
c102f31a753e Missing line in patch -l. Oops.
Rob Landley <rob@landley.net>
parents: 670
diff changeset
132 }
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
133 }
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
134
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
135 // Given a hunk of a unified diff, make the appropriate change to the file.
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
136 // This does not use the location information, but instead treats a hunk
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
137 // as a sort of regex. Copies data from input to output until it finds
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
138 // the change to be made, then outputs the changed data and returns.
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
139 // (Finding EOF first is an error.) This is a single pass operation, so
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
140 // multiple hunks must occur in order in the file.
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
141
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
142 static int apply_one_hunk(void)
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 {
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
144 struct double_list *plist, *buf = NULL, *check;
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
145 int matcheof = 0, reverse = toys.optflags & FLAG_R, backwarn = 0;
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
146 int (*lcmp)(char *aa, char *bb);
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
147
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
148 lcmp = (toys.optflags & FLAG_l) ? (void *)loosecmp : (void *)strcmp;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
149
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
150 // Break doubly linked list so we can use singly linked traversal function.
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
151 TT.current_hunk->prev->next = NULL;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
152
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
153 // Match EOF if there aren't as many ending context lines as beginning
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
154 for (plist = TT.current_hunk; plist; plist = plist->next) {
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
155 if (plist->data[0]==' ') matcheof++;
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
156 else matcheof = 0;
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
157 if (PATCH_DEBUG) fprintf(stderr, "HUNK:%s\n", plist->data);
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 }
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
159 matcheof = matcheof < TT.context;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
160
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
161 if (PATCH_DEBUG) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
162
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
163 // Loop through input data searching for this hunk. Match all context
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
164 // lines and all lines to be removed until we've found the end of a
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
165 // complete hunk.
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
166 plist = TT.current_hunk;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 buf = NULL;
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
168 if (TT.context) for (;;) {
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 char *data = get_line(TT.filein);
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
170
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 TT.linenum++;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
172
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
173 // Figure out which line of hunk to compare with next. (Skip lines
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
174 // of the hunk we'd be adding.)
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
175 while (plist && *plist->data == "+-"[reverse]) {
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
176 if (data && !lcmp(data, plist->data+1)) {
391
56d07d82e691 Don't complain about "possibly reversed" hunks that merely moved later in the file, just give a potential reason if it failed to apply.
Rob Landley <rob@landley.net>
parents: 377
diff changeset
177 if (!backwarn) backwarn = TT.linenum;
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
178 }
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
179 plist = plist->next;
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
180 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
181
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
182 // Is this EOF?
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
183 if (!data) {
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
184 if (PATCH_DEBUG) fprintf(stderr, "INEOF\n");
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
185
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
186 // Does this hunk need to match EOF?
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
187 if (!plist && matcheof) break;
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
188
391
56d07d82e691 Don't complain about "possibly reversed" hunks that merely moved later in the file, just give a potential reason if it failed to apply.
Rob Landley <rob@landley.net>
parents: 377
diff changeset
189 if (backwarn)
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
190 fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
391
56d07d82e691 Don't complain about "possibly reversed" hunks that merely moved later in the file, just give a potential reason if it failed to apply.
Rob Landley <rob@landley.net>
parents: 377
diff changeset
191 TT.hunknum, TT.linenum);
56d07d82e691 Don't complain about "possibly reversed" hunks that merely moved later in the file, just give a potential reason if it failed to apply.
Rob Landley <rob@landley.net>
parents: 377
diff changeset
192
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
193 // File ended before we found a place for this hunk.
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
194 fail_hunk();
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
195 goto done;
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
196 } else if (PATCH_DEBUG) fprintf(stderr, "IN: %s\n", data);
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
197 check = dlist_add(&buf, data);
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
198
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
199 // Compare this line with next expected line of hunk.
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
200
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
201 // A match can fail because the next line doesn't match, or because
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
202 // we hit the end of a hunk that needed EOF, and this isn't EOF.
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
203
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
204 // If match failed, flush first line of buffered data and
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
205 // recheck buffered data for a new match until we find one or run
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
206 // out of buffer.
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
207
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
208 for (;;) {
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
209 if (!plist || lcmp(check->data, plist->data+1)) {
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
210 // Match failed. Write out first line of buffered data and
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
211 // recheck remaining buffered data for a new match.
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
212
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
213 if (PATCH_DEBUG)
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
214 fprintf(stderr, "NOT: %s\n", plist->data);
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
215
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
216 TT.state = 3;
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
217 check = llist_pop(&buf);
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
218 check->prev->next = buf;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
219 buf->prev = check->prev;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
220 do_line(check);
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
221 plist = TT.current_hunk;
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
222
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
223 // If we've reached the end of the buffer without confirming a
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
224 // match, read more lines.
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
225 if (check==buf) {
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
226 buf = 0;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
227 break;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
228 }
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
229 check = buf;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
230 } else {
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
231 if (PATCH_DEBUG)
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
232 fprintf(stderr, "MAYBE: %s\n", plist->data);
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
233 // This line matches. Advance plist, detect successful match.
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
234 plist = plist->next;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
235 if (!plist && !matcheof) goto out;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
236 check = check->next;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
237 if (check == buf) break;
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
238 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 }
315
aaac01796688 Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents: 280
diff changeset
241 out:
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
242 // We have a match. Emit changed data.
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
243 TT.state = "-+"[reverse];
624
1e8b9acdafeb Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents: 550
diff changeset
244 llist_traverse(TT.current_hunk, do_line);
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
245 TT.current_hunk = NULL;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
246 TT.state = 1;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
247 done:
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
248 if (buf) {
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
249 buf->prev->next = NULL;
624
1e8b9acdafeb Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents: 550
diff changeset
250 llist_traverse(buf, do_line);
216
5697a3f7c8cf Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents: 214
diff changeset
251 }
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
252
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
253 return TT.state;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
255
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
256 // Read a patch file and find hunks, opening/creating/deleting files.
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
257 // Call apply_one_hunk() on each hunk.
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
258
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 // state 0: Not in a hunk, look for +++.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 // state 1: Found +++ file indicator, look for @@
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 // state 2: In hunk: counting initial context lines
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 // state 3: In hunk: getting body
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
263
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 void patch_main(void)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 {
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
266 int reverse = toys.optflags&FLAG_R, state = 0, patchlinenum = 0,
412
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
267 strip = 0;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
268 char *oldname = NULL, *newname = NULL;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
269
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 if (TT.infile) TT.filepatch = xopen(TT.infile, O_RDONLY);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 TT.filein = TT.fileout = -1;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
272
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 // Loop through the lines in the patch
412
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
274 for (;;) {
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 char *patchline;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
276
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 patchline = get_line(TT.filepatch);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 if (!patchline) break;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
279
351
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
280 // Other versions of patch accept damaged patches,
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
281 // so we need to also.
412
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
282 if (strip || !patchlinenum++) {
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
283 int len = strlen(patchline);
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
284 if (patchline[len-1] == '\r') {
550
b2194045c40e Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
Rob Landley <rob@landley.net>
parents: 500
diff changeset
285 if (!strip) fprintf(stderr, "Removing DOS newlines\n");
412
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
286 strip = 1;
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
287 patchline[len-1]=0;
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
288 }
2b521c791e4e Patch shouldn't be bothered by DOS newlines.
Rob Landley <rob@landley.net>
parents: 391
diff changeset
289 }
351
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
290 if (!*patchline) {
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
291 free(patchline);
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
292 patchline = xstrdup(" ");
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
293 }
add8b512f41a Another patch corner case.
Rob Landley <rob@landley.net>
parents: 348
diff changeset
294
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
295 // Are we assembling a hunk?
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
296 if (state >= 2) {
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
362
d15520819347 Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents: 351
diff changeset
298 dlist_add(&TT.current_hunk, patchline);
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
299
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
300 if (*patchline != '+') TT.oldlen--;
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
301 if (*patchline != '-') TT.newlen--;
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
302
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
303 // Context line?
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
304 if (*patchline==' ' && state==2) TT.context++;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
305 else state=3;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
306
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
307 // If we've consumed all expected hunk lines, apply the hunk.
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
308
376
35c8beb54800 For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
Rob Landley <rob@landley.net>
parents: 375
diff changeset
309 if (!TT.oldlen && !TT.newlen) state = apply_one_hunk();
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 continue;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 }
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
312 fail_hunk();
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
313 state = 0;
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
314 continue;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
316
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 // Open a new file?
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
318 if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
319 char *s, **name = &oldname;
325
5d0fbdb2fc86 The epoch can also show up as 1970-01-01 (depending on timezone), so treat any year >0 and <= 1970 as meaning "file does not exist".
Rob Landley <rob@landley.net>
parents: 315
diff changeset
320 int i;
5d0fbdb2fc86 The epoch can also show up as 1970-01-01 (depending on timezone), so treat any year >0 and <= 1970 as meaning "file does not exist".
Rob Landley <rob@landley.net>
parents: 315
diff changeset
321
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
322 if (*patchline == '+') {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
323 name = &newname;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
324 state = 1;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
325 }
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
326
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
327 free(*name);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
328 finish_oldfile();
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
329
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 // Trim date from end of filename (if any). We don't care.
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 for (s = patchline+4; *s && *s!='\t'; s++)
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 if (*s=='\\' && s[1]) s++;
325
5d0fbdb2fc86 The epoch can also show up as 1970-01-01 (depending on timezone), so treat any year >0 and <= 1970 as meaning "file does not exist".
Rob Landley <rob@landley.net>
parents: 315
diff changeset
333 i = atoi(s);
375
9f4e5e15597c One more little tweak to patch: unrecognized date format != delete file.
Rob Landley <rob@landley.net>
parents: 362
diff changeset
334 if (i>1900 && i<=1970)
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
335 *name = xstrdup("/dev/null");
280
d1a548bbd0dd Teach patch that a file dated 1969-12-31 means doesn't exist, and to fail
Rob Landley <rob@landley.net>
parents: 238
diff changeset
336 else {
d1a548bbd0dd Teach patch that a file dated 1969-12-31 means doesn't exist, and to fail
Rob Landley <rob@landley.net>
parents: 238
diff changeset
337 *s = 0;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
338 *name = xstrdup(patchline+4);
335
556c10abdff6 Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents: 331
diff changeset
339 }
556c10abdff6 Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents: 331
diff changeset
340
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
341 // We defer actually opening the file because svn produces broken
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
342 // patches that don't signal they want to create a new file the
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
343 // way the patch man page says, so you have to read the first hunk
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
344 // and _guess_.
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
345
377
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
346 // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
347 // but a missing ,value means the value is 1.
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
348 } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
349 int i;
377
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
350 char *s = patchline+4;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
351
377
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
352 // Read oldline[,oldlen] +newline[,newlen]
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
353
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
354 TT.oldlen = TT.newlen = 1;
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
355 TT.oldline = strtol(s, &s, 10);
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
356 if (*s == ',') TT.oldlen=strtol(s+1, &s, 10);
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
357 TT.newline = strtol(s+2, &s, 10);
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
358 if (*s == ',') TT.newlen = strtol(s+1, &s, 10);
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
359
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
360 TT.context = 0;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
361 state = 2;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
362
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
363 // If this is the first hunk, open the file.
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
364 if (TT.filein == -1) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
365 int oldsum, newsum, del = 0;
377
15ca7e49b6ff The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
Rob Landley <rob@landley.net>
parents: 376
diff changeset
366 char *name;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
367
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
368 oldsum = TT.oldline + TT.oldlen;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
369 newsum = TT.newline + TT.newlen;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
370
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
371 name = reverse ? oldname : newname;
335
556c10abdff6 Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents: 331
diff changeset
372
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
373 // We're deleting oldname if new file is /dev/null (before -p)
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
374 // or if new hunk is empty (zero context) after patching
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
375 if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum))
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
376 {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
377 name = reverse ? newname : oldname;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
378 del++;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
379 }
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
380
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
381 // handle -p path truncation.
500
cd70270dbc51 Teach patch -p that consecutive /// counts as one path component.
Rob Landley <rob@landley.net>
parents: 412
diff changeset
382 for (i = 0, s = name; *s;) {
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
383 if ((toys.optflags & FLAG_p) && TT.prefix == i) break;
500
cd70270dbc51 Teach patch -p that consecutive /// counts as one path component.
Rob Landley <rob@landley.net>
parents: 412
diff changeset
384 if (*s++ != '/') continue;
cd70270dbc51 Teach patch -p that consecutive /// counts as one path component.
Rob Landley <rob@landley.net>
parents: 412
diff changeset
385 while (*s == '/') s++;
cd70270dbc51 Teach patch -p that consecutive /// counts as one path component.
Rob Landley <rob@landley.net>
parents: 412
diff changeset
386 name = s;
cd70270dbc51 Teach patch -p that consecutive /// counts as one path component.
Rob Landley <rob@landley.net>
parents: 412
diff changeset
387 i++;
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
388 }
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
389
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
390 if (del) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
391 printf("removing %s\n", name);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
392 xunlink(name);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
393 state = 0;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
394 // If we've got a file to open, do so.
670
e2ebaf211a0f patch -l (loose match)
Rob Landley <rob@landley.net>
parents: 656
diff changeset
395 } else if (!(toys.optflags & FLAG_p) || i <= TT.prefix) {
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
396 // If the old file was null, we're creating a new one.
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
397 if (!strcmp(oldname, "/dev/null") || !oldsum) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
398 printf("creating %s\n", name);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
399 s = strrchr(name, '/');
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
400 if (s) {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
401 *s = 0;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
402 xmkpath(name, -1);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
403 *s = '/';
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
404 }
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
405 TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
406 } else {
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
407 printf("patching %s\n", name);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
408 TT.filein = xopen(name, O_RDWR);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
409 }
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
410 TT.fileout = copy_tempfile(TT.filein, name, &TT.tempname);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
411 TT.linenum = 0;
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
412 TT.hunknum = 0;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
413 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
415
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
416 TT.hunknum++;
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
417
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 continue;
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
419 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
420
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
421 // If we didn't continue above, discard this line.
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
422 free(patchline);
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
423 }
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
424
219
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
425 finish_oldfile();
cfa11e043e2b Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents: 216
diff changeset
426
214
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
427 if (CFG_TOYBOX_FREE) {
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
428 close(TT.filepatch);
348
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
429 free(oldname);
c7143968cea6 Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents: 335
diff changeset
430 free(newname);
214
98820d1eaa79 Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents: 209
diff changeset
431 }
209
9a0d4e8a9c61 Patch command.
Rob Landley <rob@landley.net>
parents:
diff changeset
432 }