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