xref: /minix3/usr.bin/patch/inp.c (revision d8127f841f370c290f11352f513f005cb8c59e40)
1757e8328SLionel Sambuc /*
2757e8328SLionel Sambuc  * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $
3757e8328SLionel Sambuc  * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $
4*d8127f84SDavid van Moolenbroek  * $NetBSD: inp.c,v 1.24 2015/07/24 18:56:00 christos Exp $
5757e8328SLionel Sambuc  */
6757e8328SLionel Sambuc 
7757e8328SLionel Sambuc /*
8757e8328SLionel Sambuc  * patch - a program to apply diffs to original files
9757e8328SLionel Sambuc  *
10757e8328SLionel Sambuc  * Copyright 1986, Larry Wall
11757e8328SLionel Sambuc  *
12757e8328SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
13757e8328SLionel Sambuc  * modification, are permitted provided that the following condition is met:
14757e8328SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright notice,
15757e8328SLionel Sambuc  * this condition and the following disclaimer.
16757e8328SLionel Sambuc  *
17757e8328SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18757e8328SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19757e8328SLionel Sambuc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20757e8328SLionel Sambuc  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21757e8328SLionel Sambuc  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22757e8328SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23757e8328SLionel Sambuc  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24757e8328SLionel Sambuc  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25757e8328SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26757e8328SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27757e8328SLionel Sambuc  * SUCH DAMAGE.
28757e8328SLionel Sambuc  *
29757e8328SLionel Sambuc  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
30757e8328SLionel Sambuc  * behaviour
31757e8328SLionel Sambuc  */
32757e8328SLionel Sambuc 
33757e8328SLionel Sambuc #include <sys/cdefs.h>
34*d8127f84SDavid van Moolenbroek __RCSID("$NetBSD: inp.c,v 1.24 2015/07/24 18:56:00 christos Exp $");
35757e8328SLionel Sambuc 
36757e8328SLionel Sambuc #include <sys/types.h>
37757e8328SLionel Sambuc #include <sys/file.h>
38757e8328SLionel Sambuc #include <sys/stat.h>
39757e8328SLionel Sambuc #include <sys/mman.h>
40*d8127f84SDavid van Moolenbroek #include <sys/wait.h>
41757e8328SLionel Sambuc 
42757e8328SLionel Sambuc #include <ctype.h>
43*d8127f84SDavid van Moolenbroek #include <errno.h>
44757e8328SLionel Sambuc #include <fcntl.h>
45757e8328SLionel Sambuc #include <libgen.h>
46757e8328SLionel Sambuc #include <limits.h>
47757e8328SLionel Sambuc #include <stddef.h>
48757e8328SLionel Sambuc #include <stdio.h>
49757e8328SLionel Sambuc #include <stdlib.h>
50757e8328SLionel Sambuc #include <string.h>
51757e8328SLionel Sambuc #include <unistd.h>
52757e8328SLionel Sambuc 
53757e8328SLionel Sambuc #include "common.h"
54757e8328SLionel Sambuc #include "util.h"
55757e8328SLionel Sambuc #include "pch.h"
56757e8328SLionel Sambuc #include "inp.h"
57757e8328SLionel Sambuc 
58757e8328SLionel Sambuc 
59757e8328SLionel Sambuc /* Input-file-with-indexable-lines abstract type */
60757e8328SLionel Sambuc 
61757e8328SLionel Sambuc static off_t	i_size;		/* size of the input file */
62757e8328SLionel Sambuc static char	*i_womp;	/* plan a buffer for entire file */
63757e8328SLionel Sambuc static char	**i_ptr;	/* pointers to lines in i_womp */
64757e8328SLionel Sambuc static char	empty_line[] = { '\0' };
65757e8328SLionel Sambuc 
66757e8328SLionel Sambuc static int	tifd = -1;	/* plan b virtual string array */
67757e8328SLionel Sambuc static char	*tibuf[2];	/* plan b buffers */
68757e8328SLionel Sambuc static LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
69757e8328SLionel Sambuc static LINENUM	lines_per_buf;	/* how many lines per buffer */
70757e8328SLionel Sambuc static int	tireclen;	/* length of records in tmp file */
71757e8328SLionel Sambuc 
72757e8328SLionel Sambuc static bool	rev_in_string(const char *);
73757e8328SLionel Sambuc static bool	reallocate_lines(size_t *);
74757e8328SLionel Sambuc 
75757e8328SLionel Sambuc /* returns false if insufficient memory */
76757e8328SLionel Sambuc static bool	plan_a(const char *);
77757e8328SLionel Sambuc 
78757e8328SLionel Sambuc static void	plan_b(const char *);
79757e8328SLionel Sambuc 
80757e8328SLionel Sambuc /* New patch--prepare to edit another file. */
81757e8328SLionel Sambuc 
82757e8328SLionel Sambuc void
re_input(void)83757e8328SLionel Sambuc re_input(void)
84757e8328SLionel Sambuc {
85757e8328SLionel Sambuc 	if (using_plan_a) {
86757e8328SLionel Sambuc 		i_size = 0;
87757e8328SLionel Sambuc 		free(i_ptr);
88757e8328SLionel Sambuc 		i_ptr = NULL;
89757e8328SLionel Sambuc 		if (i_womp != NULL) {
90757e8328SLionel Sambuc 			munmap(i_womp, i_size);
91757e8328SLionel Sambuc 			i_womp = NULL;
92757e8328SLionel Sambuc 		}
93757e8328SLionel Sambuc 	} else {
94757e8328SLionel Sambuc 		using_plan_a = true;	/* maybe the next one is smaller */
95757e8328SLionel Sambuc 		close(tifd);
96757e8328SLionel Sambuc 		tifd = -1;
97757e8328SLionel Sambuc 		free(tibuf[0]);
98757e8328SLionel Sambuc 		free(tibuf[1]);
99757e8328SLionel Sambuc 		tibuf[0] = tibuf[1] = NULL;
100757e8328SLionel Sambuc 		tiline[0] = tiline[1] = -1;
101757e8328SLionel Sambuc 		tireclen = 0;
102757e8328SLionel Sambuc 	}
103757e8328SLionel Sambuc }
104757e8328SLionel Sambuc 
105757e8328SLionel Sambuc /* Construct the line index, somehow or other. */
106757e8328SLionel Sambuc 
107757e8328SLionel Sambuc void
scan_input(const char * filename)108757e8328SLionel Sambuc scan_input(const char *filename)
109757e8328SLionel Sambuc {
110757e8328SLionel Sambuc 	if (!plan_a(filename))
111757e8328SLionel Sambuc 		plan_b(filename);
112757e8328SLionel Sambuc 	if (verbose) {
113757e8328SLionel Sambuc 		say("Patching file %s using Plan %s...\n", filename,
114757e8328SLionel Sambuc 		    (using_plan_a ? "A" : "B"));
115757e8328SLionel Sambuc 	}
116757e8328SLionel Sambuc }
117757e8328SLionel Sambuc 
118757e8328SLionel Sambuc static bool
reallocate_lines(size_t * lines_allocated)119757e8328SLionel Sambuc reallocate_lines(size_t *lines_allocated)
120757e8328SLionel Sambuc {
121757e8328SLionel Sambuc 	char	**p;
122757e8328SLionel Sambuc 	size_t	new_size;
123757e8328SLionel Sambuc 
124757e8328SLionel Sambuc 	new_size = *lines_allocated * 3 / 2;
125757e8328SLionel Sambuc 	p = realloc(i_ptr, (new_size + 2) * sizeof(char *));
126757e8328SLionel Sambuc 	if (p == NULL) {	/* shucks, it was a near thing */
127757e8328SLionel Sambuc 		munmap(i_womp, i_size);
128757e8328SLionel Sambuc 		i_womp = NULL;
129757e8328SLionel Sambuc 		free(i_ptr);
130757e8328SLionel Sambuc 		i_ptr = NULL;
131757e8328SLionel Sambuc 		*lines_allocated = 0;
132757e8328SLionel Sambuc 		return false;
133757e8328SLionel Sambuc 	}
134757e8328SLionel Sambuc 	*lines_allocated = new_size;
135757e8328SLionel Sambuc 	i_ptr = p;
136757e8328SLionel Sambuc 	return true;
137757e8328SLionel Sambuc }
138757e8328SLionel Sambuc 
139757e8328SLionel Sambuc /* Try keeping everything in memory. */
140757e8328SLionel Sambuc 
141757e8328SLionel Sambuc static bool
plan_a(const char * filename)142757e8328SLionel Sambuc plan_a(const char *filename)
143757e8328SLionel Sambuc {
144*d8127f84SDavid van Moolenbroek 	int		ifd, statfailed, devnull, pstat;
145757e8328SLionel Sambuc 	char		*p, *s, lbuf[MAXLINELEN];
146757e8328SLionel Sambuc 	struct stat	filestat;
147757e8328SLionel Sambuc 	off_t		i;
148757e8328SLionel Sambuc 	ptrdiff_t	sz;
149757e8328SLionel Sambuc 	size_t		iline, lines_allocated;
150*d8127f84SDavid van Moolenbroek 	pid_t		pid;
151*d8127f84SDavid van Moolenbroek 	char		*argp[4] = {NULL};
152757e8328SLionel Sambuc 
153757e8328SLionel Sambuc #ifdef DEBUGGING
154757e8328SLionel Sambuc 	if (debug & 8)
155757e8328SLionel Sambuc 		return false;
156757e8328SLionel Sambuc #endif
157757e8328SLionel Sambuc 
158757e8328SLionel Sambuc 	if (filename == NULL || *filename == '\0')
159757e8328SLionel Sambuc 		return false;
160757e8328SLionel Sambuc 
161757e8328SLionel Sambuc 	statfailed = stat(filename, &filestat);
162757e8328SLionel Sambuc 	if (statfailed && ok_to_create_file) {
163757e8328SLionel Sambuc 		if (verbose)
164757e8328SLionel Sambuc 			say("(Creating file %s...)\n", filename);
165757e8328SLionel Sambuc 
166757e8328SLionel Sambuc 		/*
167757e8328SLionel Sambuc 		 * in check_patch case, we still display `Creating file' even
168757e8328SLionel Sambuc 		 * though we're not. The rule is that -C should be as similar
169757e8328SLionel Sambuc 		 * to normal patch behavior as possible
170757e8328SLionel Sambuc 		 */
171757e8328SLionel Sambuc 		if (check_only)
172757e8328SLionel Sambuc 			return true;
173757e8328SLionel Sambuc 		makedirs(filename, true);
174757e8328SLionel Sambuc 		close(creat(filename, 0666));
175757e8328SLionel Sambuc 		statfailed = stat(filename, &filestat);
176757e8328SLionel Sambuc 	}
177757e8328SLionel Sambuc 	if (statfailed && check_only)
178757e8328SLionel Sambuc 		fatal("%s not found, -C mode, can't probe further\n", filename);
179*d8127f84SDavid van Moolenbroek 	/* For nonexistent or read-only files, look for RCS versions.  */
180757e8328SLionel Sambuc 	if (statfailed ||
181757e8328SLionel Sambuc 	    /* No one can write to it.  */
182757e8328SLionel Sambuc 	    (filestat.st_mode & 0222) == 0 ||
183757e8328SLionel Sambuc 	    /* I can't write to it.  */
184757e8328SLionel Sambuc 	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
185*d8127f84SDavid van Moolenbroek 		char	*filebase, *filedir;
186757e8328SLionel Sambuc 		struct stat	cstat;
187757e8328SLionel Sambuc 		char *tmp_filename1, *tmp_filename2;
188757e8328SLionel Sambuc 
189757e8328SLionel Sambuc 		tmp_filename1 = strdup(filename);
190757e8328SLionel Sambuc 		tmp_filename2 = strdup(filename);
191757e8328SLionel Sambuc 		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
192757e8328SLionel Sambuc 			fatal("strdupping filename");
193*d8127f84SDavid van Moolenbroek 
194757e8328SLionel Sambuc  		filebase = basename(tmp_filename1);
195757e8328SLionel Sambuc  		filedir = dirname(tmp_filename2);
196757e8328SLionel Sambuc 
197757e8328SLionel Sambuc #define try(f, a1, a2, a3) \
198*d8127f84SDavid van Moolenbroek 	(snprintf(lbuf, sizeof lbuf, f, a1, a2, a3), stat(lbuf, &cstat) == 0)
199757e8328SLionel Sambuc 
200757e8328SLionel Sambuc 		/*
201757e8328SLionel Sambuc 		 * else we can't write to it but it's not under a version
202757e8328SLionel Sambuc 		 * control system, so just proceed.
203757e8328SLionel Sambuc 		 */
204*d8127f84SDavid van Moolenbroek 		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
205*d8127f84SDavid van Moolenbroek 		    try("%s/RCS/%s%s", filedir, filebase, "") ||
206*d8127f84SDavid van Moolenbroek 		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
207757e8328SLionel Sambuc 			if (!statfailed) {
208757e8328SLionel Sambuc 				if ((filestat.st_mode & 0222) != 0)
209757e8328SLionel Sambuc 					/* The owner can write to it.  */
210757e8328SLionel Sambuc 					fatal("file %s seems to be locked "
211*d8127f84SDavid van Moolenbroek 					    "by somebody else under RCS\n",
212*d8127f84SDavid van Moolenbroek 					    filename);
213757e8328SLionel Sambuc 				/*
214757e8328SLionel Sambuc 				 * It might be checked out unlocked.  See if
215757e8328SLionel Sambuc 				 * it's safe to check out the default version
216757e8328SLionel Sambuc 				 * locked.
217757e8328SLionel Sambuc 				 */
218757e8328SLionel Sambuc 				if (verbose)
219757e8328SLionel Sambuc 					say("Comparing file %s to default "
220*d8127f84SDavid van Moolenbroek 					    "RCS version...\n", filename);
221*d8127f84SDavid van Moolenbroek 
222*d8127f84SDavid van Moolenbroek 				switch (pid = fork()) {
223*d8127f84SDavid van Moolenbroek 				case -1:
224*d8127f84SDavid van Moolenbroek 					fatal("can't fork: %s\n",
225*d8127f84SDavid van Moolenbroek 					    strerror(errno));
226*d8127f84SDavid van Moolenbroek 				case 0:
227*d8127f84SDavid van Moolenbroek 					devnull = open("/dev/null", O_RDONLY);
228*d8127f84SDavid van Moolenbroek 					if (devnull == -1) {
229*d8127f84SDavid van Moolenbroek 						fatal("can't open /dev/null: %s",
230*d8127f84SDavid van Moolenbroek 						    strerror(errno));
231*d8127f84SDavid van Moolenbroek 					}
232*d8127f84SDavid van Moolenbroek 					(void)dup2(devnull, STDOUT_FILENO);
233*d8127f84SDavid van Moolenbroek 					argp[0] = __UNCONST(RCSDIFF);
234*d8127f84SDavid van Moolenbroek 					argp[1] = __UNCONST(filename);
235*d8127f84SDavid van Moolenbroek 					execv(RCSDIFF, argp);
236*d8127f84SDavid van Moolenbroek 					exit(127);
237*d8127f84SDavid van Moolenbroek 				}
238*d8127f84SDavid van Moolenbroek 				pid = waitpid(pid, &pstat, 0);
239*d8127f84SDavid van Moolenbroek 				if (pid == -1 || WEXITSTATUS(pstat) != 0) {
240757e8328SLionel Sambuc 					fatal("can't check out file %s: "
241*d8127f84SDavid van Moolenbroek 					    "differs from default RCS version\n",
242*d8127f84SDavid van Moolenbroek 					    filename);
243757e8328SLionel Sambuc 				}
244*d8127f84SDavid van Moolenbroek 			}
245*d8127f84SDavid van Moolenbroek 
246757e8328SLionel Sambuc 			if (verbose)
247*d8127f84SDavid van Moolenbroek 				say("Checking out file %s from RCS...\n",
248*d8127f84SDavid van Moolenbroek 				    filename);
249*d8127f84SDavid van Moolenbroek 
250*d8127f84SDavid van Moolenbroek 			switch (pid = fork()) {
251*d8127f84SDavid van Moolenbroek 			case -1:
252*d8127f84SDavid van Moolenbroek 				fatal("can't fork: %s\n", strerror(errno));
253*d8127f84SDavid van Moolenbroek 			case 0:
254*d8127f84SDavid van Moolenbroek 				argp[0] = __UNCONST(CHECKOUT);
255*d8127f84SDavid van Moolenbroek 				argp[1] = __UNCONST("-l");
256*d8127f84SDavid van Moolenbroek 				argp[2] = __UNCONST(filename);
257*d8127f84SDavid van Moolenbroek 				execv(CHECKOUT, argp);
258*d8127f84SDavid van Moolenbroek 				exit(127);
259757e8328SLionel Sambuc 			}
260*d8127f84SDavid van Moolenbroek 			pid = waitpid(pid, &pstat, 0);
261*d8127f84SDavid van Moolenbroek 			if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
262*d8127f84SDavid van Moolenbroek 			    stat(filename, &filestat)) {
263*d8127f84SDavid van Moolenbroek 				fatal("can't check out file %s from RCS\n",
264*d8127f84SDavid van Moolenbroek 				    filename);
265757e8328SLionel Sambuc 			}
266*d8127f84SDavid van Moolenbroek 		} else if (statfailed) {
267*d8127f84SDavid van Moolenbroek 			fatal("can't find %s\n", filename);
268*d8127f84SDavid van Moolenbroek 		}
269*d8127f84SDavid van Moolenbroek 		free(tmp_filename1);
270*d8127f84SDavid van Moolenbroek 		free(tmp_filename2);
271*d8127f84SDavid van Moolenbroek 	}
272*d8127f84SDavid van Moolenbroek 
273757e8328SLionel Sambuc 	filemode = filestat.st_mode;
274757e8328SLionel Sambuc 	if (!S_ISREG(filemode))
275757e8328SLionel Sambuc 		fatal("%s is not a normal file--can't patch\n", filename);
276757e8328SLionel Sambuc 	i_size = filestat.st_size;
277757e8328SLionel Sambuc 	if (out_of_mem) {
278757e8328SLionel Sambuc 		set_hunkmax();	/* make sure dynamic arrays are allocated */
279757e8328SLionel Sambuc 		out_of_mem = false;
280757e8328SLionel Sambuc 		return false;	/* force plan b because plan a bombed */
281757e8328SLionel Sambuc 	}
282757e8328SLionel Sambuc 	if ((uintmax_t)i_size > (uintmax_t)SIZE_MAX) {
283757e8328SLionel Sambuc 		say("block too large to mmap\n");
284757e8328SLionel Sambuc 		return false;
285757e8328SLionel Sambuc 	}
286757e8328SLionel Sambuc 	if ((ifd = open(filename, O_RDONLY)) < 0)
287757e8328SLionel Sambuc 		pfatal("can't open file %s", filename);
288757e8328SLionel Sambuc 
289757e8328SLionel Sambuc 	if (i_size) {
290757e8328SLionel Sambuc 		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
291757e8328SLionel Sambuc 		if (i_womp == MAP_FAILED) {
292757e8328SLionel Sambuc 			perror("mmap failed");
293757e8328SLionel Sambuc 			i_womp = NULL;
294757e8328SLionel Sambuc 			close(ifd);
295757e8328SLionel Sambuc 			return false;
296757e8328SLionel Sambuc 		}
297757e8328SLionel Sambuc 	} else {
298757e8328SLionel Sambuc 		i_womp = NULL;
299757e8328SLionel Sambuc 	}
300757e8328SLionel Sambuc 
301757e8328SLionel Sambuc 	close(ifd);
302757e8328SLionel Sambuc #if !defined(__minix)
303757e8328SLionel Sambuc 	if (i_size)
304757e8328SLionel Sambuc 		madvise(i_womp, i_size, MADV_SEQUENTIAL);
305757e8328SLionel Sambuc #endif /* !defined(__minix) */
306757e8328SLionel Sambuc 
307757e8328SLionel Sambuc 	/* estimate the number of lines */
308757e8328SLionel Sambuc 	lines_allocated = i_size / 25;
309757e8328SLionel Sambuc 	if (lines_allocated < 100)
310757e8328SLionel Sambuc 		lines_allocated = 100;
311757e8328SLionel Sambuc 
312757e8328SLionel Sambuc 	if (!reallocate_lines(&lines_allocated))
313757e8328SLionel Sambuc 		return false;
314757e8328SLionel Sambuc 
315757e8328SLionel Sambuc 	/* now scan the buffer and build pointer array */
316757e8328SLionel Sambuc 	iline = 1;
317757e8328SLionel Sambuc 	i_ptr[iline] = i_womp;
318757e8328SLionel Sambuc 	/* test for NUL too, to maintain the behavior of the original code */
319757e8328SLionel Sambuc 	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
320757e8328SLionel Sambuc 		if (*s == '\n') {
321757e8328SLionel Sambuc 			if (iline == lines_allocated) {
322757e8328SLionel Sambuc 				if (!reallocate_lines(&lines_allocated))
323757e8328SLionel Sambuc 					return false;
324757e8328SLionel Sambuc 			}
325757e8328SLionel Sambuc 			/* these are NOT NUL terminated */
326757e8328SLionel Sambuc 			i_ptr[++iline] = s + 1;
327757e8328SLionel Sambuc 		}
328757e8328SLionel Sambuc 	}
329757e8328SLionel Sambuc 	/* if the last line contains no EOL, append one */
330757e8328SLionel Sambuc 	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
331757e8328SLionel Sambuc 		last_line_missing_eol = true;
332757e8328SLionel Sambuc 		/* fix last line */
333757e8328SLionel Sambuc 		sz = s - i_ptr[iline];
334757e8328SLionel Sambuc 		p = malloc(sz + 1);
335757e8328SLionel Sambuc 		if (p == NULL) {
336757e8328SLionel Sambuc 			free(i_ptr);
337757e8328SLionel Sambuc 			i_ptr = NULL;
338757e8328SLionel Sambuc 			munmap(i_womp, i_size);
339757e8328SLionel Sambuc 			i_womp = NULL;
340757e8328SLionel Sambuc 			return false;
341757e8328SLionel Sambuc 		}
342757e8328SLionel Sambuc 
343757e8328SLionel Sambuc 		memcpy(p, i_ptr[iline], sz);
344757e8328SLionel Sambuc 		p[sz] = '\n';
345757e8328SLionel Sambuc 		i_ptr[iline] = p;
346757e8328SLionel Sambuc 		/* count the extra line and make it point to some valid mem */
347757e8328SLionel Sambuc 		i_ptr[++iline] = empty_line;
348757e8328SLionel Sambuc 	} else
349757e8328SLionel Sambuc 		last_line_missing_eol = false;
350757e8328SLionel Sambuc 
351757e8328SLionel Sambuc 	input_lines = iline - 1;
352757e8328SLionel Sambuc 
353757e8328SLionel Sambuc 	/* now check for revision, if any */
354757e8328SLionel Sambuc 
355757e8328SLionel Sambuc 	if (revision != NULL) {
356757e8328SLionel Sambuc 		if (!rev_in_string(i_womp)) {
357757e8328SLionel Sambuc 			if (force) {
358757e8328SLionel Sambuc 				if (verbose)
359757e8328SLionel Sambuc 					say("Warning: this file doesn't appear "
360757e8328SLionel Sambuc 					    "to be the %s version--patching anyway.\n",
361757e8328SLionel Sambuc 					    revision);
362757e8328SLionel Sambuc 			} else if (batch) {
363757e8328SLionel Sambuc 				fatal("this file doesn't appear to be the "
364757e8328SLionel Sambuc 				    "%s version--aborting.\n",
365757e8328SLionel Sambuc 				    revision);
366757e8328SLionel Sambuc 			} else {
367757e8328SLionel Sambuc 				ask("This file doesn't appear to be the "
368757e8328SLionel Sambuc 				    "%s version--patch anyway? [n] ",
369757e8328SLionel Sambuc 				    revision);
370757e8328SLionel Sambuc 				if (*buf != 'y')
371757e8328SLionel Sambuc 					fatal("aborted\n");
372757e8328SLionel Sambuc 			}
373757e8328SLionel Sambuc 		} else if (verbose)
374757e8328SLionel Sambuc 			say("Good.  This file appears to be the %s version.\n",
375757e8328SLionel Sambuc 			    revision);
376757e8328SLionel Sambuc 	}
377757e8328SLionel Sambuc 	return true;		/* plan a will work */
378757e8328SLionel Sambuc }
379757e8328SLionel Sambuc 
380757e8328SLionel Sambuc /* Keep (virtually) nothing in memory. */
381757e8328SLionel Sambuc 
382757e8328SLionel Sambuc static void
plan_b(const char * filename)383757e8328SLionel Sambuc plan_b(const char *filename)
384757e8328SLionel Sambuc {
385757e8328SLionel Sambuc 	FILE	*ifp;
386757e8328SLionel Sambuc 	size_t	i = 0, j, maxlen = 1;
387757e8328SLionel Sambuc 	char	*p;
388757e8328SLionel Sambuc 	bool	found_revision = (revision == NULL);
389757e8328SLionel Sambuc 
390757e8328SLionel Sambuc 	using_plan_a = false;
391757e8328SLionel Sambuc 	if ((ifp = fopen(filename, "r")) == NULL)
392757e8328SLionel Sambuc 		pfatal("can't open file %s", filename);
393757e8328SLionel Sambuc 	unlink(TMPINNAME);
394757e8328SLionel Sambuc 	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
395757e8328SLionel Sambuc 		pfatal("can't open file %s", TMPINNAME);
396757e8328SLionel Sambuc 	while (fgets(buf, buf_len, ifp) != NULL) {
397757e8328SLionel Sambuc 		if (revision != NULL && !found_revision && rev_in_string(buf))
398757e8328SLionel Sambuc 			found_revision = true;
399757e8328SLionel Sambuc 		if ((i = strlen(buf)) > maxlen)
400757e8328SLionel Sambuc 			maxlen = i;	/* find longest line */
401757e8328SLionel Sambuc 	}
402757e8328SLionel Sambuc 	last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
403757e8328SLionel Sambuc 	if (last_line_missing_eol && maxlen == i)
404757e8328SLionel Sambuc 		maxlen++;
405757e8328SLionel Sambuc 
406757e8328SLionel Sambuc 	if (revision != NULL) {
407757e8328SLionel Sambuc 		if (!found_revision) {
408757e8328SLionel Sambuc 			if (force) {
409757e8328SLionel Sambuc 				if (verbose)
410757e8328SLionel Sambuc 					say("Warning: this file doesn't appear "
411757e8328SLionel Sambuc 					    "to be the %s version--patching anyway.\n",
412757e8328SLionel Sambuc 					    revision);
413757e8328SLionel Sambuc 			} else if (batch) {
414757e8328SLionel Sambuc 				fatal("this file doesn't appear to be the "
415757e8328SLionel Sambuc 				    "%s version--aborting.\n",
416757e8328SLionel Sambuc 				    revision);
417757e8328SLionel Sambuc 			} else {
418757e8328SLionel Sambuc 				ask("This file doesn't appear to be the %s "
419757e8328SLionel Sambuc 				    "version--patch anyway? [n] ",
420757e8328SLionel Sambuc 				    revision);
421757e8328SLionel Sambuc 				if (*buf != 'y')
422757e8328SLionel Sambuc 					fatal("aborted\n");
423757e8328SLionel Sambuc 			}
424757e8328SLionel Sambuc 		} else if (verbose)
425757e8328SLionel Sambuc 			say("Good.  This file appears to be the %s version.\n",
426757e8328SLionel Sambuc 			    revision);
427757e8328SLionel Sambuc 	}
428757e8328SLionel Sambuc 	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
429757e8328SLionel Sambuc 	lines_per_buf = BUFFERSIZE / maxlen;
430757e8328SLionel Sambuc 	tireclen = maxlen;
431757e8328SLionel Sambuc 	tibuf[0] = malloc(BUFFERSIZE + 1);
432757e8328SLionel Sambuc 	if (tibuf[0] == NULL)
433757e8328SLionel Sambuc 		fatal("out of memory\n");
434757e8328SLionel Sambuc 	tibuf[1] = malloc(BUFFERSIZE + 1);
435757e8328SLionel Sambuc 	if (tibuf[1] == NULL)
436757e8328SLionel Sambuc 		fatal("out of memory\n");
437757e8328SLionel Sambuc 	for (i = 1;; i++) {
438757e8328SLionel Sambuc 		p = tibuf[0] + maxlen * (i % lines_per_buf);
439757e8328SLionel Sambuc 		if (i % lines_per_buf == 0)	/* new block */
440757e8328SLionel Sambuc 			if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
441757e8328SLionel Sambuc 				pfatal("can't write temp file");
442757e8328SLionel Sambuc 		if (fgets(p, maxlen + 1, ifp) == NULL) {
443757e8328SLionel Sambuc 			input_lines = i - 1;
444757e8328SLionel Sambuc 			if (i % lines_per_buf != 0)
445757e8328SLionel Sambuc 				if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
446757e8328SLionel Sambuc 					pfatal("can't write temp file");
447757e8328SLionel Sambuc 			break;
448757e8328SLionel Sambuc 		}
449757e8328SLionel Sambuc 		j = strlen(p);
450757e8328SLionel Sambuc 		/* These are '\n' terminated strings, so no need to add a NUL */
451757e8328SLionel Sambuc 		if (j == 0 || p[j - 1] != '\n')
452757e8328SLionel Sambuc 			p[j] = '\n';
453757e8328SLionel Sambuc 	}
454757e8328SLionel Sambuc 	fclose(ifp);
455757e8328SLionel Sambuc 	close(tifd);
456757e8328SLionel Sambuc 	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
457757e8328SLionel Sambuc 		pfatal("can't reopen file %s", TMPINNAME);
458757e8328SLionel Sambuc }
459757e8328SLionel Sambuc 
460757e8328SLionel Sambuc /*
461757e8328SLionel Sambuc  * Fetch a line from the input file, \n terminated, not necessarily \0.
462757e8328SLionel Sambuc  */
463757e8328SLionel Sambuc char *
ifetch(LINENUM line,int whichbuf)464757e8328SLionel Sambuc ifetch(LINENUM line, int whichbuf)
465757e8328SLionel Sambuc {
466757e8328SLionel Sambuc 	if (line < 1 || line > input_lines) {
467757e8328SLionel Sambuc 		if (warn_on_invalid_line) {
468757e8328SLionel Sambuc 			say("No such line %ld in input file, ignoring\n", line);
469757e8328SLionel Sambuc 			warn_on_invalid_line = false;
470757e8328SLionel Sambuc 		}
471757e8328SLionel Sambuc 		return NULL;
472757e8328SLionel Sambuc 	}
473757e8328SLionel Sambuc 	if (using_plan_a)
474757e8328SLionel Sambuc 		return i_ptr[line];
475757e8328SLionel Sambuc 	else {
476757e8328SLionel Sambuc 		LINENUM	offline = line % lines_per_buf;
477757e8328SLionel Sambuc 		LINENUM	baseline = line - offline;
478757e8328SLionel Sambuc 
479757e8328SLionel Sambuc 		if (tiline[0] == baseline)
480757e8328SLionel Sambuc 			whichbuf = 0;
481757e8328SLionel Sambuc 		else if (tiline[1] == baseline)
482757e8328SLionel Sambuc 			whichbuf = 1;
483757e8328SLionel Sambuc 		else {
484757e8328SLionel Sambuc 			tiline[whichbuf] = baseline;
485757e8328SLionel Sambuc 
486757e8328SLionel Sambuc 			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
487757e8328SLionel Sambuc 			    BUFFERSIZE), SEEK_SET) < 0)
488757e8328SLionel Sambuc 				pfatal("cannot seek in the temporary input file");
489757e8328SLionel Sambuc 
490757e8328SLionel Sambuc 			if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
491757e8328SLionel Sambuc 				pfatal("error reading tmp file %s", TMPINNAME);
492757e8328SLionel Sambuc 		}
493757e8328SLionel Sambuc 		return tibuf[whichbuf] + (tireclen * offline);
494757e8328SLionel Sambuc 	}
495757e8328SLionel Sambuc }
496757e8328SLionel Sambuc 
497757e8328SLionel Sambuc /*
498757e8328SLionel Sambuc  * True if the string argument contains the revision number we want.
499757e8328SLionel Sambuc  */
500757e8328SLionel Sambuc static bool
rev_in_string(const char * string)501757e8328SLionel Sambuc rev_in_string(const char *string)
502757e8328SLionel Sambuc {
503757e8328SLionel Sambuc 	const char	*s;
504757e8328SLionel Sambuc 	size_t		patlen;
505757e8328SLionel Sambuc 
506757e8328SLionel Sambuc 	if (revision == NULL)
507757e8328SLionel Sambuc 		return true;
508757e8328SLionel Sambuc 	patlen = strlen(revision);
509757e8328SLionel Sambuc 	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
510757e8328SLionel Sambuc 		return true;
511757e8328SLionel Sambuc 	for (s = string; *s; s++) {
512757e8328SLionel Sambuc 		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
513757e8328SLionel Sambuc 		    isspace((unsigned char)s[patlen + 1])) {
514757e8328SLionel Sambuc 			return true;
515757e8328SLionel Sambuc 		}
516757e8328SLionel Sambuc 	}
517757e8328SLionel Sambuc 	return false;
518757e8328SLionel Sambuc }
519