1246074Sgabor/*-
2246074Sgabor * Copyright 1986, Larry Wall
3246074Sgabor *
4246074Sgabor * Redistribution and use in source and binary forms, with or without
5246074Sgabor * modification, are permitted provided that the following condition is met:
6246074Sgabor * 1. Redistributions of source code must retain the above copyright notice,
7246074Sgabor * this condition and the following disclaimer.
8246074Sgabor *
9246074Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10246074Sgabor * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11246074Sgabor * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12246074Sgabor * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13246074Sgabor * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14246074Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15246074Sgabor * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16246074Sgabor * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17246074Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18246074Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19246074Sgabor * SUCH DAMAGE.
20246074Sgabor *
21246074Sgabor * patch - a program to apply diffs to original files
22246074Sgabor *
23246074Sgabor * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24246074Sgabor * behaviour
25246074Sgabor *
26246091Sdelphij * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $
27246091Sdelphij * $FreeBSD$
28246074Sgabor */
29246074Sgabor
30246074Sgabor#include <sys/types.h>
31246074Sgabor#include <sys/file.h>
32246074Sgabor#include <sys/stat.h>
33246074Sgabor#include <sys/mman.h>
34285976Sdelphij#include <sys/wait.h>
35246074Sgabor
36246074Sgabor#include <ctype.h>
37285976Sdelphij#include <errno.h>
38246074Sgabor#include <libgen.h>
39287223Sdelphij#include <paths.h>
40287223Sdelphij#include <spawn.h>
41246074Sgabor#include <stddef.h>
42287223Sdelphij#include <stdint.h>
43246074Sgabor#include <stdio.h>
44246074Sgabor#include <stdlib.h>
45246074Sgabor#include <string.h>
46246074Sgabor#include <unistd.h>
47246074Sgabor
48246074Sgabor#include "common.h"
49246074Sgabor#include "util.h"
50246074Sgabor#include "pch.h"
51246074Sgabor#include "inp.h"
52246074Sgabor
53246074Sgabor
54246074Sgabor/* Input-file-with-indexable-lines abstract type */
55246074Sgabor
56246074Sgaborstatic size_t	i_size;		/* size of the input file */
57246074Sgaborstatic char	*i_womp;	/* plan a buffer for entire file */
58246074Sgaborstatic char	**i_ptr;	/* pointers to lines in i_womp */
59246074Sgaborstatic char	empty_line[] = { '\0' };
60246074Sgabor
61246074Sgaborstatic int	tifd = -1;	/* plan b virtual string array */
62246074Sgaborstatic char	*tibuf[2];	/* plan b buffers */
63246074Sgaborstatic LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
64246074Sgaborstatic LINENUM	lines_per_buf;	/* how many lines per buffer */
65246074Sgaborstatic int	tireclen;	/* length of records in tmp file */
66246074Sgabor
67246074Sgaborstatic bool	rev_in_string(const char *);
68246074Sgaborstatic bool	reallocate_lines(size_t *);
69246074Sgabor
70246074Sgabor/* returns false if insufficient memory */
71246074Sgaborstatic bool	plan_a(const char *);
72246074Sgabor
73246074Sgaborstatic void	plan_b(const char *);
74246074Sgabor
75246074Sgabor/* New patch--prepare to edit another file. */
76246074Sgabor
77246074Sgaborvoid
78246074Sgaborre_input(void)
79246074Sgabor{
80246074Sgabor	if (using_plan_a) {
81246074Sgabor		free(i_ptr);
82246074Sgabor		i_ptr = NULL;
83246074Sgabor		if (i_womp != NULL) {
84246074Sgabor			munmap(i_womp, i_size);
85246074Sgabor			i_womp = NULL;
86246074Sgabor		}
87246074Sgabor		i_size = 0;
88246074Sgabor	} else {
89246074Sgabor		using_plan_a = true;	/* maybe the next one is smaller */
90246074Sgabor		close(tifd);
91246074Sgabor		tifd = -1;
92246074Sgabor		free(tibuf[0]);
93246074Sgabor		free(tibuf[1]);
94246074Sgabor		tibuf[0] = tibuf[1] = NULL;
95246074Sgabor		tiline[0] = tiline[1] = -1;
96246074Sgabor		tireclen = 0;
97246074Sgabor	}
98246074Sgabor}
99246074Sgabor
100246074Sgabor/* Construct the line index, somehow or other. */
101246074Sgabor
102246074Sgaborvoid
103246074Sgaborscan_input(const char *filename)
104246074Sgabor{
105246074Sgabor	if (!plan_a(filename))
106246074Sgabor		plan_b(filename);
107246074Sgabor	if (verbose) {
108246074Sgabor		say("Patching file %s using Plan %s...\n", filename,
109246074Sgabor		    (using_plan_a ? "A" : "B"));
110246074Sgabor	}
111246074Sgabor}
112246074Sgabor
113246074Sgaborstatic bool
114246074Sgaborreallocate_lines(size_t *lines_allocated)
115246074Sgabor{
116246074Sgabor	char	**p;
117246074Sgabor	size_t	new_size;
118246074Sgabor
119246074Sgabor	new_size = *lines_allocated * 3 / 2;
120246074Sgabor	p = realloc(i_ptr, (new_size + 2) * sizeof(char *));
121246074Sgabor	if (p == NULL) {	/* shucks, it was a near thing */
122246074Sgabor		munmap(i_womp, i_size);
123246074Sgabor		i_womp = NULL;
124246074Sgabor		free(i_ptr);
125246074Sgabor		i_ptr = NULL;
126246074Sgabor		*lines_allocated = 0;
127246074Sgabor		return false;
128246074Sgabor	}
129246074Sgabor	*lines_allocated = new_size;
130246074Sgabor	i_ptr = p;
131246074Sgabor	return true;
132246074Sgabor}
133246074Sgabor
134246074Sgabor/* Try keeping everything in memory. */
135246074Sgabor
136246074Sgaborstatic bool
137246074Sgaborplan_a(const char *filename)
138246074Sgabor{
139287223Sdelphij	int		ifd, statfailed, pstat;
140246074Sgabor	char		*p, *s, lbuf[INITLINELEN];
141246074Sgabor	struct stat	filestat;
142246074Sgabor	ptrdiff_t	sz;
143246074Sgabor	size_t		i;
144246074Sgabor	size_t		iline, lines_allocated;
145285976Sdelphij	pid_t		pid;
146246074Sgabor
147246074Sgabor#ifdef DEBUGGING
148246074Sgabor	if (debug & 8)
149246074Sgabor		return false;
150246074Sgabor#endif
151246074Sgabor
152246074Sgabor	if (filename == NULL || *filename == '\0')
153246074Sgabor		return false;
154246074Sgabor
155246074Sgabor	statfailed = stat(filename, &filestat);
156246074Sgabor	if (statfailed && ok_to_create_file) {
157246074Sgabor		if (verbose)
158246074Sgabor			say("(Creating file %s...)\n", filename);
159246074Sgabor
160246074Sgabor		/*
161246074Sgabor		 * in check_patch case, we still display `Creating file' even
162246074Sgabor		 * though we're not. The rule is that -C should be as similar
163246074Sgabor		 * to normal patch behavior as possible
164246074Sgabor		 */
165246074Sgabor		if (check_only)
166246074Sgabor			return true;
167246074Sgabor		makedirs(filename, true);
168246074Sgabor		close(creat(filename, 0666));
169246074Sgabor		statfailed = stat(filename, &filestat);
170246074Sgabor	}
171246074Sgabor	if (statfailed && check_only)
172246074Sgabor		fatal("%s not found, -C mode, can't probe further\n", filename);
173285976Sdelphij	/* For nonexistent or read-only files, look for RCS versions.  */
174285976Sdelphij
175246074Sgabor	if (statfailed ||
176246074Sgabor	    /* No one can write to it.  */
177246074Sgabor	    (filestat.st_mode & 0222) == 0 ||
178246074Sgabor	    /* I can't write to it.  */
179246074Sgabor	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
180285976Sdelphij		char	*filebase, *filedir;
181246074Sgabor		struct stat	cstat;
182287223Sdelphij		char	*tmp_filename1, *tmp_filename2;
183287223Sdelphij		char	*argp[4] = { NULL };
184287223Sdelphij		posix_spawn_file_actions_t file_actions;
185246074Sgabor
186246074Sgabor		tmp_filename1 = strdup(filename);
187246074Sgabor		tmp_filename2 = strdup(filename);
188246074Sgabor		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
189246074Sgabor			fatal("strdupping filename");
190285976Sdelphij
191246074Sgabor		filebase = basename(tmp_filename1);
192246074Sgabor		filedir = dirname(tmp_filename2);
193246074Sgabor
194287223Sdelphij		memset(argp, 0, sizeof(argp));
195287223Sdelphij
196246074Sgabor#define try(f, a1, a2, a3) \
197285976Sdelphij	(snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0)
198246074Sgabor
199246074Sgabor		/*
200246074Sgabor		 * else we can't write to it but it's not under a version
201246074Sgabor		 * control system, so just proceed.
202246074Sgabor		 */
203285976Sdelphij		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
204285976Sdelphij		    try("%s/RCS/%s%s", filedir, filebase, "") ||
205285976Sdelphij		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
206246074Sgabor			if (!statfailed) {
207246074Sgabor				if ((filestat.st_mode & 0222) != 0)
208246074Sgabor					/* The owner can write to it.  */
209246074Sgabor					fatal("file %s seems to be locked "
210285976Sdelphij					    "by somebody else under RCS\n",
211285976Sdelphij					    filename);
212246074Sgabor				/*
213246074Sgabor				 * It might be checked out unlocked.  See if
214246074Sgabor				 * it's safe to check out the default version
215246074Sgabor				 * locked.
216246074Sgabor				 */
217246074Sgabor				if (verbose)
218246074Sgabor					say("Comparing file %s to default "
219285976Sdelphij					    "RCS version...\n", filename);
220285976Sdelphij
221287223Sdelphij				argp[0] = __DECONST(char *, RCSDIFF);
222287223Sdelphij				argp[1] = __DECONST(char *, filename);
223287223Sdelphij				posix_spawn_file_actions_init(&file_actions);
224287223Sdelphij				posix_spawn_file_actions_addopen(&file_actions,
225287223Sdelphij				    STDOUT_FILENO, _PATH_DEVNULL, O_WRONLY, 0);
226287223Sdelphij				if (posix_spawn(&pid, RCSDIFF, &file_actions,
227287223Sdelphij				    NULL, argp, NULL) == 0) {
228287223Sdelphij					pid = waitpid(pid, &pstat, 0);
229287223Sdelphij					if (pid == -1 || WEXITSTATUS(pstat) != 0)
230287223Sdelphij						fatal("can't check out file %s: "
231287223Sdelphij						    "differs from default RCS version\n",
232287223Sdelphij						    filename);
233287223Sdelphij				} else
234287223Sdelphij					fatal("posix_spawn: %s\n", strerror(errno));
235287223Sdelphij				posix_spawn_file_actions_destroy(&file_actions);
236246074Sgabor			}
237285976Sdelphij
238246074Sgabor			if (verbose)
239285976Sdelphij				say("Checking out file %s from RCS...\n",
240285976Sdelphij				    filename);
241285976Sdelphij
242287223Sdelphij			argp[0] = __DECONST(char *, CHECKOUT);
243287223Sdelphij			argp[1] = __DECONST(char *, "-l");
244287223Sdelphij			argp[2] = __DECONST(char *, filename);
245287223Sdelphij			if (posix_spawn(&pid, CHECKOUT, NULL, NULL, argp,
246287223Sdelphij			    NULL) == 0) {
247287223Sdelphij				pid = waitpid(pid, &pstat, 0);
248287223Sdelphij				if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
249287223Sdelphij				    stat(filename, &filestat))
250287223Sdelphij					fatal("can't check out file %s from RCS\n",
251287223Sdelphij					    filename);
252287223Sdelphij			} else
253287223Sdelphij				fatal("posix_spawn: %s\n", strerror(errno));
254285976Sdelphij		} else if (statfailed) {
255285976Sdelphij			fatal("can't find %s\n", filename);
256246074Sgabor		}
257285976Sdelphij		free(tmp_filename1);
258285976Sdelphij		free(tmp_filename2);
259246074Sgabor	}
260285976Sdelphij
261246074Sgabor	filemode = filestat.st_mode;
262246074Sgabor	if (!S_ISREG(filemode))
263246074Sgabor		fatal("%s is not a normal file--can't patch\n", filename);
264246074Sgabor	if ((uint64_t)filestat.st_size > SIZE_MAX) {
265246074Sgabor		say("block too large to mmap\n");
266246074Sgabor		return false;
267246074Sgabor	}
268246074Sgabor	i_size = (size_t)filestat.st_size;
269246074Sgabor	if (out_of_mem) {
270246074Sgabor		set_hunkmax();	/* make sure dynamic arrays are allocated */
271246074Sgabor		out_of_mem = false;
272246074Sgabor		return false;	/* force plan b because plan a bombed */
273246074Sgabor	}
274246074Sgabor	if ((ifd = open(filename, O_RDONLY)) < 0)
275246074Sgabor		pfatal("can't open file %s", filename);
276246074Sgabor
277246074Sgabor	if (i_size) {
278246074Sgabor		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
279246074Sgabor		if (i_womp == MAP_FAILED) {
280246074Sgabor			perror("mmap failed");
281246074Sgabor			i_womp = NULL;
282246074Sgabor			close(ifd);
283246074Sgabor			return false;
284246074Sgabor		}
285246074Sgabor	} else {
286246074Sgabor		i_womp = NULL;
287246074Sgabor	}
288246074Sgabor
289246074Sgabor	close(ifd);
290246074Sgabor	if (i_size)
291246074Sgabor		madvise(i_womp, i_size, MADV_SEQUENTIAL);
292246074Sgabor
293246074Sgabor	/* estimate the number of lines */
294246074Sgabor	lines_allocated = i_size / 25;
295246074Sgabor	if (lines_allocated < 100)
296246074Sgabor		lines_allocated = 100;
297246074Sgabor
298246074Sgabor	if (!reallocate_lines(&lines_allocated))
299246074Sgabor		return false;
300246074Sgabor
301246074Sgabor	/* now scan the buffer and build pointer array */
302246074Sgabor	iline = 1;
303246074Sgabor	i_ptr[iline] = i_womp;
304246074Sgabor	/* test for NUL too, to maintain the behavior of the original code */
305246074Sgabor	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
306246074Sgabor		if (*s == '\n') {
307246074Sgabor			if (iline == lines_allocated) {
308246074Sgabor				if (!reallocate_lines(&lines_allocated))
309246074Sgabor					return false;
310246074Sgabor			}
311246074Sgabor			/* these are NOT NUL terminated */
312246074Sgabor			i_ptr[++iline] = s + 1;
313246074Sgabor		}
314246074Sgabor	}
315246074Sgabor	/* if the last line contains no EOL, append one */
316246074Sgabor	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
317246074Sgabor		last_line_missing_eol = true;
318246074Sgabor		/* fix last line */
319246074Sgabor		sz = s - i_ptr[iline];
320246074Sgabor		p = malloc(sz + 1);
321246074Sgabor		if (p == NULL) {
322246074Sgabor			free(i_ptr);
323246074Sgabor			i_ptr = NULL;
324246074Sgabor			munmap(i_womp, i_size);
325246074Sgabor			i_womp = NULL;
326246074Sgabor			return false;
327246074Sgabor		}
328246074Sgabor
329246074Sgabor		memcpy(p, i_ptr[iline], sz);
330246074Sgabor		p[sz] = '\n';
331246074Sgabor		i_ptr[iline] = p;
332246074Sgabor		/* count the extra line and make it point to some valid mem */
333246074Sgabor		i_ptr[++iline] = empty_line;
334246074Sgabor	} else
335246074Sgabor		last_line_missing_eol = false;
336246074Sgabor
337246074Sgabor	input_lines = iline - 1;
338246074Sgabor
339246074Sgabor	/* now check for revision, if any */
340246074Sgabor
341246074Sgabor	if (revision != NULL) {
342246074Sgabor		if (!rev_in_string(i_womp)) {
343246074Sgabor			if (force) {
344246074Sgabor				if (verbose)
345246074Sgabor					say("Warning: this file doesn't appear "
346246074Sgabor					    "to be the %s version--patching anyway.\n",
347246074Sgabor					    revision);
348246074Sgabor			} else if (batch) {
349246074Sgabor				fatal("this file doesn't appear to be the "
350246074Sgabor				    "%s version--aborting.\n",
351246074Sgabor				    revision);
352246074Sgabor			} else {
353246074Sgabor				ask("This file doesn't appear to be the "
354246074Sgabor				    "%s version--patch anyway? [n] ",
355246074Sgabor				    revision);
356246074Sgabor				if (*buf != 'y')
357246074Sgabor					fatal("aborted\n");
358246074Sgabor			}
359246074Sgabor		} else if (verbose)
360246074Sgabor			say("Good.  This file appears to be the %s version.\n",
361246074Sgabor			    revision);
362246074Sgabor	}
363246074Sgabor	return true;		/* plan a will work */
364246074Sgabor}
365246074Sgabor
366246074Sgabor/* Keep (virtually) nothing in memory. */
367246074Sgabor
368246074Sgaborstatic void
369246074Sgaborplan_b(const char *filename)
370246074Sgabor{
371246074Sgabor	FILE	*ifp;
372246074Sgabor	size_t	i = 0, j, maxlen = 1;
373246074Sgabor	char	*p;
374246074Sgabor	bool	found_revision = (revision == NULL);
375246074Sgabor
376246074Sgabor	using_plan_a = false;
377246074Sgabor	if ((ifp = fopen(filename, "r")) == NULL)
378246074Sgabor		pfatal("can't open file %s", filename);
379246074Sgabor	unlink(TMPINNAME);
380246074Sgabor	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
381246074Sgabor		pfatal("can't open file %s", TMPINNAME);
382246074Sgabor	while (fgets(buf, buf_size, ifp) != NULL) {
383246074Sgabor		if (revision != NULL && !found_revision && rev_in_string(buf))
384246074Sgabor			found_revision = true;
385246074Sgabor		if ((i = strlen(buf)) > maxlen)
386246074Sgabor			maxlen = i;	/* find longest line */
387246074Sgabor	}
388246074Sgabor	last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
389246074Sgabor	if (last_line_missing_eol && maxlen == i)
390246074Sgabor		maxlen++;
391246074Sgabor
392246074Sgabor	if (revision != NULL) {
393246074Sgabor		if (!found_revision) {
394246074Sgabor			if (force) {
395246074Sgabor				if (verbose)
396246074Sgabor					say("Warning: this file doesn't appear "
397246074Sgabor					    "to be the %s version--patching anyway.\n",
398246074Sgabor					    revision);
399246074Sgabor			} else if (batch) {
400246074Sgabor				fatal("this file doesn't appear to be the "
401246074Sgabor				    "%s version--aborting.\n",
402246074Sgabor				    revision);
403246074Sgabor			} else {
404246074Sgabor				ask("This file doesn't appear to be the %s "
405246074Sgabor				    "version--patch anyway? [n] ",
406246074Sgabor				    revision);
407246074Sgabor				if (*buf != 'y')
408246074Sgabor					fatal("aborted\n");
409246074Sgabor			}
410246074Sgabor		} else if (verbose)
411246074Sgabor			say("Good.  This file appears to be the %s version.\n",
412246074Sgabor			    revision);
413246074Sgabor	}
414246074Sgabor	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
415246074Sgabor	lines_per_buf = BUFFERSIZE / maxlen;
416246074Sgabor	tireclen = maxlen;
417246074Sgabor	tibuf[0] = malloc(BUFFERSIZE + 1);
418246074Sgabor	if (tibuf[0] == NULL)
419246074Sgabor		fatal("out of memory\n");
420246074Sgabor	tibuf[1] = malloc(BUFFERSIZE + 1);
421246074Sgabor	if (tibuf[1] == NULL)
422246074Sgabor		fatal("out of memory\n");
423246074Sgabor	for (i = 1;; i++) {
424246074Sgabor		p = tibuf[0] + maxlen * (i % lines_per_buf);
425246074Sgabor		if (i % lines_per_buf == 0)	/* new block */
426246074Sgabor			if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
427246074Sgabor				pfatal("can't write temp file");
428246074Sgabor		if (fgets(p, maxlen + 1, ifp) == NULL) {
429246074Sgabor			input_lines = i - 1;
430246074Sgabor			if (i % lines_per_buf != 0)
431246074Sgabor				if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
432246074Sgabor					pfatal("can't write temp file");
433246074Sgabor			break;
434246074Sgabor		}
435246074Sgabor		j = strlen(p);
436246074Sgabor		/* These are '\n' terminated strings, so no need to add a NUL */
437246074Sgabor		if (j == 0 || p[j - 1] != '\n')
438246074Sgabor			p[j] = '\n';
439246074Sgabor	}
440246074Sgabor	fclose(ifp);
441246074Sgabor	close(tifd);
442246074Sgabor	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
443246074Sgabor		pfatal("can't reopen file %s", TMPINNAME);
444246074Sgabor}
445246074Sgabor
446246074Sgabor/*
447246074Sgabor * Fetch a line from the input file, \n terminated, not necessarily \0.
448246074Sgabor */
449246074Sgaborchar *
450246074Sgaborifetch(LINENUM line, int whichbuf)
451246074Sgabor{
452246074Sgabor	if (line < 1 || line > input_lines) {
453246074Sgabor		if (warn_on_invalid_line) {
454246074Sgabor			say("No such line %ld in input file, ignoring\n", line);
455246074Sgabor			warn_on_invalid_line = false;
456246074Sgabor		}
457246074Sgabor		return NULL;
458246074Sgabor	}
459246074Sgabor	if (using_plan_a)
460246074Sgabor		return i_ptr[line];
461246074Sgabor	else {
462246074Sgabor		LINENUM	offline = line % lines_per_buf;
463246074Sgabor		LINENUM	baseline = line - offline;
464246074Sgabor
465246074Sgabor		if (tiline[0] == baseline)
466246074Sgabor			whichbuf = 0;
467246074Sgabor		else if (tiline[1] == baseline)
468246074Sgabor			whichbuf = 1;
469246074Sgabor		else {
470246074Sgabor			tiline[whichbuf] = baseline;
471246074Sgabor
472246074Sgabor			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
473246074Sgabor			    BUFFERSIZE), SEEK_SET) < 0)
474246074Sgabor				pfatal("cannot seek in the temporary input file");
475246074Sgabor
476246074Sgabor			if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
477246074Sgabor				pfatal("error reading tmp file %s", TMPINNAME);
478246074Sgabor		}
479246074Sgabor		return tibuf[whichbuf] + (tireclen * offline);
480246074Sgabor	}
481246074Sgabor}
482246074Sgabor
483246074Sgabor/*
484246074Sgabor * True if the string argument contains the revision number we want.
485246074Sgabor */
486246074Sgaborstatic bool
487246074Sgaborrev_in_string(const char *string)
488246074Sgabor{
489246074Sgabor	const char	*s;
490246074Sgabor	size_t		patlen;
491246074Sgabor
492246074Sgabor	if (revision == NULL)
493246074Sgabor		return true;
494246074Sgabor	patlen = strlen(revision);
495246074Sgabor	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
496246074Sgabor		return true;
497246074Sgabor	for (s = string; *s; s++) {
498246074Sgabor		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
499246074Sgabor		    isspace((unsigned char)s[patlen + 1])) {
500246074Sgabor			return true;
501246074Sgabor		}
502246074Sgabor	}
503246074Sgabor	return false;
504246074Sgabor}
505