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