| @@ -1,521 +1,521 @@ | | | @@ -1,521 +1,521 @@ |
1 | /* | | 1 | /* |
2 | * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $ | | 2 | * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $ |
3 | * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $ | | 3 | * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $ |
4 | * $NetBSD: inp.c,v 1.27 2021/05/25 11:25:59 cjep Exp $ | | 4 | * $NetBSD: inp.c,v 1.28 2021/05/26 07:57:05 cjep Exp $ |
5 | */ | | 5 | */ |
6 | | | 6 | |
7 | /* | | 7 | /* |
8 | * patch - a program to apply diffs to original files | | 8 | * patch - a program to apply diffs to original files |
9 | * | | 9 | * |
10 | * Copyright 1986, Larry Wall | | 10 | * Copyright 1986, Larry Wall |
11 | * | | 11 | * |
12 | * Redistribution and use in source and binary forms, with or without | | 12 | * Redistribution and use in source and binary forms, with or without |
13 | * modification, are permitted provided that the following condition is met: | | 13 | * modification, are permitted provided that the following condition is met: |
14 | * 1. Redistributions of source code must retain the above copyright notice, | | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
15 | * this condition and the following disclaimer. | | 15 | * this condition and the following disclaimer. |
16 | * | | 16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY | | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | | 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR | | 20 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR |
21 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 21 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 | * SUCH DAMAGE. | | 27 | * SUCH DAMAGE. |
28 | * | | 28 | * |
29 | * -C option added in 1998, original code by Marc Espie, based on FreeBSD | | 29 | * -C option added in 1998, original code by Marc Espie, based on FreeBSD |
30 | * behaviour | | 30 | * behaviour |
31 | */ | | 31 | */ |
32 | | | 32 | |
33 | #include <sys/cdefs.h> | | 33 | #include <sys/cdefs.h> |
34 | __RCSID("$NetBSD: inp.c,v 1.27 2021/05/25 11:25:59 cjep Exp $"); | | 34 | __RCSID("$NetBSD: inp.c,v 1.28 2021/05/26 07:57:05 cjep Exp $"); |
35 | | | 35 | |
36 | #include <sys/types.h> | | 36 | #include <sys/types.h> |
37 | #include <sys/file.h> | | 37 | #include <sys/file.h> |
38 | #include <sys/stat.h> | | 38 | #include <sys/stat.h> |
39 | #include <sys/mman.h> | | 39 | #include <sys/mman.h> |
40 | #include <sys/wait.h> | | 40 | #include <sys/wait.h> |
41 | | | 41 | |
42 | #include <ctype.h> | | 42 | #include <ctype.h> |
43 | #include <errno.h> | | 43 | #include <errno.h> |
44 | #include <fcntl.h> | | 44 | #include <fcntl.h> |
45 | #include <libgen.h> | | 45 | #include <libgen.h> |
46 | #include <limits.h> | | 46 | #include <limits.h> |
47 | #include <stddef.h> | | 47 | #include <stddef.h> |
48 | #include <stdio.h> | | 48 | #include <stdio.h> |
49 | #include <stdlib.h> | | 49 | #include <stdlib.h> |
50 | #include <string.h> | | 50 | #include <string.h> |
51 | #include <unistd.h> | | 51 | #include <unistd.h> |
52 | | | 52 | |
53 | #include "common.h" | | 53 | #include "common.h" |
54 | #include "util.h" | | 54 | #include "util.h" |
55 | #include "pch.h" | | 55 | #include "pch.h" |
56 | #include "inp.h" | | 56 | #include "inp.h" |
57 | | | 57 | |
58 | | | 58 | |
59 | /* Input-file-with-indexable-lines abstract type */ | | 59 | /* Input-file-with-indexable-lines abstract type */ |
60 | | | 60 | |
61 | static off_t i_size; /* size of the input file */ | | 61 | static off_t i_size; /* size of the input file */ |
62 | static char *i_womp; /* plan a buffer for entire file */ | | 62 | static char *i_womp; /* plan a buffer for entire file */ |
63 | static char **i_ptr; /* pointers to lines in i_womp */ | | 63 | static char **i_ptr; /* pointers to lines in i_womp */ |
64 | static char empty_line[] = { '\0' }; | | 64 | static char empty_line[] = { '\0' }; |
65 | | | 65 | |
66 | static int tifd = -1; /* plan b virtual string array */ | | 66 | static int tifd = -1; /* plan b virtual string array */ |
67 | static char *tibuf[2]; /* plan b buffers */ | | 67 | static char *tibuf[2]; /* plan b buffers */ |
68 | static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */ | | 68 | static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */ |
69 | static LINENUM lines_per_buf; /* how many lines per buffer */ | | 69 | static LINENUM lines_per_buf; /* how many lines per buffer */ |
70 | static int tireclen; /* length of records in tmp file */ | | 70 | static int tireclen; /* length of records in tmp file */ |
71 | | | 71 | |
72 | static bool rev_in_string(const char *); | | 72 | static bool rev_in_string(const char *); |
73 | static bool reallocate_lines(size_t *); | | 73 | static bool reallocate_lines(size_t *); |
74 | | | 74 | |
75 | /* returns false if insufficient memory */ | | 75 | /* returns false if insufficient memory */ |
76 | static bool plan_a(const char *); | | 76 | static bool plan_a(const char *); |
77 | | | 77 | |
78 | static void plan_b(const char *); | | 78 | static void plan_b(const char *); |
79 | | | 79 | |
80 | /* New patch--prepare to edit another file. */ | | 80 | /* New patch--prepare to edit another file. */ |
81 | | | 81 | |
82 | void | | 82 | void |
83 | re_input(void) | | 83 | re_input(void) |
84 | { | | 84 | { |
85 | if (using_plan_a) { | | 85 | if (using_plan_a) { |
86 | i_size = 0; | | 86 | i_size = 0; |
87 | free(i_ptr); | | 87 | free(i_ptr); |
88 | i_ptr = NULL; | | 88 | i_ptr = NULL; |
89 | if (i_womp != NULL) { | | 89 | if (i_womp != NULL) { |
90 | munmap(i_womp, i_size); | | 90 | munmap(i_womp, i_size); |
91 | i_womp = NULL; | | 91 | i_womp = NULL; |
92 | } | | 92 | } |
93 | } else { | | 93 | } else { |
94 | using_plan_a = true; /* maybe the next one is smaller */ | | 94 | using_plan_a = true; /* maybe the next one is smaller */ |
95 | close(tifd); | | 95 | close(tifd); |
96 | tifd = -1; | | 96 | tifd = -1; |
97 | free(tibuf[0]); | | 97 | free(tibuf[0]); |
98 | free(tibuf[1]); | | 98 | free(tibuf[1]); |
99 | tibuf[0] = tibuf[1] = NULL; | | 99 | tibuf[0] = tibuf[1] = NULL; |
100 | tiline[0] = tiline[1] = -1; | | 100 | tiline[0] = tiline[1] = -1; |
101 | tireclen = 0; | | 101 | tireclen = 0; |
102 | } | | 102 | } |
103 | } | | 103 | } |
104 | | | 104 | |
105 | /* Construct the line index, somehow or other. */ | | 105 | /* Construct the line index, somehow or other. */ |
106 | | | 106 | |
107 | void | | 107 | void |
108 | scan_input(const char *filename) | | 108 | scan_input(const char *filename) |
109 | { | | 109 | { |
110 | if (!plan_a(filename)) | | 110 | if (!plan_a(filename)) |
111 | plan_b(filename); | | 111 | plan_b(filename); |
112 | if (verbose) { | | 112 | if (verbose) { |
113 | say("Patching file %s using Plan %s...\n", filename, | | 113 | say("Patching file %s using Plan %s...\n", filename, |
114 | (using_plan_a ? "A" : "B")); | | 114 | (using_plan_a ? "A" : "B")); |
115 | } | | 115 | } |
116 | } | | 116 | } |
117 | | | 117 | |
118 | static bool | | 118 | static bool |
119 | reallocate_lines(size_t *lines_allocated) | | 119 | reallocate_lines(size_t *lines_allocated) |
120 | { | | 120 | { |
121 | char **p; | | 121 | char **p; |
122 | size_t new_size; | | 122 | size_t new_size; |
123 | | | 123 | |
124 | new_size = *lines_allocated * 3 / 2; | | 124 | new_size = *lines_allocated * 3 / 2; |
125 | p = pch_realloc(i_ptr, new_size + 2, sizeof(char *)); | | 125 | p = pch_realloc(i_ptr, new_size + 2, sizeof(char *)); |
126 | if (p == NULL) { /* shucks, it was a near thing */ | | 126 | if (p == NULL) { /* shucks, it was a near thing */ |
127 | munmap(i_womp, i_size); | | 127 | munmap(i_womp, i_size); |
128 | i_womp = NULL; | | 128 | i_womp = NULL; |
129 | free(i_ptr); | | 129 | free(i_ptr); |
130 | i_ptr = NULL; | | 130 | i_ptr = NULL; |
131 | *lines_allocated = 0; | | 131 | *lines_allocated = 0; |
132 | return false; | | 132 | return false; |
133 | } | | 133 | } |
134 | *lines_allocated = new_size; | | 134 | *lines_allocated = new_size; |
135 | i_ptr = p; | | 135 | i_ptr = p; |
136 | return true; | | 136 | return true; |
137 | } | | 137 | } |
138 | | | 138 | |
139 | /* Try keeping everything in memory. */ | | 139 | /* Try keeping everything in memory. */ |
140 | | | 140 | |
141 | static bool | | 141 | static bool |
142 | plan_a(const char *filename) | | 142 | plan_a(const char *filename) |
143 | { | | 143 | { |
144 | int ifd, statfailed, devnull, pstat; | | 144 | int ifd, statfailed, devnull, pstat; |
145 | char *p, *s, *lbuf; | | 145 | char *p, *s, *lbuf; |
146 | struct stat filestat; | | 146 | struct stat filestat; |
147 | off_t i; | | 147 | off_t i; |
148 | ptrdiff_t sz; | | 148 | ptrdiff_t sz; |
149 | size_t iline, lines_allocated, lbufsz; | | 149 | size_t iline, lines_allocated, lbufsz; |
150 | pid_t pid; | | 150 | pid_t pid; |
151 | char *argp[4] = {NULL}; | | 151 | char *argp[4] = {NULL}; |
152 | | | 152 | |
153 | #ifdef DEBUGGING | | 153 | #ifdef DEBUGGING |
154 | if (debug & 8) | | 154 | if (debug & 8) |
155 | return false; | | 155 | return false; |
156 | #endif | | 156 | #endif |
157 | | | 157 | |
158 | if (filename == NULL || *filename == '\0') | | 158 | if (filename == NULL || *filename == '\0') |
159 | return false; | | 159 | return false; |
160 | | | 160 | |
161 | statfailed = stat(filename, &filestat); | | 161 | statfailed = stat(filename, &filestat); |
162 | if (statfailed && ok_to_create_file) { | | 162 | if (statfailed && ok_to_create_file) { |
163 | if (verbose) | | 163 | if (verbose) |
164 | say("(Creating file %s...)\n", filename); | | 164 | say("(Creating file %s...)\n", filename); |
165 | | | 165 | |
166 | /* | | 166 | /* |
167 | * in check_patch case, we still display `Creating file' even | | 167 | * in check_patch case, we still display `Creating file' even |
168 | * though we're not. The rule is that -C should be as similar | | 168 | * though we're not. The rule is that -C should be as similar |
169 | * to normal patch behavior as possible | | 169 | * to normal patch behavior as possible |
170 | */ | | 170 | */ |
171 | if (check_only) | | 171 | if (check_only) |
172 | return true; | | 172 | return true; |
173 | makedirs(filename, true); | | 173 | makedirs(filename, true); |
174 | close(creat(filename, 0666)); | | 174 | close(creat(filename, 0666)); |
175 | statfailed = stat(filename, &filestat); | | 175 | statfailed = stat(filename, &filestat); |
176 | } | | 176 | } |
177 | if (statfailed && check_only) | | 177 | if (statfailed && check_only) |
178 | fatal("%s not found, -C mode, can't probe further\n", filename); | | 178 | fatal("%s not found, -C mode, can't probe further\n", filename); |
179 | /* For nonexistent or read-only files, look for RCS versions. */ | | 179 | /* For nonexistent or read-only files, look for RCS versions. */ |
180 | if (statfailed || | | 180 | if (statfailed || |
181 | /* No one can write to it. */ | | 181 | /* No one can write to it. */ |
182 | (filestat.st_mode & 0222) == 0 || | | 182 | (filestat.st_mode & 0222) == 0 || |
183 | /* I can't write to it. */ | | 183 | /* I can't write to it. */ |
184 | ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) { | | 184 | ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) { |
185 | char *filebase, *filedir; | | 185 | char *filebase, *filedir; |
186 | struct stat cstat; | | 186 | struct stat cstat; |
187 | char *tmp_filename1, *tmp_filename2; | | 187 | char *tmp_filename1, *tmp_filename2; |
188 | | | 188 | |
189 | tmp_filename1 = strdup(filename); | | 189 | tmp_filename1 = strdup(filename); |
190 | tmp_filename2 = strdup(filename); | | 190 | tmp_filename2 = strdup(filename); |
191 | if (tmp_filename1 == NULL || tmp_filename2 == NULL) | | 191 | if (tmp_filename1 == NULL || tmp_filename2 == NULL) |
192 | fatal("strdupping filename"); | | 192 | fatal("strdupping filename"); |
193 | | | 193 | |
194 | filebase = basename(tmp_filename1); | | 194 | filebase = basename(tmp_filename1); |
195 | filedir = dirname(tmp_filename2); | | 195 | filedir = dirname(tmp_filename2); |
196 | | | 196 | |
197 | lbufsz = INITLINELEN; | | 197 | lbufsz = INITLINELEN; |
198 | if ((lbuf = malloc(bufsz)) == NULL) | | 198 | if ((lbuf = malloc(bufsz)) == NULL) |
199 | pfatal("allocating line buffer"); | | 199 | pfatal("allocating line buffer"); |
200 | lbuf[0] = '\0'; | | 200 | lbuf[0] = '\0'; |
201 | | | 201 | |
202 | #define try(f, a1, a2, a3) \ | | 202 | #define try(f, a1, a2, a3) \ |
203 | (snprintf(lbuf, lbufsz, f, a1, a2, a3), stat(lbuf, &cstat) == 0) | | 203 | (snprintf(lbuf, lbufsz, f, a1, a2, a3), stat(lbuf, &cstat) == 0) |
204 | | | 204 | |
205 | /* | | 205 | /* |
206 | * else we can't write to it but it's not under a version | | 206 | * else we can't write to it but it's not under a version |
207 | * control system, so just proceed. | | 207 | * control system, so just proceed. |
208 | */ | | 208 | */ |
209 | if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || | | 209 | if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || |
210 | try("%s/RCS/%s%s", filedir, filebase, "") || | | 210 | try("%s/RCS/%s%s", filedir, filebase, "") || |
211 | try("%s/%s%s", filedir, filebase, RCSSUFFIX)) { | | 211 | try("%s/%s%s", filedir, filebase, RCSSUFFIX)) { |
212 | if (!statfailed) { | | 212 | if (!statfailed) { |
213 | if ((filestat.st_mode & 0222) != 0) | | 213 | if ((filestat.st_mode & 0222) != 0) |
214 | /* The owner can write to it. */ | | 214 | /* The owner can write to it. */ |
215 | fatal("file %s seems to be locked " | | 215 | fatal("file %s seems to be locked " |
216 | "by somebody else under RCS\n", | | 216 | "by somebody else under RCS\n", |
217 | filename); | | 217 | filename); |
218 | /* | | 218 | /* |
219 | * It might be checked out unlocked. See if | | 219 | * It might be checked out unlocked. See if |
220 | * it's safe to check out the default version | | 220 | * it's safe to check out the default version |
221 | * locked. | | 221 | * locked. |
222 | */ | | 222 | */ |
223 | if (verbose) | | 223 | if (verbose) |
224 | say("Comparing file %s to default " | | 224 | say("Comparing file %s to default " |
225 | "RCS version...\n", filename); | | 225 | "RCS version...\n", filename); |
226 | | | 226 | |
227 | switch (pid = fork()) { | | 227 | switch (pid = fork()) { |
228 | case -1: | | 228 | case -1: |
229 | fatal("can't fork: %s\n", | | 229 | fatal("can't fork: %s\n", |
230 | strerror(errno)); | | 230 | strerror(errno)); |
231 | case 0: | | 231 | case 0: |
232 | devnull = open("/dev/null", O_RDONLY); | | 232 | devnull = open("/dev/null", O_RDONLY); |
233 | if (devnull == -1) { | | 233 | if (devnull == -1) { |
234 | fatal("can't open /dev/null: %s", | | 234 | fatal("can't open /dev/null: %s", |
235 | strerror(errno)); | | 235 | strerror(errno)); |
236 | } | | 236 | } |
237 | (void)dup2(devnull, STDOUT_FILENO); | | 237 | (void)dup2(devnull, STDOUT_FILENO); |
238 | argp[0] = __UNCONST(RCSDIFF); | | 238 | argp[0] = __UNCONST(RCSDIFF); |
239 | argp[1] = __UNCONST(filename); | | 239 | argp[1] = __UNCONST(filename); |
240 | execv(RCSDIFF, argp); | | 240 | execv(RCSDIFF, argp); |
241 | exit(127); | | 241 | exit(127); |
242 | } | | 242 | } |
243 | pid = waitpid(pid, &pstat, 0); | | 243 | pid = waitpid(pid, &pstat, 0); |
244 | if (pid == -1 || WEXITSTATUS(pstat) != 0) { | | 244 | if (pid == -1 || WEXITSTATUS(pstat) != 0) { |
245 | fatal("can't check out file %s: " | | 245 | fatal("can't check out file %s: " |
246 | "differs from default RCS version\n", | | 246 | "differs from default RCS version\n", |
247 | filename); | | 247 | filename); |
248 | } | | 248 | } |
249 | } | | 249 | } |
250 | | | 250 | |
251 | if (verbose) | | 251 | if (verbose) |
252 | say("Checking out file %s from RCS...\n", | | 252 | say("Checking out file %s from RCS...\n", |
253 | filename); | | 253 | filename); |
254 | | | 254 | |
255 | switch (pid = fork()) { | | 255 | switch (pid = fork()) { |
256 | case -1: | | 256 | case -1: |
257 | fatal("can't fork: %s\n", strerror(errno)); | | 257 | fatal("can't fork: %s\n", strerror(errno)); |
258 | case 0: | | 258 | case 0: |
259 | argp[0] = __UNCONST(CHECKOUT); | | 259 | argp[0] = __UNCONST(CHECKOUT); |
260 | argp[1] = __UNCONST("-l"); | | 260 | argp[1] = __UNCONST("-l"); |
261 | argp[2] = __UNCONST(filename); | | 261 | argp[2] = __UNCONST(filename); |
262 | execv(CHECKOUT, argp); | | 262 | execv(CHECKOUT, argp); |
263 | exit(127); | | 263 | exit(127); |
264 | } | | 264 | } |
265 | pid = waitpid(pid, &pstat, 0); | | 265 | pid = waitpid(pid, &pstat, 0); |
266 | if (pid == -1 || WEXITSTATUS(pstat) != 0 || | | 266 | if (pid == -1 || WEXITSTATUS(pstat) != 0 || |
267 | stat(filename, &filestat)) { | | 267 | stat(filename, &filestat)) { |
268 | fatal("can't check out file %s from RCS\n", | | 268 | fatal("can't check out file %s from RCS\n", |
269 | filename); | | 269 | filename); |
270 | } | | 270 | } |
271 | } else if (statfailed) { | | 271 | } else if (statfailed) { |
272 | fatal("can't find %s\n", filename); | | 272 | fatal("can't find %s\n", filename); |
273 | } | | 273 | } |
274 | free(tmp_filename1); | | 274 | free(tmp_filename1); |
275 | free(tmp_filename2); | | 275 | free(tmp_filename2); |
276 | } | | 276 | } |
277 | | | 277 | |
278 | filemode = filestat.st_mode; | | 278 | filemode = filestat.st_mode; |
279 | if (!S_ISREG(filemode)) | | 279 | if (!S_ISREG(filemode)) |
280 | fatal("%s is not a normal file--can't patch\n", filename); | | 280 | fatal("%s is not a normal file--can't patch\n", filename); |
281 | i_size = filestat.st_size; | | 281 | i_size = filestat.st_size; |
282 | if (out_of_mem) { | | 282 | if (out_of_mem) { |
283 | set_hunkmax(); /* make sure dynamic arrays are allocated */ | | 283 | set_hunkmax(); /* make sure dynamic arrays are allocated */ |
284 | out_of_mem = false; | | 284 | out_of_mem = false; |
285 | return false; /* force plan b because plan a bombed */ | | 285 | return false; /* force plan b because plan a bombed */ |
286 | } | | 286 | } |
287 | if ((uintmax_t)i_size > (uintmax_t)SIZE_MAX) { | | 287 | if ((uintmax_t)i_size > (uintmax_t)SIZE_MAX) { |
288 | say("block too large to mmap\n"); | | 288 | say("block too large to mmap\n"); |
289 | return false; | | 289 | return false; |
290 | } | | 290 | } |
291 | if ((ifd = open(filename, O_RDONLY)) < 0) | | 291 | if ((ifd = open(filename, O_RDONLY)) < 0) |
292 | pfatal("can't open file %s", filename); | | 292 | pfatal("can't open file %s", filename); |
293 | | | 293 | |
294 | if (i_size) { | | 294 | if (i_size) { |
295 | i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); | | 295 | i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); |
296 | if (i_womp == MAP_FAILED) { | | 296 | if (i_womp == MAP_FAILED) { |
297 | perror("mmap failed"); | | 297 | perror("mmap failed"); |
298 | i_womp = NULL; | | 298 | i_womp = NULL; |
299 | close(ifd); | | 299 | close(ifd); |
300 | return false; | | 300 | return false; |
301 | } | | 301 | } |
302 | } else { | | 302 | } else { |
303 | i_womp = NULL; | | 303 | i_womp = NULL; |
304 | } | | 304 | } |
305 | | | 305 | |
306 | close(ifd); | | 306 | close(ifd); |
307 | if (i_size) | | 307 | if (i_size) |
308 | madvise(i_womp, i_size, MADV_SEQUENTIAL); | | 308 | madvise(i_womp, i_size, MADV_SEQUENTIAL); |
309 | | | 309 | |
310 | /* estimate the number of lines */ | | 310 | /* estimate the number of lines */ |
311 | lines_allocated = i_size / 25; | | 311 | lines_allocated = i_size / 25; |
312 | if (lines_allocated < 100) | | 312 | if (lines_allocated < 100) |
313 | lines_allocated = 100; | | 313 | lines_allocated = 100; |
314 | | | 314 | |
315 | if (!reallocate_lines(&lines_allocated)) | | 315 | if (!reallocate_lines(&lines_allocated)) |
316 | return false; | | 316 | return false; |
317 | | | 317 | |
318 | /* now scan the buffer and build pointer array */ | | 318 | /* now scan the buffer and build pointer array */ |
319 | iline = 1; | | 319 | iline = 1; |
320 | i_ptr[iline] = i_womp; | | 320 | i_ptr[iline] = i_womp; |
321 | /* test for NUL too, to maintain the behavior of the original code */ | | 321 | /* test for NUL too, to maintain the behavior of the original code */ |
322 | for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) { | | 322 | for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) { |
323 | if (*s == '\n') { | | 323 | if (*s == '\n') { |
324 | if (iline == lines_allocated) { | | 324 | if (iline == lines_allocated) { |
325 | if (!reallocate_lines(&lines_allocated)) | | 325 | if (!reallocate_lines(&lines_allocated)) |
326 | return false; | | 326 | return false; |
327 | } | | 327 | } |
328 | /* these are NOT NUL terminated */ | | 328 | /* these are NOT NUL terminated */ |
329 | i_ptr[++iline] = s + 1; | | 329 | i_ptr[++iline] = s + 1; |
330 | } | | 330 | } |
331 | } | | 331 | } |
332 | /* if the last line contains no EOL, append one */ | | 332 | /* if the last line contains no EOL, append one */ |
333 | if (i_size > 0 && i_womp[i_size - 1] != '\n') { | | 333 | if (i_size > 0 && i_womp[i_size - 1] != '\n') { |
334 | last_line_missing_eol = true; | | 334 | last_line_missing_eol = true; |
335 | /* fix last line */ | | 335 | /* fix last line */ |
336 | sz = s - i_ptr[iline]; | | 336 | sz = s - i_ptr[iline]; |
337 | p = malloc(sz + 1); | | 337 | p = malloc(sz + 1); |
338 | if (p == NULL) { | | 338 | if (p == NULL) { |
339 | free(i_ptr); | | 339 | free(i_ptr); |
340 | i_ptr = NULL; | | 340 | i_ptr = NULL; |
341 | munmap(i_womp, i_size); | | 341 | munmap(i_womp, i_size); |
342 | i_womp = NULL; | | 342 | i_womp = NULL; |
343 | return false; | | 343 | return false; |
344 | } | | 344 | } |
345 | | | 345 | |
346 | memcpy(p, i_ptr[iline], sz); | | 346 | memcpy(p, i_ptr[iline], sz); |
347 | p[sz] = '\n'; | | 347 | p[sz] = '\n'; |
348 | i_ptr[iline] = p; | | 348 | i_ptr[iline] = p; |
349 | /* count the extra line and make it point to some valid mem */ | | 349 | /* count the extra line and make it point to some valid mem */ |
350 | i_ptr[++iline] = empty_line; | | 350 | i_ptr[++iline] = empty_line; |
351 | } else | | 351 | } else |
352 | last_line_missing_eol = false; | | 352 | last_line_missing_eol = false; |
353 | | | 353 | |
354 | input_lines = iline - 1; | | 354 | input_lines = iline - 1; |
355 | | | 355 | |
356 | /* now check for revision, if any */ | | 356 | /* now check for revision, if any */ |
357 | | | 357 | |
358 | if (revision != NULL) { | | 358 | if (revision != NULL) { |
359 | if (!rev_in_string(i_womp)) { | | 359 | if (!rev_in_string(i_womp)) { |
360 | if (force) { | | 360 | if (force) { |
361 | if (verbose) | | 361 | if (verbose) |
362 | say("Warning: this file doesn't appear " | | 362 | say("Warning: this file doesn't appear " |
363 | "to be the %s version--patching anyway.\n", | | 363 | "to be the %s version--patching anyway.\n", |
364 | revision); | | 364 | revision); |
365 | } else if (batch) { | | 365 | } else if (batch) { |
366 | fatal("this file doesn't appear to be the " | | 366 | fatal("this file doesn't appear to be the " |
367 | "%s version--aborting.\n", | | 367 | "%s version--aborting.\n", |
368 | revision); | | 368 | revision); |
369 | } else { | | 369 | } else { |
370 | ask("This file doesn't appear to be the " | | 370 | ask("This file doesn't appear to be the " |
371 | "%s version--patch anyway? [n] ", | | 371 | "%s version--patch anyway? [n] ", |
372 | revision); | | 372 | revision); |
373 | if (*buf != 'y') | | 373 | if (*buf != 'y') |
374 | fatal("aborted\n"); | | 374 | fatal("aborted\n"); |
375 | } | | 375 | } |
376 | } else if (verbose) | | 376 | } else if (verbose) |
377 | say("Good. This file appears to be the %s version.\n", | | 377 | say("Good. This file appears to be the %s version.\n", |
378 | revision); | | 378 | revision); |
379 | } | | 379 | } |
380 | return true; /* plan a will work */ | | 380 | return true; /* plan a will work */ |
381 | } | | 381 | } |
382 | | | 382 | |
383 | /* Keep (virtually) nothing in memory. */ | | 383 | /* Keep (virtually) nothing in memory. */ |
384 | | | 384 | |
385 | static void | | 385 | static void |
386 | plan_b(const char *filename) | | 386 | plan_b(const char *filename) |
387 | { | | 387 | { |
388 | FILE *ifp; | | 388 | FILE *ifp; |
389 | size_t i = 0, j, maxlen = 1; | | 389 | size_t i = 0, j, maxlen = 1; |
390 | char *p; | | 390 | char *p; |
391 | bool found_revision = (revision == NULL); | | 391 | bool found_revision = (revision == NULL); |
392 | | | 392 | |
393 | using_plan_a = false; | | 393 | using_plan_a = false; |
394 | if ((ifp = fopen(filename, "r")) == NULL) | | 394 | if ((ifp = fopen(filename, "r")) == NULL) |
395 | pfatal("can't open file %s", filename); | | 395 | pfatal("can't open file %s", filename); |
396 | unlink(TMPINNAME); | | 396 | unlink(TMPINNAME); |
397 | if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) | | 397 | if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) |
398 | pfatal("can't open file %s", TMPINNAME); | | 398 | pfatal("can't open file %s", TMPINNAME); |
399 | while (getline(&buf, &bufsz, ifp) != -1) { | | 399 | while (getline(&buf, &bufsz, ifp) != -1) { |
400 | if (revision != NULL && !found_revision && rev_in_string(buf)) | | 400 | if (revision != NULL && !found_revision && rev_in_string(buf)) |
401 | found_revision = true; | | 401 | found_revision = true; |
402 | if ((i = strlen(buf)) > maxlen) | | 402 | if ((i = strlen(buf)) > maxlen) |
403 | maxlen = i; /* find longest line */ | | 403 | maxlen = i; /* find longest line */ |
404 | } | | 404 | } |
405 | last_line_missing_eol = i > 0 && buf[i - 1] != '\n'; | | 405 | last_line_missing_eol = i > 0 && buf[i - 1] != '\n'; |
406 | if (last_line_missing_eol && maxlen == i) | | 406 | if (last_line_missing_eol && maxlen == i) |
407 | maxlen++; | | 407 | maxlen++; |
408 | | | 408 | |
409 | if (revision != NULL) { | | 409 | if (revision != NULL) { |
410 | if (!found_revision) { | | 410 | if (!found_revision) { |
411 | if (force) { | | 411 | if (force) { |
412 | if (verbose) | | 412 | if (verbose) |
413 | say("Warning: this file doesn't appear " | | 413 | say("Warning: this file doesn't appear " |
414 | "to be the %s version--patching anyway.\n", | | 414 | "to be the %s version--patching anyway.\n", |
415 | revision); | | 415 | revision); |
416 | } else if (batch) { | | 416 | } else if (batch) { |
417 | fatal("this file doesn't appear to be the " | | 417 | fatal("this file doesn't appear to be the " |
418 | "%s version--aborting.\n", | | 418 | "%s version--aborting.\n", |
419 | revision); | | 419 | revision); |
420 | } else { | | 420 | } else { |
421 | ask("This file doesn't appear to be the %s " | | 421 | ask("This file doesn't appear to be the %s " |
422 | "version--patch anyway? [n] ", | | 422 | "version--patch anyway? [n] ", |
423 | revision); | | 423 | revision); |
424 | if (*buf != 'y') | | 424 | if (*buf != 'y') |
425 | fatal("aborted\n"); | | 425 | fatal("aborted\n"); |
426 | } | | 426 | } |
427 | } else if (verbose) | | 427 | } else if (verbose) |
428 | say("Good. This file appears to be the %s version.\n", | | 428 | say("Good. This file appears to be the %s version.\n", |
429 | revision); | | 429 | revision); |
430 | } | | 430 | } |
431 | fseek(ifp, 0L, SEEK_SET); /* rewind file */ | | 431 | fseek(ifp, 0L, SEEK_SET); /* rewind file */ |
432 | lines_per_buf = BUFFERSIZE / maxlen; | | 432 | lines_per_buf = BUFFERSIZE / maxlen; |
433 | tireclen = maxlen; | | 433 | tireclen = maxlen; |
434 | tibuf[0] = malloc(BUFFERSIZE + 1); | | 434 | tibuf[0] = malloc(BUFFERSIZE + 1); |
435 | if (tibuf[0] == NULL) | | 435 | if (tibuf[0] == NULL) |
436 | fatal("out of memory\n"); | | 436 | fatal("out of memory\n"); |
437 | tibuf[1] = malloc(BUFFERSIZE + 1); | | 437 | tibuf[1] = malloc(BUFFERSIZE + 1); |
438 | if (tibuf[1] == NULL) | | 438 | if (tibuf[1] == NULL) |
439 | fatal("out of memory\n"); | | 439 | fatal("out of memory\n"); |
440 | for (i = 1;; i++) { | | 440 | for (i = 1;; i++) { |
441 | p = tibuf[0] + maxlen * (i % lines_per_buf); | | 441 | p = tibuf[0] + maxlen * (i % lines_per_buf); |
442 | if (i % lines_per_buf == 0) /* new block */ | | 442 | if (i % lines_per_buf == 0) /* new block */ |
443 | if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) | | 443 | if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) |
444 | pfatal("can't write temp file"); | | 444 | pfatal("can't write temp file"); |
445 | if (fgets(p, maxlen + 1, ifp) == NULL) { | | 445 | if (fgets(p, maxlen + 1, ifp) == NULL) { |
446 | input_lines = i - 1; | | 446 | input_lines = i - 1; |
447 | if (i % lines_per_buf != 0) | | 447 | if (i % lines_per_buf != 0) |
448 | if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) | | 448 | if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) |
449 | pfatal("can't write temp file"); | | 449 | pfatal("can't write temp file"); |
450 | break; | | 450 | break; |
451 | } | | 451 | } |
452 | j = strlen(p); | | 452 | j = strlen(p); |
453 | /* These are '\n' terminated strings, so no need to add a NUL */ | | 453 | /* These are '\n' terminated strings, so no need to add a NUL */ |
454 | if (j == 0 || p[j - 1] != '\n') | | 454 | if (j == 0 || p[j - 1] != '\n') |
455 | p[j] = '\n'; | | 455 | p[j] = '\n'; |
456 | } | | 456 | } |
457 | fclose(ifp); | | 457 | fclose(ifp); |
458 | close(tifd); | | 458 | close(tifd); |
459 | if ((tifd = open(TMPINNAME, O_RDONLY)) < 0) | | 459 | if ((tifd = open(TMPINNAME, O_RDONLY)) < 0) |
460 | pfatal("can't reopen file %s", TMPINNAME); | | 460 | pfatal("can't reopen file %s", TMPINNAME); |
461 | } | | 461 | } |
462 | | | 462 | |
463 | /* | | 463 | /* |
464 | * Fetch a line from the input file, \n terminated, not necessarily \0. | | 464 | * Fetch a line from the input file, \n terminated, not necessarily \0. |
465 | */ | | 465 | */ |
466 | char * | | 466 | char * |
467 | ifetch(LINENUM line, int whichbuf) | | 467 | ifetch(LINENUM line, int whichbuf) |
468 | { | | 468 | { |
469 | if (line < 1 || line > input_lines) { | | 469 | if (line < 1 || line > input_lines) { |
470 | if (warn_on_invalid_line) { | | 470 | if (warn_on_invalid_line) { |
471 | say("No such line %ld in input file, ignoring\n", line); | | 471 | say("No such line %ld in input file, ignoring\n", line); |
472 | warn_on_invalid_line = false; | | 472 | warn_on_invalid_line = false; |
473 | } | | 473 | } |
474 | return NULL; | | 474 | return NULL; |
475 | } | | 475 | } |
476 | if (using_plan_a) | | 476 | if (using_plan_a) |
477 | return i_ptr[line]; | | 477 | return i_ptr[line]; |
478 | else { | | 478 | else { |
479 | LINENUM offline = line % lines_per_buf; | | 479 | LINENUM offline = line % lines_per_buf; |
480 | LINENUM baseline = line - offline; | | 480 | LINENUM baseline = line - offline; |
481 | | | 481 | |
482 | if (tiline[0] == baseline) | | 482 | if (tiline[0] == baseline) |
483 | whichbuf = 0; | | 483 | whichbuf = 0; |
484 | else if (tiline[1] == baseline) | | 484 | else if (tiline[1] == baseline) |
485 | whichbuf = 1; | | 485 | whichbuf = 1; |
486 | else { | | 486 | else { |
487 | tiline[whichbuf] = baseline; | | 487 | tiline[whichbuf] = baseline; |
488 | | | 488 | |
489 | if (lseek(tifd, (off_t) (baseline / lines_per_buf * | | 489 | if (lseek(tifd, (off_t) (baseline / lines_per_buf * |
490 | BUFFERSIZE), SEEK_SET) < 0) | | 490 | BUFFERSIZE), SEEK_SET) < 0) |
491 | pfatal("cannot seek in the temporary input file"); | | 491 | pfatal("cannot seek in the temporary input file"); |
492 | | | 492 | |
493 | if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) | | 493 | if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) |
494 | pfatal("error reading tmp file %s", TMPINNAME); | | 494 | pfatal("error reading tmp file %s", TMPINNAME); |
495 | } | | 495 | } |
496 | return tibuf[whichbuf] + (tireclen * offline); | | 496 | return tibuf[whichbuf] + (tireclen * offline); |
497 | } | | 497 | } |
498 | } | | 498 | } |
499 | | | 499 | |
500 | /* | | 500 | /* |
501 | * True if the string argument contains the revision number we want. | | 501 | * True if the string argument contains the revision number we want. |
502 | */ | | 502 | */ |
503 | static bool | | 503 | static bool |
504 | rev_in_string(const char *string) | | 504 | rev_in_string(const char *string) |
505 | { | | 505 | { |
506 | const char *s; | | 506 | const char *s; |
507 | size_t patlen; | | 507 | size_t patlen; |
508 | | | 508 | |
509 | if (revision == NULL) | | 509 | if (revision == NULL) |
510 | return true; | | 510 | return true; |
511 | patlen = strlen(revision); | | 511 | patlen = strlen(revision); |
512 | if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen])) | | 512 | if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen])) |
513 | return true; | | 513 | return true; |
514 | for (s = string; *s; s++) { | | 514 | for (s = string; *s; s++) { |
515 | if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) && | | 515 | if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) && |
516 | isspace((unsigned char)s[patlen + 1])) { | | 516 | isspace((unsigned char)s[patlen + 1])) { |
517 | return true; | | 517 | return true; |
518 | } | | 518 | } |
519 | } | | 519 | } |
520 | return false; | | 520 | return false; |
521 | } | | 521 | } |