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>
34285979Sdelphij#include <sys/wait.h>
35246074Sgabor
36246074Sgabor#include <ctype.h>
37285979Sdelphij#include <errno.h>
38246074Sgabor#include <libgen.h>
39246074Sgabor#include <limits.h>
40246074Sgabor#include <stddef.h>
41246074Sgabor#include <stdio.h>
42246074Sgabor#include <stdlib.h>
43246074Sgabor#include <string.h>
44246074Sgabor#include <unistd.h>
45246074Sgabor
46246074Sgabor#include "common.h"
47246074Sgabor#include "util.h"
48246074Sgabor#include "pch.h"
49246074Sgabor#include "inp.h"
50246074Sgabor
51246074Sgabor
52246074Sgabor/* Input-file-with-indexable-lines abstract type */
53246074Sgabor
54246074Sgaborstatic size_t	i_size;		/* size of the input file */
55246074Sgaborstatic char	*i_womp;	/* plan a buffer for entire file */
56246074Sgaborstatic char	**i_ptr;	/* pointers to lines in i_womp */
57246074Sgaborstatic char	empty_line[] = { '\0' };
58246074Sgabor
59246074Sgaborstatic int	tifd = -1;	/* plan b virtual string array */
60246074Sgaborstatic char	*tibuf[2];	/* plan b buffers */
61246074Sgaborstatic LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
62246074Sgaborstatic LINENUM	lines_per_buf;	/* how many lines per buffer */
63246074Sgaborstatic int	tireclen;	/* length of records in tmp file */
64246074Sgabor
65246074Sgaborstatic bool	rev_in_string(const char *);
66246074Sgaborstatic bool	reallocate_lines(size_t *);
67246074Sgabor
68246074Sgabor/* returns false if insufficient memory */
69246074Sgaborstatic bool	plan_a(const char *);
70246074Sgabor
71246074Sgaborstatic void	plan_b(const char *);
72246074Sgabor
73246074Sgabor/* New patch--prepare to edit another file. */
74246074Sgabor
75246074Sgaborvoid
76246074Sgaborre_input(void)
77246074Sgabor{
78246074Sgabor	if (using_plan_a) {
79246074Sgabor		free(i_ptr);
80246074Sgabor		i_ptr = NULL;
81246074Sgabor		if (i_womp != NULL) {
82246074Sgabor			munmap(i_womp, i_size);
83246074Sgabor			i_womp = NULL;
84246074Sgabor		}
85246074Sgabor		i_size = 0;
86246074Sgabor	} else {
87246074Sgabor		using_plan_a = true;	/* maybe the next one is smaller */
88246074Sgabor		close(tifd);
89246074Sgabor		tifd = -1;
90246074Sgabor		free(tibuf[0]);
91246074Sgabor		free(tibuf[1]);
92246074Sgabor		tibuf[0] = tibuf[1] = NULL;
93246074Sgabor		tiline[0] = tiline[1] = -1;
94246074Sgabor		tireclen = 0;
95246074Sgabor	}
96246074Sgabor}
97246074Sgabor
98246074Sgabor/* Construct the line index, somehow or other. */
99246074Sgabor
100246074Sgaborvoid
101246074Sgaborscan_input(const char *filename)
102246074Sgabor{
103246074Sgabor	if (!plan_a(filename))
104246074Sgabor		plan_b(filename);
105246074Sgabor	if (verbose) {
106246074Sgabor		say("Patching file %s using Plan %s...\n", filename,
107246074Sgabor		    (using_plan_a ? "A" : "B"));
108246074Sgabor	}
109246074Sgabor}
110246074Sgabor
111246074Sgaborstatic bool
112246074Sgaborreallocate_lines(size_t *lines_allocated)
113246074Sgabor{
114246074Sgabor	char	**p;
115246074Sgabor	size_t	new_size;
116246074Sgabor
117246074Sgabor	new_size = *lines_allocated * 3 / 2;
118246074Sgabor	p = realloc(i_ptr, (new_size + 2) * sizeof(char *));
119246074Sgabor	if (p == NULL) {	/* shucks, it was a near thing */
120246074Sgabor		munmap(i_womp, i_size);
121246074Sgabor		i_womp = NULL;
122246074Sgabor		free(i_ptr);
123246074Sgabor		i_ptr = NULL;
124246074Sgabor		*lines_allocated = 0;
125246074Sgabor		return false;
126246074Sgabor	}
127246074Sgabor	*lines_allocated = new_size;
128246074Sgabor	i_ptr = p;
129246074Sgabor	return true;
130246074Sgabor}
131246074Sgabor
132246074Sgabor/* Try keeping everything in memory. */
133246074Sgabor
134246074Sgaborstatic bool
135246074Sgaborplan_a(const char *filename)
136246074Sgabor{
137285979Sdelphij	int		ifd, statfailed, devnull, pstat;
138246074Sgabor	char		*p, *s, lbuf[INITLINELEN];
139246074Sgabor	struct stat	filestat;
140246074Sgabor	ptrdiff_t	sz;
141246074Sgabor	size_t		i;
142246074Sgabor	size_t		iline, lines_allocated;
143285979Sdelphij	pid_t		pid;
144285979Sdelphij	char		*argp[4] = {NULL};
145246074Sgabor
146246074Sgabor#ifdef DEBUGGING
147246074Sgabor	if (debug & 8)
148246074Sgabor		return false;
149246074Sgabor#endif
150246074Sgabor
151246074Sgabor	if (filename == NULL || *filename == '\0')
152246074Sgabor		return false;
153246074Sgabor
154246074Sgabor	statfailed = stat(filename, &filestat);
155246074Sgabor	if (statfailed && ok_to_create_file) {
156246074Sgabor		if (verbose)
157246074Sgabor			say("(Creating file %s...)\n", filename);
158246074Sgabor
159246074Sgabor		/*
160246074Sgabor		 * in check_patch case, we still display `Creating file' even
161246074Sgabor		 * though we're not. The rule is that -C should be as similar
162246074Sgabor		 * to normal patch behavior as possible
163246074Sgabor		 */
164246074Sgabor		if (check_only)
165246074Sgabor			return true;
166246074Sgabor		makedirs(filename, true);
167246074Sgabor		close(creat(filename, 0666));
168246074Sgabor		statfailed = stat(filename, &filestat);
169246074Sgabor	}
170246074Sgabor	if (statfailed && check_only)
171246074Sgabor		fatal("%s not found, -C mode, can't probe further\n", filename);
172285979Sdelphij	/* For nonexistent or read-only files, look for RCS versions.  */
173285979Sdelphij
174246074Sgabor	if (statfailed ||
175246074Sgabor	    /* No one can write to it.  */
176246074Sgabor	    (filestat.st_mode & 0222) == 0 ||
177246074Sgabor	    /* I can't write to it.  */
178246074Sgabor	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
179285979Sdelphij		char	*filebase, *filedir;
180246074Sgabor		struct stat	cstat;
181246074Sgabor		char *tmp_filename1, *tmp_filename2;
182246074Sgabor
183246074Sgabor		tmp_filename1 = strdup(filename);
184246074Sgabor		tmp_filename2 = strdup(filename);
185246074Sgabor		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
186246074Sgabor			fatal("strdupping filename");
187285979Sdelphij
188246074Sgabor		filebase = basename(tmp_filename1);
189246074Sgabor		filedir = dirname(tmp_filename2);
190246074Sgabor
191246074Sgabor#define try(f, a1, a2, a3) \
192285979Sdelphij	(snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0)
193246074Sgabor
194246074Sgabor		/*
195246074Sgabor		 * else we can't write to it but it's not under a version
196246074Sgabor		 * control system, so just proceed.
197246074Sgabor		 */
198285979Sdelphij		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
199285979Sdelphij		    try("%s/RCS/%s%s", filedir, filebase, "") ||
200285979Sdelphij		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
201246074Sgabor			if (!statfailed) {
202246074Sgabor				if ((filestat.st_mode & 0222) != 0)
203246074Sgabor					/* The owner can write to it.  */
204246074Sgabor					fatal("file %s seems to be locked "
205285979Sdelphij					    "by somebody else under RCS\n",
206285979Sdelphij					    filename);
207246074Sgabor				/*
208246074Sgabor				 * It might be checked out unlocked.  See if
209246074Sgabor				 * it's safe to check out the default version
210246074Sgabor				 * locked.
211246074Sgabor				 */
212246074Sgabor				if (verbose)
213246074Sgabor					say("Comparing file %s to default "
214285979Sdelphij					    "RCS version...\n", filename);
215285979Sdelphij
216285979Sdelphij				switch (pid = fork()) {
217285979Sdelphij				case -1:
218285979Sdelphij					fatal("can't fork: %s\n",
219285979Sdelphij					    strerror(errno));
220285979Sdelphij				case 0:
221285979Sdelphij					devnull = open("/dev/null", O_RDONLY);
222285979Sdelphij					if (devnull == -1) {
223285979Sdelphij						fatal("can't open /dev/null: %s",
224285979Sdelphij						    strerror(errno));
225285979Sdelphij					}
226285979Sdelphij					(void)dup2(devnull, STDOUT_FILENO);
227285979Sdelphij					argp[0] = strdup(RCSDIFF);
228285979Sdelphij					argp[1] = strdup(filename);
229285979Sdelphij					execv(RCSDIFF, argp);
230285979Sdelphij					exit(127);
231285979Sdelphij				}
232285979Sdelphij				pid = waitpid(pid, &pstat, 0);
233285979Sdelphij				if (pid == -1 || WEXITSTATUS(pstat) != 0) {
234246074Sgabor					fatal("can't check out file %s: "
235285979Sdelphij					    "differs from default RCS version\n",
236285979Sdelphij					    filename);
237285979Sdelphij				}
238246074Sgabor			}
239285979Sdelphij
240246074Sgabor			if (verbose)
241285979Sdelphij				say("Checking out file %s from RCS...\n",
242285979Sdelphij				    filename);
243285979Sdelphij
244285979Sdelphij			switch (pid = fork()) {
245285979Sdelphij			case -1:
246285979Sdelphij				fatal("can't fork: %s\n", strerror(errno));
247285979Sdelphij			case 0:
248285979Sdelphij				argp[0] = strdup(CHECKOUT);
249285979Sdelphij				argp[1] = strdup("-l");
250285979Sdelphij				argp[2] = strdup(filename);
251285979Sdelphij				execv(CHECKOUT, argp);
252285979Sdelphij				exit(127);
253285979Sdelphij			}
254285979Sdelphij			pid = waitpid(pid, &pstat, 0);
255285979Sdelphij			if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
256285979Sdelphij			    stat(filename, &filestat)) {
257285979Sdelphij				fatal("can't check out file %s from RCS\n",
258285979Sdelphij				    filename);
259285979Sdelphij			}
260285979Sdelphij		} else if (statfailed) {
261285979Sdelphij			fatal("can't find %s\n", filename);
262246074Sgabor		}
263285979Sdelphij		free(tmp_filename1);
264285979Sdelphij		free(tmp_filename2);
265246074Sgabor	}
266285979Sdelphij
267246074Sgabor	filemode = filestat.st_mode;
268246074Sgabor	if (!S_ISREG(filemode))
269246074Sgabor		fatal("%s is not a normal file--can't patch\n", filename);
270246074Sgabor	if ((uint64_t)filestat.st_size > SIZE_MAX) {
271246074Sgabor		say("block too large to mmap\n");
272246074Sgabor		return false;
273246074Sgabor	}
274246074Sgabor	i_size = (size_t)filestat.st_size;
275246074Sgabor	if (out_of_mem) {
276246074Sgabor		set_hunkmax();	/* make sure dynamic arrays are allocated */
277246074Sgabor		out_of_mem = false;
278246074Sgabor		return false;	/* force plan b because plan a bombed */
279246074Sgabor	}
280246074Sgabor	if ((ifd = open(filename, O_RDONLY)) < 0)
281246074Sgabor		pfatal("can't open file %s", filename);
282246074Sgabor
283246074Sgabor	if (i_size) {
284246074Sgabor		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
285246074Sgabor		if (i_womp == MAP_FAILED) {
286246074Sgabor			perror("mmap failed");
287246074Sgabor			i_womp = NULL;
288246074Sgabor			close(ifd);
289246074Sgabor			return false;
290246074Sgabor		}
291246074Sgabor	} else {
292246074Sgabor		i_womp = NULL;
293246074Sgabor	}
294246074Sgabor
295246074Sgabor	close(ifd);
296246074Sgabor	if (i_size)
297246074Sgabor		madvise(i_womp, i_size, MADV_SEQUENTIAL);
298246074Sgabor
299246074Sgabor	/* estimate the number of lines */
300246074Sgabor	lines_allocated = i_size / 25;
301246074Sgabor	if (lines_allocated < 100)
302246074Sgabor		lines_allocated = 100;
303246074Sgabor
304246074Sgabor	if (!reallocate_lines(&lines_allocated))
305246074Sgabor		return false;
306246074Sgabor
307246074Sgabor	/* now scan the buffer and build pointer array */
308246074Sgabor	iline = 1;
309246074Sgabor	i_ptr[iline] = i_womp;
310246074Sgabor	/* test for NUL too, to maintain the behavior of the original code */
311246074Sgabor	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
312246074Sgabor		if (*s == '\n') {
313246074Sgabor			if (iline == lines_allocated) {
314246074Sgabor				if (!reallocate_lines(&lines_allocated))
315246074Sgabor					return false;
316246074Sgabor			}
317246074Sgabor			/* these are NOT NUL terminated */
318246074Sgabor			i_ptr[++iline] = s + 1;
319246074Sgabor		}
320246074Sgabor	}
321246074Sgabor	/* if the last line contains no EOL, append one */
322246074Sgabor	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
323246074Sgabor		last_line_missing_eol = true;
324246074Sgabor		/* fix last line */
325246074Sgabor		sz = s - i_ptr[iline];
326246074Sgabor		p = malloc(sz + 1);
327246074Sgabor		if (p == NULL) {
328246074Sgabor			free(i_ptr);
329246074Sgabor			i_ptr = NULL;
330246074Sgabor			munmap(i_womp, i_size);
331246074Sgabor			i_womp = NULL;
332246074Sgabor			return false;
333246074Sgabor		}
334246074Sgabor
335246074Sgabor		memcpy(p, i_ptr[iline], sz);
336246074Sgabor		p[sz] = '\n';
337246074Sgabor		i_ptr[iline] = p;
338246074Sgabor		/* count the extra line and make it point to some valid mem */
339246074Sgabor		i_ptr[++iline] = empty_line;
340246074Sgabor	} else
341246074Sgabor		last_line_missing_eol = false;
342246074Sgabor
343246074Sgabor	input_lines = iline - 1;
344246074Sgabor
345246074Sgabor	/* now check for revision, if any */
346246074Sgabor
347246074Sgabor	if (revision != NULL) {
348246074Sgabor		if (!rev_in_string(i_womp)) {
349246074Sgabor			if (force) {
350246074Sgabor				if (verbose)
351246074Sgabor					say("Warning: this file doesn't appear "
352246074Sgabor					    "to be the %s version--patching anyway.\n",
353246074Sgabor					    revision);
354246074Sgabor			} else if (batch) {
355246074Sgabor				fatal("this file doesn't appear to be the "
356246074Sgabor				    "%s version--aborting.\n",
357246074Sgabor				    revision);
358246074Sgabor			} else {
359246074Sgabor				ask("This file doesn't appear to be the "
360246074Sgabor				    "%s version--patch anyway? [n] ",
361246074Sgabor				    revision);
362246074Sgabor				if (*buf != 'y')
363246074Sgabor					fatal("aborted\n");
364246074Sgabor			}
365246074Sgabor		} else if (verbose)
366246074Sgabor			say("Good.  This file appears to be the %s version.\n",
367246074Sgabor			    revision);
368246074Sgabor	}
369246074Sgabor	return true;		/* plan a will work */
370246074Sgabor}
371246074Sgabor
372246074Sgabor/* Keep (virtually) nothing in memory. */
373246074Sgabor
374246074Sgaborstatic void
375246074Sgaborplan_b(const char *filename)
376246074Sgabor{
377246074Sgabor	FILE	*ifp;
378246074Sgabor	size_t	i = 0, j, maxlen = 1;
379246074Sgabor	char	*p;
380246074Sgabor	bool	found_revision = (revision == NULL);
381246074Sgabor
382246074Sgabor	using_plan_a = false;
383246074Sgabor	if ((ifp = fopen(filename, "r")) == NULL)
384246074Sgabor		pfatal("can't open file %s", filename);
385246074Sgabor	unlink(TMPINNAME);
386246074Sgabor	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
387246074Sgabor		pfatal("can't open file %s", TMPINNAME);
388246074Sgabor	while (fgets(buf, buf_size, ifp) != NULL) {
389246074Sgabor		if (revision != NULL && !found_revision && rev_in_string(buf))
390246074Sgabor			found_revision = true;
391246074Sgabor		if ((i = strlen(buf)) > maxlen)
392246074Sgabor			maxlen = i;	/* find longest line */
393246074Sgabor	}
394246074Sgabor	last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
395246074Sgabor	if (last_line_missing_eol && maxlen == i)
396246074Sgabor		maxlen++;
397246074Sgabor
398246074Sgabor	if (revision != NULL) {
399246074Sgabor		if (!found_revision) {
400246074Sgabor			if (force) {
401246074Sgabor				if (verbose)
402246074Sgabor					say("Warning: this file doesn't appear "
403246074Sgabor					    "to be the %s version--patching anyway.\n",
404246074Sgabor					    revision);
405246074Sgabor			} else if (batch) {
406246074Sgabor				fatal("this file doesn't appear to be the "
407246074Sgabor				    "%s version--aborting.\n",
408246074Sgabor				    revision);
409246074Sgabor			} else {
410246074Sgabor				ask("This file doesn't appear to be the %s "
411246074Sgabor				    "version--patch anyway? [n] ",
412246074Sgabor				    revision);
413246074Sgabor				if (*buf != 'y')
414246074Sgabor					fatal("aborted\n");
415246074Sgabor			}
416246074Sgabor		} else if (verbose)
417246074Sgabor			say("Good.  This file appears to be the %s version.\n",
418246074Sgabor			    revision);
419246074Sgabor	}
420246074Sgabor	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
421246074Sgabor	lines_per_buf = BUFFERSIZE / maxlen;
422246074Sgabor	tireclen = maxlen;
423246074Sgabor	tibuf[0] = malloc(BUFFERSIZE + 1);
424246074Sgabor	if (tibuf[0] == NULL)
425246074Sgabor		fatal("out of memory\n");
426246074Sgabor	tibuf[1] = malloc(BUFFERSIZE + 1);
427246074Sgabor	if (tibuf[1] == NULL)
428246074Sgabor		fatal("out of memory\n");
429246074Sgabor	for (i = 1;; i++) {
430246074Sgabor		p = tibuf[0] + maxlen * (i % lines_per_buf);
431246074Sgabor		if (i % lines_per_buf == 0)	/* new block */
432246074Sgabor			if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
433246074Sgabor				pfatal("can't write temp file");
434246074Sgabor		if (fgets(p, maxlen + 1, ifp) == NULL) {
435246074Sgabor			input_lines = i - 1;
436246074Sgabor			if (i % lines_per_buf != 0)
437246074Sgabor				if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
438246074Sgabor					pfatal("can't write temp file");
439246074Sgabor			break;
440246074Sgabor		}
441246074Sgabor		j = strlen(p);
442246074Sgabor		/* These are '\n' terminated strings, so no need to add a NUL */
443246074Sgabor		if (j == 0 || p[j - 1] != '\n')
444246074Sgabor			p[j] = '\n';
445246074Sgabor	}
446246074Sgabor	fclose(ifp);
447246074Sgabor	close(tifd);
448246074Sgabor	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
449246074Sgabor		pfatal("can't reopen file %s", TMPINNAME);
450246074Sgabor}
451246074Sgabor
452246074Sgabor/*
453246074Sgabor * Fetch a line from the input file, \n terminated, not necessarily \0.
454246074Sgabor */
455246074Sgaborchar *
456246074Sgaborifetch(LINENUM line, int whichbuf)
457246074Sgabor{
458246074Sgabor	if (line < 1 || line > input_lines) {
459246074Sgabor		if (warn_on_invalid_line) {
460246074Sgabor			say("No such line %ld in input file, ignoring\n", line);
461246074Sgabor			warn_on_invalid_line = false;
462246074Sgabor		}
463246074Sgabor		return NULL;
464246074Sgabor	}
465246074Sgabor	if (using_plan_a)
466246074Sgabor		return i_ptr[line];
467246074Sgabor	else {
468246074Sgabor		LINENUM	offline = line % lines_per_buf;
469246074Sgabor		LINENUM	baseline = line - offline;
470246074Sgabor
471246074Sgabor		if (tiline[0] == baseline)
472246074Sgabor			whichbuf = 0;
473246074Sgabor		else if (tiline[1] == baseline)
474246074Sgabor			whichbuf = 1;
475246074Sgabor		else {
476246074Sgabor			tiline[whichbuf] = baseline;
477246074Sgabor
478246074Sgabor			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
479246074Sgabor			    BUFFERSIZE), SEEK_SET) < 0)
480246074Sgabor				pfatal("cannot seek in the temporary input file");
481246074Sgabor
482246074Sgabor			if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
483246074Sgabor				pfatal("error reading tmp file %s", TMPINNAME);
484246074Sgabor		}
485246074Sgabor		return tibuf[whichbuf] + (tireclen * offline);
486246074Sgabor	}
487246074Sgabor}
488246074Sgabor
489246074Sgabor/*
490246074Sgabor * True if the string argument contains the revision number we want.
491246074Sgabor */
492246074Sgaborstatic bool
493246074Sgaborrev_in_string(const char *string)
494246074Sgabor{
495246074Sgabor	const char	*s;
496246074Sgabor	size_t		patlen;
497246074Sgabor
498246074Sgabor	if (revision == NULL)
499246074Sgabor		return true;
500246074Sgabor	patlen = strlen(revision);
501246074Sgabor	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
502246074Sgabor		return true;
503246074Sgabor	for (s = string; *s; s++) {
504246074Sgabor		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
505246074Sgabor		    isspace((unsigned char)s[patlen + 1])) {
506246074Sgabor			return true;
507246074Sgabor		}
508246074Sgabor	}
509246074Sgabor	return false;
510246074Sgabor}
511