ifnet: Add oqdrops statistics
[dragonfly.git] / usr.bin / patch / inp.c
blob41cc27a384b6157b5e06f104c8e2e6b61a794867
1 /*-
2 * Copyright 1986, Larry Wall
3 *
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.
8 *
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
19 * SUCH DAMAGE.
21 * patch - a program to apply diffs to original files
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24 * behaviour
26 * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $
27 * $FreeBSD: head/usr.bin/patch/inp.c 246091 2013-01-29 20:05:16Z delphij $
30 #include <sys/types.h>
31 #include <sys/file.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "common.h"
45 #include "util.h"
46 #include "pch.h"
47 #include "inp.h"
50 /* Input-file-with-indexable-lines abstract type */
52 static size_t i_size; /* size of the input file */
53 static char *i_womp; /* plan a buffer for entire file */
54 static char **i_ptr; /* pointers to lines in i_womp */
55 static char empty_line[] = { '\0' };
57 static int tifd = -1; /* plan b virtual string array */
58 static char *tibuf[2]; /* plan b buffers */
59 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
60 static LINENUM lines_per_buf; /* how many lines per buffer */
61 static int tireclen; /* length of records in tmp file */
63 static bool rev_in_string(const char *);
64 static bool reallocate_lines(size_t *);
66 /* returns false if insufficient memory */
67 static bool plan_a(const char *);
69 static void plan_b(const char *);
71 /* New patch--prepare to edit another file. */
73 void
74 re_input(void)
76 if (using_plan_a) {
77 free(i_ptr);
78 i_ptr = NULL;
79 if (i_womp != NULL) {
80 munmap(i_womp, i_size);
81 i_womp = NULL;
83 i_size = 0;
84 } else {
85 using_plan_a = true; /* maybe the next one is smaller */
86 close(tifd);
87 tifd = -1;
88 free(tibuf[0]);
89 free(tibuf[1]);
90 tibuf[0] = tibuf[1] = NULL;
91 tiline[0] = tiline[1] = -1;
92 tireclen = 0;
96 /* Construct the line index, somehow or other. */
98 void
99 scan_input(const char *filename)
101 if (!plan_a(filename))
102 plan_b(filename);
103 if (verbose) {
104 say("Patching file %s using Plan %s...\n", filename,
105 (using_plan_a ? "A" : "B"));
109 static bool
110 reallocate_lines(size_t *lines_allocated)
112 char **p;
113 size_t new_size;
115 new_size = *lines_allocated * 3 / 2;
116 p = realloc(i_ptr, (new_size + 2) * sizeof(char *));
117 if (p == NULL) { /* shucks, it was a near thing */
118 munmap(i_womp, i_size);
119 i_womp = NULL;
120 free(i_ptr);
121 i_ptr = NULL;
122 *lines_allocated = 0;
123 return false;
125 *lines_allocated = new_size;
126 i_ptr = p;
127 return true;
130 /* Try keeping everything in memory. */
132 static bool
133 plan_a(const char *filename)
135 int ifd, statfailed;
136 char *p, *s;
137 struct stat filestat;
138 ptrdiff_t sz;
139 size_t i;
140 size_t iline, lines_allocated;
142 #ifdef DEBUGGING
143 if (debug & 8)
144 return false;
145 #endif
147 if (filename == NULL || *filename == '\0')
148 return false;
150 statfailed = stat(filename, &filestat);
151 if (statfailed && ok_to_create_file) {
152 if (verbose)
153 say("(Creating file %s...)\n", filename);
156 * in check_patch case, we still display `Creating file' even
157 * though we're not. The rule is that -C should be as similar
158 * to normal patch behavior as possible
160 if (check_only)
161 return true;
162 makedirs(filename, true);
163 close(creat(filename, 0666));
164 statfailed = stat(filename, &filestat);
166 if (statfailed && check_only)
167 fatal("%s not found, -C mode, can't probe further\n", filename);
169 * For nonexistent or read-only files we no longer attempt to locate
170 * or checkout the file via RCS or SCCS, or do any call to system()
171 * whatsoever. CVE-2015-1416.
173 if (statfailed ||
174 /* No one can write to it. */
175 (filestat.st_mode & 0222) == 0 ||
176 /* I can't write to it. */
177 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
178 if (statfailed)
179 fatal("can't find %s\n", filename);
181 filemode = filestat.st_mode;
182 if (!S_ISREG(filemode))
183 fatal("%s is not a normal file--can't patch\n", filename);
184 if ((uint64_t)filestat.st_size > SIZE_MAX) {
185 say("block too large to mmap\n");
186 return false;
188 i_size = (size_t)filestat.st_size;
189 if (out_of_mem) {
190 set_hunkmax(); /* make sure dynamic arrays are allocated */
191 out_of_mem = false;
192 return false; /* force plan b because plan a bombed */
194 if ((ifd = open(filename, O_RDONLY)) < 0)
195 pfatal("can't open file %s", filename);
197 if (i_size) {
198 i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
199 if (i_womp == MAP_FAILED) {
200 perror("mmap failed");
201 i_womp = NULL;
202 close(ifd);
203 return false;
205 } else {
206 i_womp = NULL;
209 close(ifd);
210 if (i_size)
211 madvise(i_womp, i_size, MADV_SEQUENTIAL);
213 /* estimate the number of lines */
214 lines_allocated = i_size / 25;
215 if (lines_allocated < 100)
216 lines_allocated = 100;
218 if (!reallocate_lines(&lines_allocated))
219 return false;
221 /* now scan the buffer and build pointer array */
222 iline = 1;
223 i_ptr[iline] = i_womp;
224 /* test for NUL too, to maintain the behavior of the original code */
225 for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
226 if (*s == '\n') {
227 if (iline == lines_allocated) {
228 if (!reallocate_lines(&lines_allocated))
229 return false;
231 /* these are NOT NUL terminated */
232 i_ptr[++iline] = s + 1;
235 /* if the last line contains no EOL, append one */
236 if (i_size > 0 && i_womp[i_size - 1] != '\n') {
237 last_line_missing_eol = true;
238 /* fix last line */
239 sz = s - i_ptr[iline];
240 p = malloc(sz + 1);
241 if (p == NULL) {
242 free(i_ptr);
243 i_ptr = NULL;
244 munmap(i_womp, i_size);
245 i_womp = NULL;
246 return false;
249 memcpy(p, i_ptr[iline], sz);
250 p[sz] = '\n';
251 i_ptr[iline] = p;
252 /* count the extra line and make it point to some valid mem */
253 i_ptr[++iline] = empty_line;
254 } else
255 last_line_missing_eol = false;
257 input_lines = iline - 1;
259 /* now check for revision, if any */
261 if (revision != NULL) {
262 if (!rev_in_string(i_womp)) {
263 if (force) {
264 if (verbose)
265 say("Warning: this file doesn't appear "
266 "to be the %s version--patching anyway.\n",
267 revision);
268 } else if (batch) {
269 fatal("this file doesn't appear to be the "
270 "%s version--aborting.\n",
271 revision);
272 } else {
273 ask("This file doesn't appear to be the "
274 "%s version--patch anyway? [n] ",
275 revision);
276 if (*buf != 'y')
277 fatal("aborted\n");
279 } else if (verbose)
280 say("Good. This file appears to be the %s version.\n",
281 revision);
283 return true; /* plan a will work */
286 /* Keep (virtually) nothing in memory. */
288 static void
289 plan_b(const char *filename)
291 FILE *ifp;
292 size_t i = 0, j, maxlen = 1;
293 char *p;
294 bool found_revision = (revision == NULL);
296 using_plan_a = false;
297 if ((ifp = fopen(filename, "r")) == NULL)
298 pfatal("can't open file %s", filename);
299 unlink(TMPINNAME);
300 if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
301 pfatal("can't open file %s", TMPINNAME);
302 while (fgets(buf, buf_size, ifp) != NULL) {
303 if (revision != NULL && !found_revision && rev_in_string(buf))
304 found_revision = true;
305 if ((i = strlen(buf)) > maxlen)
306 maxlen = i; /* find longest line */
308 last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
309 if (last_line_missing_eol && maxlen == i)
310 maxlen++;
312 if (revision != NULL) {
313 if (!found_revision) {
314 if (force) {
315 if (verbose)
316 say("Warning: this file doesn't appear "
317 "to be the %s version--patching anyway.\n",
318 revision);
319 } else if (batch) {
320 fatal("this file doesn't appear to be the "
321 "%s version--aborting.\n",
322 revision);
323 } else {
324 ask("This file doesn't appear to be the %s "
325 "version--patch anyway? [n] ",
326 revision);
327 if (*buf != 'y')
328 fatal("aborted\n");
330 } else if (verbose)
331 say("Good. This file appears to be the %s version.\n",
332 revision);
334 fseek(ifp, 0L, SEEK_SET); /* rewind file */
335 lines_per_buf = BUFFERSIZE / maxlen;
336 tireclen = maxlen;
337 tibuf[0] = malloc(BUFFERSIZE + 1);
338 if (tibuf[0] == NULL)
339 fatal("out of memory\n");
340 tibuf[1] = malloc(BUFFERSIZE + 1);
341 if (tibuf[1] == NULL)
342 fatal("out of memory\n");
343 for (i = 1;; i++) {
344 p = tibuf[0] + maxlen * (i % lines_per_buf);
345 if (i % lines_per_buf == 0) /* new block */
346 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
347 pfatal("can't write temp file");
348 if (fgets(p, maxlen + 1, ifp) == NULL) {
349 input_lines = i - 1;
350 if (i % lines_per_buf != 0)
351 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
352 pfatal("can't write temp file");
353 break;
355 j = strlen(p);
356 /* These are '\n' terminated strings, so no need to add a NUL */
357 if (j == 0 || p[j - 1] != '\n')
358 p[j] = '\n';
360 fclose(ifp);
361 close(tifd);
362 if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
363 pfatal("can't reopen file %s", TMPINNAME);
367 * Fetch a line from the input file, \n terminated, not necessarily \0.
369 char *
370 ifetch(LINENUM line, int whichbuf)
372 if (line < 1 || line > input_lines) {
373 if (warn_on_invalid_line) {
374 say("No such line %ld in input file, ignoring\n", line);
375 warn_on_invalid_line = false;
377 return NULL;
379 if (using_plan_a)
380 return i_ptr[line];
381 else {
382 LINENUM offline = line % lines_per_buf;
383 LINENUM baseline = line - offline;
385 if (tiline[0] == baseline)
386 whichbuf = 0;
387 else if (tiline[1] == baseline)
388 whichbuf = 1;
389 else {
390 tiline[whichbuf] = baseline;
392 if (lseek(tifd, (off_t) (baseline / lines_per_buf *
393 BUFFERSIZE), SEEK_SET) < 0)
394 pfatal("cannot seek in the temporary input file");
396 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
397 pfatal("error reading tmp file %s", TMPINNAME);
399 return tibuf[whichbuf] + (tireclen * offline);
404 * True if the string argument contains the revision number we want.
406 static bool
407 rev_in_string(const char *string)
409 const char *s;
410 size_t patlen;
412 if (revision == NULL)
413 return true;
414 patlen = strlen(revision);
415 if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
416 return true;
417 for (s = string; *s; s++) {
418 if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
419 isspace((unsigned char)s[patlen + 1])) {
420 return true;
423 return false;