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