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