Mercurial > hg > toybox
annotate toys/patch.c @ 362:d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 29 Nov 2009 22:14:06 -0600 |
parents | add8b512f41a |
children | 9f4e5e15597c |
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 | 3 * patch.c - Apply a "universal" diff. |
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 | 6 * |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
219
diff
changeset
|
7 * see http://www.opengroup.org/onlinepubs/009695399/utilities/patch.html |
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 | 9 * |
10 * TODO: | |
11 * -b backup | |
12 * -l treat all whitespace as a single space | |
13 * -N ignore already applied | |
14 * -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 | |
18 * | |
19 * -E remove empty files --remove-empty-files | |
20 * -f force (no questions asked) | |
21 * -F fuzz (number, default 2) | |
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 |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
24 USE_PATCH(NEWTOY(patch, "up#i:R", TOYFLAG_USR|TOYFLAG_BIN)) |
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) |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
219
diff
changeset
|
35 -p number of '/' to strip from start of file paths (default=all) |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
219
diff
changeset
|
36 -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
|
37 -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
|
38 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
219
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 |
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
|
43 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
|
44 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
|
45 */ |
209 | 46 |
47 #include "toys.h" | |
48 | |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
49 DEFINE_GLOBALS( |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
50 char *infile; |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
51 long prefix; |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
52 |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
53 struct double_list *current_hunk; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
54 long oldline, oldlen, newline, newlen, linenum; |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
55 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
|
56 char *tempname; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
57 ) |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
58 |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
59 #define TT this.patch |
209 | 60 |
61 #define FLAG_REVERSE 1 | |
62 #define FLAG_PATHLEN 4 | |
63 | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
64 // 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
|
65 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
66 // state < 2: just free |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
67 // 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
|
68 // 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
|
69 // 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
|
70 |
209 | 71 static void do_line(void *data) |
72 { | |
73 struct double_list *dlist = (struct double_list *)data; | |
74 | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
75 if (TT.state>1 && *dlist->data != TT.state) |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
76 fdprintf(TT.state == 2 ? 2 : TT.fileout, |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
77 "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
78 |
209 | 79 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
|
80 free(data); |
209 | 81 } |
82 | |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
83 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
|
84 { |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
85 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
|
86 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
|
87 } |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
88 |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
89 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
|
90 { |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
91 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
|
92 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
|
93 |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
94 fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", 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
|
95 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
|
96 |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
97 // 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
|
98 // 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
|
99 |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
100 TT.state = 2; |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
101 llist_free(TT.current_hunk, do_line); |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 } |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
106 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
107 static int apply_hunk(void) |
209 | 108 { |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
109 struct double_list *plist, *buf = NULL, *check; |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
110 int matcheof = 0, reverse = toys.optflags & FLAG_REVERSE, backwarn = 0; |
209 | 111 |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
112 // 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
|
113 TT.current_hunk->prev->next = NULL; |
209 | 114 |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
115 // 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
|
116 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
|
117 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
|
118 else matcheof = 0; |
209 | 119 } |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
120 matcheof = matcheof < TT.context; |
209 | 121 |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
122 // 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
|
123 // 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
|
124 // complete hunk. |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
125 plist = TT.current_hunk; |
209 | 126 buf = NULL; |
216
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
127 if (TT.context) for (;;) { |
209 | 128 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
|
129 |
209 | 130 TT.linenum++; |
131 | |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
132 // 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
|
133 // 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
|
134 while (plist && *plist->data == "+-"[reverse]) { |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
135 if (data && !strcmp(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
|
136 if (!backwarn) { |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
137 fdprintf(2,"Possibly reversed hunk %d at %ld\n", |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
138 TT.hunknum, 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
|
139 backwarn++; |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
140 } |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
141 } |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
142 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
|
143 } |
209 | 144 |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
145 // 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
|
146 if (!data) { |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
147 // 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
|
148 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
|
149 |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
150 // 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
|
151 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
|
152 goto done; |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
153 } |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
154 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
|
155 |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
156 // Compare this line with next expected line of hunk. |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
157 // todo: teach the strcmp() to ignore whitespace. |
209 | 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 // 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
|
160 // 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
|
161 |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
162 // 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
|
163 // 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
|
164 // 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
|
165 |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
166 for (;;) { |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
167 if (!plist || strcmp(check->data, plist->data+1)) { |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
168 // 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
|
169 // 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
|
170 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
178 // 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
|
179 // 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
|
180 if (check==buf) { |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
181 buf = 0; |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
182 break; |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
183 } |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
184 check = buf; |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
185 } else { |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
186 // 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
|
187 plist = plist->next; |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
188 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
|
189 check = check->next; |
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
190 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
|
191 } |
209 | 192 } |
193 } | |
315
aaac01796688
Upgrade patch to detect hunks that start after a false start.
Rob Landley <rob@landley.net>
parents:
280
diff
changeset
|
194 out: |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
195 // 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
|
196 TT.state = "-+"[reverse]; |
362
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
197 llist_free(TT.current_hunk, do_line); |
d15520819347
Fix reversed patch detection logic, remove unused variables, rename variables, better comments.
Rob Landley <rob@landley.net>
parents:
351
diff
changeset
|
198 TT.current_hunk = NULL; |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
199 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
|
200 done: |
216
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
201 if (buf) { |
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
202 buf->prev->next = NULL; |
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
203 llist_free(buf, do_line); |
5697a3f7c8cf
Make patch's file add actually work, including directory creating and
Rob Landley <rob@landley.net>
parents:
214
diff
changeset
|
204 } |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
205 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
206 return TT.state; |
209 | 207 } |
208 | |
209 // state 0: Not in a hunk, look for +++. | |
210 // state 1: Found +++ file indicator, look for @@ | |
211 // state 2: In hunk: counting initial context lines | |
212 // state 3: In hunk: getting body | |
213 | |
214 void patch_main(void) | |
215 { | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
216 int reverse = toys.optflags & FLAG_REVERSE, state = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
217 char *oldname = NULL, *newname = NULL; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
218 |
209 | 219 if (TT.infile) TT.filepatch = xopen(TT.infile, O_RDONLY); |
220 TT.filein = TT.fileout = -1; | |
221 | |
222 // Loop through the lines in the patch | |
223 for(;;) { | |
224 char *patchline; | |
225 | |
226 patchline = get_line(TT.filepatch); | |
227 if (!patchline) break; | |
228 | |
351 | 229 // Other versions of patch accept damaged patches, |
230 // so we need to also. | |
231 if (!*patchline) { | |
232 free(patchline); | |
233 patchline = xstrdup(" "); | |
234 } | |
235 | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
236 // Are we assembling a hunk? |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
237 if (state >= 2) { |
209 | 238 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
|
239 dlist_add(&TT.current_hunk, patchline); |
209 | 240 |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
241 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
|
242 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
|
243 |
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
244 // Context line? |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
245 if (*patchline==' ' && state==2) TT.context++; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
246 else state=3; |
209 | 247 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
248 // 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
|
249 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
250 if (!TT.oldlen && !TT.newlen) state = apply_hunk(); |
209 | 251 continue; |
252 } | |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
253 fail_hunk(); |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
254 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
|
255 continue; |
209 | 256 } |
257 | |
258 // Open a new file? | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
263 if (*patchline == '+') { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
264 name = &newname; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
265 state = 1; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
266 } |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
267 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
268 free(*name); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
269 finish_oldfile(); |
209 | 270 |
271 // Trim date from end of filename (if any). We don't care. | |
272 for (s = patchline+4; *s && *s!='\t'; s++) | |
273 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
|
274 i = atoi(s); |
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
|
275 if (i && i<=1970) |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
276 *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
|
277 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
|
278 *s = 0; |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
279 *name = xstrdup(patchline+4); |
335
556c10abdff6
Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents:
331
diff
changeset
|
280 } |
556c10abdff6
Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents:
331
diff
changeset
|
281 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
282 // 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
|
283 // 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
|
284 // 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
|
285 // and _guess_. |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
286 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
287 // Start a new hunk? |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
288 } 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
|
289 int i; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
290 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
291 i = sscanf(patchline+4, "%ld,%ld +%ld,%ld", &TT.oldline, |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
292 &TT.oldlen, &TT.newline, &TT.newlen); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
293 if (i != 4) |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
294 error_exit("Corrupt hunk %d at %ld\n", TT.hunknum, TT.linenum); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
295 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
296 TT.context = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
297 state = 2; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
298 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
299 // 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
|
300 if (TT.filein == -1) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
301 int oldsum, newsum, del = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
302 char *s, *name; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
303 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
304 oldsum = TT.oldline + TT.oldlen; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
305 newsum = TT.newline + TT.newlen; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
306 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
307 name = reverse ? oldname : newname; |
335
556c10abdff6
Make patch work with -R when deleting files.
Rob Landley <rob@landley.net>
parents:
331
diff
changeset
|
308 |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
309 // 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
|
310 // 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
|
311 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
|
312 { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
313 name = reverse ? newname : oldname; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
314 del++; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
315 } |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
316 |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
317 // handle -p path truncation. |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
318 for (i=0, s = name; *s;) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
319 if ((toys.optflags & FLAG_PATHLEN) && TT.prefix == i) break; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
320 if (*(s++)=='/') { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
321 name = s; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
322 i++; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
323 } |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
324 } |
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 if (del) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
327 printf("removing %s\n", name); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
328 xunlink(name); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
329 state = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
330 // If we've got a file to open, do so. |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
331 } else if (!(toys.optflags & FLAG_PATHLEN) || i <= TT.prefix) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
332 // 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
|
333 if (!strcmp(oldname, "/dev/null") || !oldsum) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
334 printf("creating %s\n", name); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
335 s = strrchr(name, '/'); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
336 if (s) { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
337 *s = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
338 xmkpath(name, -1); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
339 *s = '/'; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
340 } |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
341 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
|
342 } else { |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
343 printf("patching %s\n", name); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
344 TT.filein = xopen(name, O_RDWR); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
345 } |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
346 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
|
347 TT.linenum = 0; |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
348 TT.hunknum = 0; |
209 | 349 } |
350 } | |
351 | |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
352 TT.hunknum++; |
209 | 353 |
354 continue; | |
355 } | |
356 | |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
357 // If we didn't continue above, discard this line. |
209 | 358 free(patchline); |
359 } | |
360 | |
219
cfa11e043e2b
Fix patch to use @@ values and match EOF when fewer trailing than starting
Rob Landley <rob@landley.net>
parents:
216
diff
changeset
|
361 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
|
362 |
214
98820d1eaa79
Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents:
209
diff
changeset
|
363 if (CFG_TOYBOX_FREE) { |
98820d1eaa79
Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents:
209
diff
changeset
|
364 close(TT.filepatch); |
348
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
365 free(oldname); |
c7143968cea6
Rearchitect patch to handle more darn corner cases.
Rob Landley <rob@landley.net>
parents:
335
diff
changeset
|
366 free(newname); |
214
98820d1eaa79
Upgrade patch to understand creating and deleting files.
Rob Landley <rob@landley.net>
parents:
209
diff
changeset
|
367 } |
209 | 368 } |