2 * Copyright 1986, Larry Wall
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * patch - a program to apply diffs to original files
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
26 * $OpenBSD: inp.c,v 1.44 2015/07/26 14:32:19 millert Exp $
29 #include <sys/types.h>
53 /* Input-file-with-indexable-lines abstract type */
55 static size_t i_size; /* size of the input file */
56 static char *i_womp; /* plan a buffer for entire file */
57 static char **i_ptr; /* pointers to lines in i_womp */
58 static char empty_line[] = { '\0' };
60 static int tifd = -1; /* plan b virtual string array */
61 static char *tibuf[2]; /* plan b buffers */
62 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
63 static size_t lines_per_buf; /* how many lines per buffer */
64 static size_t tibuflen; /* plan b buffer length */
65 static size_t tireclen; /* length of records in tmp file */
67 static bool rev_in_string(const char *);
68 static bool reallocate_lines(size_t *);
70 /* returns false if insufficient memory */
71 static bool plan_a(const char *);
73 static void plan_b(const char *);
75 /* New patch--prepare to edit another file. */
84 munmap(i_womp, i_size);
89 using_plan_a = true; /* maybe the next one is smaller */
94 tibuf[0] = tibuf[1] = NULL;
95 tiline[0] = tiline[1] = -1;
100 /* Construct the line index, somehow or other. */
103 scan_input(const char *filename)
105 if (!plan_a(filename))
108 say("Patching file %s using Plan %s...\n", filename,
109 (using_plan_a ? "A" : "B"));
114 reallocate_lines(size_t *lines_allocated)
119 new_size = *lines_allocated * 3 / 2;
120 p = reallocarray(i_ptr, new_size + 2, sizeof(char *));
121 if (p == NULL) { /* shucks, it was a near thing */
122 munmap(i_womp, i_size);
126 *lines_allocated = 0;
129 *lines_allocated = new_size;
134 /* Try keeping everything in memory. */
137 plan_a(const char *filename)
141 struct stat filestat;
144 size_t iline, lines_allocated;
151 if (filename == NULL || *filename == '\0')
154 statfailed = stat(filename, &filestat);
155 if (statfailed && ok_to_create_file) {
157 say("(Creating file %s...)\n", filename);
160 * in check_patch case, we still display `Creating file' even
161 * though we're not. The rule is that -C should be as similar
162 * to normal patch behavior as possible
166 makedirs(filename, true);
167 close(creat(filename, 0666));
168 statfailed = stat(filename, &filestat);
171 fatal("can't find %s\n", filename);
172 filemode = filestat.st_mode;
173 if (!S_ISREG(filemode))
174 fatal("%s is not a normal file--can't patch\n", filename);
175 if ((uint64_t)filestat.st_size > SIZE_MAX) {
176 say("block too large to mmap\n");
179 i_size = (size_t)filestat.st_size;
181 set_hunkmax(); /* make sure dynamic arrays are allocated */
183 return false; /* force plan b because plan a bombed */
185 if ((ifd = open(filename, O_RDONLY)) < 0)
186 pfatal("can't open file %s", filename);
189 i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
190 if (i_womp == MAP_FAILED) {
191 perror("mmap failed");
202 madvise(i_womp, i_size, MADV_SEQUENTIAL);
204 /* estimate the number of lines */
205 lines_allocated = i_size / 25;
206 if (lines_allocated < 100)
207 lines_allocated = 100;
209 if (!reallocate_lines(&lines_allocated))
212 /* now scan the buffer and build pointer array */
214 i_ptr[iline] = i_womp;
216 * Testing for NUL here actively breaks files that innocently use NUL
217 * for other reasons. mmap(2) succeeded, just scan the whole buffer.
219 for (s = i_womp, i = 0; i < i_size; s++, i++) {
221 if (iline == lines_allocated) {
222 if (!reallocate_lines(&lines_allocated))
225 /* these are NOT NUL terminated */
226 i_ptr[++iline] = s + 1;
229 /* if the last line contains no EOL, append one */
230 if (i_size > 0 && i_womp[i_size - 1] != '\n') {
231 last_line_missing_eol = true;
233 sz = s - i_ptr[iline];
238 munmap(i_womp, i_size);
243 memcpy(p, i_ptr[iline], sz);
246 /* count the extra line and make it point to some valid mem */
247 i_ptr[++iline] = empty_line;
249 last_line_missing_eol = false;
251 input_lines = iline - 1;
253 /* now check for revision, if any */
255 if (revision != NULL) {
256 if (i_womp == NULL || !rev_in_string(i_womp)) {
259 say("Warning: this file doesn't appear "
260 "to be the %s version--patching anyway.\n",
263 fatal("this file doesn't appear to be the "
264 "%s version--aborting.\n",
267 ask("This file doesn't appear to be the "
268 "%s version--patch anyway? [n] ",
274 say("Good. This file appears to be the %s version.\n",
277 return true; /* plan a will work */
280 /* Keep (virtually) nothing in memory. */
283 plan_b(const char *filename)
286 size_t i, j, len, maxlen;
287 char *lbuf = NULL, *p;
288 bool found_revision = (revision == NULL);
290 using_plan_a = false;
291 if ((ifp = fopen(filename, "r")) == NULL)
292 pfatal("can't open file %s", filename);
294 if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
295 pfatal("can't open file %s", TMPINNAME);
298 while ((p = fgetln(ifp, &len)) != NULL) {
299 if (p[len - 1] == '\n')
302 /* EOF without EOL, copy and add the NUL */
303 if ((lbuf = malloc(len + 1)) == NULL)
304 fatal("out of memory\n");
305 memcpy(lbuf, p, len);
309 last_line_missing_eol = true;
312 if (revision != NULL && !found_revision && rev_in_string(p))
313 found_revision = true;
315 maxlen = len; /* find longest line */
319 pfatal("can't read file %s", filename);
321 if (revision != NULL) {
322 if (!found_revision) {
325 say("Warning: this file doesn't appear "
326 "to be the %s version--patching anyway.\n",
329 fatal("this file doesn't appear to be the "
330 "%s version--aborting.\n",
333 ask("This file doesn't appear to be the %s "
334 "version--patch anyway? [n] ",
340 say("Good. This file appears to be the %s version.\n",
343 fseek(ifp, 0L, SEEK_SET); /* rewind file */
345 tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE;
346 lines_per_buf = tibuflen / maxlen;
347 tibuf[0] = malloc(tibuflen + 1);
348 if (tibuf[0] == NULL)
349 fatal("out of memory\n");
350 tibuf[1] = malloc(tibuflen + 1);
351 if (tibuf[1] == NULL)
352 fatal("out of memory\n");
354 p = tibuf[0] + maxlen * (i % lines_per_buf);
355 if (i % lines_per_buf == 0) /* new block */
356 if (write(tifd, tibuf[0], tibuflen) !=
358 pfatal("can't write temp file");
359 if (fgets(p, maxlen + 1, ifp) == NULL) {
361 if (i % lines_per_buf != 0)
362 if (write(tifd, tibuf[0], tibuflen) !=
364 pfatal("can't write temp file");
368 /* These are '\n' terminated strings, so no need to add a NUL */
369 if (j == 0 || p[j - 1] != '\n')
374 if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
375 pfatal("can't reopen file %s", TMPINNAME);
379 * Fetch a line from the input file, \n terminated, not necessarily \0.
382 ifetch(LINENUM line, int whichbuf)
384 if (line < 1 || line > input_lines) {
385 if (warn_on_invalid_line) {
386 say("No such line %ld in input file, ignoring\n", line);
387 warn_on_invalid_line = false;
394 LINENUM offline = line % lines_per_buf;
395 LINENUM baseline = line - offline;
397 if (tiline[0] == baseline)
399 else if (tiline[1] == baseline)
402 tiline[whichbuf] = baseline;
404 if (lseek(tifd, (off_t) (baseline / lines_per_buf *
405 tibuflen), SEEK_SET) < 0)
406 pfatal("cannot seek in the temporary input file");
408 if (read(tifd, tibuf[whichbuf], tibuflen) !=
410 pfatal("error reading tmp file %s", TMPINNAME);
412 return tibuf[whichbuf] + (tireclen * offline);
417 * True if the string argument contains the revision number we want.
420 rev_in_string(const char *string)
425 if (revision == NULL)
427 patlen = strlen(revision);
428 if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
430 for (s = string; *s; s++) {
431 if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
432 isspace((unsigned char)s[patlen + 1])) {