xref: /freebsd-src/usr.bin/diff/diffreg.c (revision e43df07e3725ef6d14a2ca635598c18295b1b481)
1 /*	$OpenBSD: diffreg.c,v 1.93 2019/06/28 13:35:00 deraadt Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code and documentation must retain the above
13  *    copyright notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed or owned by Caldera
20  *	International, Inc.
21  * 4. Neither the name of Caldera International, Inc. nor the names of other
22  *    contributors may be used to endorse or promote products derived from
23  *    this software without specific prior written permission.
24  *
25  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
26  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
30  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*-
39  * Copyright (c) 1991, 1993
40  *	The Regents of the University of California.  All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)diffreg.c   8.1 (Berkeley) 6/6/93
67  */
68 
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71 
72 #include <sys/capsicum.h>
73 #include <sys/stat.h>
74 
75 #include <capsicum_helpers.h>
76 #include <ctype.h>
77 #include <err.h>
78 #include <errno.h>
79 #include <fcntl.h>
80 #include <paths.h>
81 #include <regex.h>
82 #include <stdbool.h>
83 #include <stddef.h>
84 #include <stdint.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 
89 #include "pr.h"
90 #include "diff.h"
91 #include "xmalloc.h"
92 
93 /*
94  * diff - compare two files.
95  */
96 
97 /*
98  *	Uses an algorithm due to Harold Stone, which finds a pair of longest
99  *	identical subsequences in the two files.
100  *
101  *	The major goal is to generate the match vector J. J[i] is the index of
102  *	the line in file1 corresponding to line i file0. J[i] = 0 if there is no
103  *	such line in file1.
104  *
105  *	Lines are hashed so as to work in core. All potential matches are
106  *	located by sorting the lines of each file on the hash (called
107  *	``value''). In particular, this collects the equivalence classes in
108  *	file1 together. Subroutine equiv replaces the value of each line in
109  *	file0 by the index of the first element of its matching equivalence in
110  *	(the reordered) file1. To save space equiv squeezes file1 into a single
111  *	array member in which the equivalence classes are simply concatenated,
112  *	except that their first members are flagged by changing sign.
113  *
114  *	Next the indices that point into member are unsorted into array class
115  *	according to the original order of file0.
116  *
117  *	The cleverness lies in routine stone. This marches through the lines of
118  *	file0, developing a vector klist of "k-candidates". At step i
119  *	a k-candidate is a matched pair of lines x,y (x in file0 y in file1)
120  *	such that there is a common subsequence of length k between the first
121  *	i lines of file0 and the first y lines of file1, but there is no such
122  *	subsequence for any smaller y. x is the earliest possible mate to y that
123  *	occurs in such a subsequence.
124  *
125  *	Whenever any of the members of the equivalence class of lines in file1
126  *	matable to a line in file0 has serial number less than the y of some
127  *	k-candidate, that k-candidate with the smallest such y is replaced. The
128  *	new k-candidate is chained (via pred) to the current k-1 candidate so
129  *	that the actual subsequence can be recovered. When a member has serial
130  *	number greater that the y of all k-candidates, the klist is extended. At
131  *	the end, the longest subsequence is pulled out and placed in the array J
132  *	by unravel.
133  *
134  *	With J in hand, the matches there recorded are check'ed against reality
135  *	to assure that no spurious matches have crept in due to hashing. If they
136  *	have, they are broken, and "jackpot" is recorded -- a harmless matter
137  *	except that a true match for a spuriously mated line may now be
138  *	unnecessarily reported as a change.
139  *
140  *	Much of the complexity of the program comes simply from trying to
141  *	minimize core utilization and maximize the range of doable problems by
142  *	dynamically allocating what is needed and reusing what is not. The core
143  *	requirements for problems larger than somewhat are (in words)
144  *	2*length(file0) + length(file1) + 3*(number of k-candidates installed),
145  *	typically about 6n words for files of length n.
146  */
147 
148 struct cand {
149 	int	x;
150 	int	y;
151 	int	pred;
152 };
153 
154 static struct line {
155 	int	serial;
156 	int	value;
157 } *file[2];
158 
159 /*
160  * The following struct is used to record change information when
161  * doing a "context" or "unified" diff.  (see routine "change" to
162  * understand the highly mnemonic field names)
163  */
164 struct context_vec {
165 	int	a;		/* start line in old file */
166 	int	b;		/* end line in old file */
167 	int	c;		/* start line in new file */
168 	int	d;		/* end line in new file */
169 };
170 
171 enum readhash { RH_BINARY, RH_OK, RH_EOF };
172 
173 #define MIN_PAD		1
174 static FILE	*opentemp(const char *);
175 static void	 output(char *, FILE *, char *, FILE *, int);
176 static void	 check(FILE *, FILE *, int);
177 static void	 range(int, int, const char *);
178 static void	 uni_range(int, int);
179 static void	 dump_context_vec(FILE *, FILE *, int);
180 static void	 dump_unified_vec(FILE *, FILE *, int);
181 static bool	 prepare(int, FILE *, size_t, int);
182 static void	 prune(void);
183 static void	 equiv(struct line *, int, struct line *, int, int *);
184 static void	 unravel(int);
185 static void	 unsort(struct line *, int, int *);
186 static void	 change(char *, FILE *, char *, FILE *, int, int, int, int, int *);
187 static void	 sort(struct line *, int);
188 static void	 print_header(const char *, const char *);
189 static void	 print_space(int, int, int);
190 static bool	 ignoreline_pattern(char *);
191 static bool	 ignoreline(char *, bool);
192 static int	 asciifile(FILE *);
193 static int	 fetch(long *, int, int, FILE *, int, int, int);
194 static int	 newcand(int, int, int);
195 static int	 search(int *, int, int);
196 static int	 skipline(FILE *);
197 static int	 isqrt(int);
198 static int	 stone(int *, int, int *, int *, int);
199 static enum readhash readhash(FILE *, int, unsigned *);
200 static int	 files_differ(FILE *, FILE *, int);
201 static char	*match_function(const long *, int, FILE *);
202 static char	*preadline(int, size_t, off_t);
203 
204 static int	 *J;			/* will be overlaid on class */
205 static int	 *class;		/* will be overlaid on file[0] */
206 static int	 *klist;		/* will be overlaid on file[0] after class */
207 static int	 *member;		/* will be overlaid on file[1] */
208 static int	 clen;
209 static int	 inifdef;		/* whether or not we are in a #ifdef block */
210 static int	 len[2];
211 static int	 pref, suff;	/* length of prefix and suffix */
212 static int	 slen[2];
213 static int	 anychange;
214 static int	 hw, padding;	/* half width and padding */
215 static int	 edoffset;
216 static long	*ixnew;		/* will be overlaid on file[1] */
217 static long	*ixold;		/* will be overlaid on klist */
218 static struct cand *clist;	/* merely a free storage pot for candidates */
219 static int	 clistlen;		/* the length of clist */
220 static struct line *sfile[2];	/* shortened by pruning common prefix/suffix */
221 static int	(*chrtran)(int);	/* translation table for case-folding */
222 static struct context_vec *context_vec_start;
223 static struct context_vec *context_vec_end;
224 static struct context_vec *context_vec_ptr;
225 
226 #define FUNCTION_CONTEXT_SIZE	55
227 static char lastbuf[FUNCTION_CONTEXT_SIZE];
228 static int lastline;
229 static int lastmatchline;
230 
231 static int
232 clow2low(int c)
233 {
234 
235 	return (c);
236 }
237 
238 static int
239 cup2low(int c)
240 {
241 
242 	return (tolower(c));
243 }
244 
245 int
246 diffreg(char *file1, char *file2, int flags, int capsicum)
247 {
248 	FILE *f1, *f2;
249 	int i, rval;
250 	struct pr *pr = NULL;
251 	cap_rights_t rights_ro;
252 
253 	f1 = f2 = NULL;
254 	rval = D_SAME;
255 	anychange = 0;
256 	lastline = 0;
257 	lastmatchline = 0;
258 	context_vec_ptr = context_vec_start - 1;
259 
260 	 /*
261 	  * hw excludes padding and make sure when -t is not used,
262 	  * the second column always starts from the closest tab stop
263 	  */
264 	if (diff_format == D_SIDEBYSIDE) {
265 		hw = width >> 1;
266 		padding = tabsize - (hw % tabsize);
267 		if ((flags & D_EXPANDTABS) != 0 || (padding % tabsize == 0))
268 			padding = MIN_PAD;
269 
270 		hw = (width >> 1) -
271 		    ((padding == MIN_PAD) ? (padding << 1) : padding) - 1;
272 	}
273 
274 
275 	if (flags & D_IGNORECASE)
276 		chrtran = cup2low;
277 	else
278 		chrtran = clow2low;
279 	if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
280 		return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
281 	if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
282 		goto closem;
283 
284 	if (flags & D_EMPTY1)
285 		f1 = fopen(_PATH_DEVNULL, "r");
286 	else {
287 		if (!S_ISREG(stb1.st_mode)) {
288 			if ((f1 = opentemp(file1)) == NULL ||
289 			    fstat(fileno(f1), &stb1) == -1) {
290 				warn("%s", file1);
291 				rval = D_ERROR;
292 				status |= 2;
293 				goto closem;
294 			}
295 		} else if (strcmp(file1, "-") == 0)
296 			f1 = stdin;
297 		else
298 			f1 = fopen(file1, "r");
299 	}
300 	if (f1 == NULL) {
301 		warn("%s", file1);
302 		rval = D_ERROR;
303 		status |= 2;
304 		goto closem;
305 	}
306 
307 	if (flags & D_EMPTY2)
308 		f2 = fopen(_PATH_DEVNULL, "r");
309 	else {
310 		if (!S_ISREG(stb2.st_mode)) {
311 			if ((f2 = opentemp(file2)) == NULL ||
312 			    fstat(fileno(f2), &stb2) == -1) {
313 				warn("%s", file2);
314 				rval = D_ERROR;
315 				status |= 2;
316 				goto closem;
317 			}
318 		} else if (strcmp(file2, "-") == 0)
319 			f2 = stdin;
320 		else
321 			f2 = fopen(file2, "r");
322 	}
323 	if (f2 == NULL) {
324 		warn("%s", file2);
325 		rval = D_ERROR;
326 		status |= 2;
327 		goto closem;
328 	}
329 
330 	if (lflag)
331 		pr = start_pr(file1, file2);
332 
333 	if (capsicum) {
334 		cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
335 		if (caph_rights_limit(fileno(f1), &rights_ro) < 0)
336 			err(2, "unable to limit rights on: %s", file1);
337 		if (caph_rights_limit(fileno(f2), &rights_ro) < 0)
338 			err(2, "unable to limit rights on: %s", file2);
339 		if (fileno(f1) == STDIN_FILENO || fileno(f2) == STDIN_FILENO) {
340 			/* stdin has already been limited */
341 			if (caph_limit_stderr() == -1)
342 				err(2, "unable to limit stderr");
343 			if (caph_limit_stdout() == -1)
344 				err(2, "unable to limit stdout");
345 		} else if (caph_limit_stdio() == -1)
346 				err(2, "unable to limit stdio");
347 
348 		caph_cache_catpages();
349 		caph_cache_tzdata();
350 		if (caph_enter() < 0)
351 			err(2, "unable to enter capability mode");
352 	}
353 
354 	switch (files_differ(f1, f2, flags)) {
355 	case 0:
356 		goto closem;
357 	case 1:
358 		break;
359 	default:
360 		/* error */
361 		rval = D_ERROR;
362 		status |= 2;
363 		goto closem;
364 	}
365 
366 	if (diff_format == D_BRIEF && ignore_pats == NULL &&
367 	    (flags & (D_FOLDBLANKS|D_IGNOREBLANKS|D_IGNORECASE|D_STRIPCR)) == 0)
368 	{
369 		rval = D_DIFFER;
370 		status |= 1;
371 		goto closem;
372 	}
373 	if ((flags & D_FORCEASCII) != 0) {
374 		(void)prepare(0, f1, stb1.st_size, flags);
375 		(void)prepare(1, f2, stb2.st_size, flags);
376 	} else if (!asciifile(f1) || !asciifile(f2) ||
377 		    !prepare(0, f1, stb1.st_size, flags) ||
378 		    !prepare(1, f2, stb2.st_size, flags)) {
379 		rval = D_BINARY;
380 		status |= 1;
381 		goto closem;
382 	}
383 
384 	prune();
385 	sort(sfile[0], slen[0]);
386 	sort(sfile[1], slen[1]);
387 
388 	member = (int *)file[1];
389 	equiv(sfile[0], slen[0], sfile[1], slen[1], member);
390 	member = xreallocarray(member, slen[1] + 2, sizeof(*member));
391 
392 	class = (int *)file[0];
393 	unsort(sfile[0], slen[0], class);
394 	class = xreallocarray(class, slen[0] + 2, sizeof(*class));
395 
396 	klist = xcalloc(slen[0] + 2, sizeof(*klist));
397 	clen = 0;
398 	clistlen = 100;
399 	clist = xcalloc(clistlen, sizeof(*clist));
400 	i = stone(class, slen[0], member, klist, flags);
401 	free(member);
402 	free(class);
403 
404 	J = xreallocarray(J, len[0] + 2, sizeof(*J));
405 	unravel(klist[i]);
406 	free(clist);
407 	free(klist);
408 
409 	ixold = xreallocarray(ixold, len[0] + 2, sizeof(*ixold));
410 	ixnew = xreallocarray(ixnew, len[1] + 2, sizeof(*ixnew));
411 	check(f1, f2, flags);
412 	output(file1, f1, file2, f2, flags);
413 
414 closem:
415 	if (pr != NULL)
416 		stop_pr(pr);
417 	if (anychange) {
418 		status |= 1;
419 		if (rval == D_SAME)
420 			rval = D_DIFFER;
421 	}
422 	if (f1 != NULL)
423 		fclose(f1);
424 	if (f2 != NULL)
425 		fclose(f2);
426 
427 	return (rval);
428 }
429 
430 /*
431  * Check to see if the given files differ.
432  * Returns 0 if they are the same, 1 if different, and -1 on error.
433  * XXX - could use code from cmp(1) [faster]
434  */
435 static int
436 files_differ(FILE *f1, FILE *f2, int flags)
437 {
438 	char buf1[BUFSIZ], buf2[BUFSIZ];
439 	size_t i, j;
440 
441 	if ((flags & (D_EMPTY1|D_EMPTY2)) || stb1.st_size != stb2.st_size ||
442 	    (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
443 		return (1);
444 	for (;;) {
445 		i = fread(buf1, 1, sizeof(buf1), f1);
446 		j = fread(buf2, 1, sizeof(buf2), f2);
447 		if ((!i && ferror(f1)) || (!j && ferror(f2)))
448 			return (-1);
449 		if (i != j)
450 			return (1);
451 		if (i == 0)
452 			return (0);
453 		if (memcmp(buf1, buf2, i) != 0)
454 			return (1);
455 	}
456 }
457 
458 static FILE *
459 opentemp(const char *f)
460 {
461 	char buf[BUFSIZ], tempfile[PATH_MAX];
462 	ssize_t nread;
463 	int ifd, ofd;
464 
465 	if (strcmp(f, "-") == 0)
466 		ifd = STDIN_FILENO;
467 	else if ((ifd = open(f, O_RDONLY, 0644)) == -1)
468 		return (NULL);
469 
470 	(void)strlcpy(tempfile, _PATH_TMP "/diff.XXXXXXXX", sizeof(tempfile));
471 
472 	if ((ofd = mkstemp(tempfile)) == -1) {
473 		close(ifd);
474 		return (NULL);
475 	}
476 	unlink(tempfile);
477 	while ((nread = read(ifd, buf, BUFSIZ)) > 0) {
478 		if (write(ofd, buf, nread) != nread) {
479 			close(ifd);
480 			close(ofd);
481 			return (NULL);
482 		}
483 	}
484 	close(ifd);
485 	lseek(ofd, (off_t)0, SEEK_SET);
486 	return (fdopen(ofd, "r"));
487 }
488 
489 static bool
490 prepare(int i, FILE *fd, size_t filesize, int flags)
491 {
492 	struct line *p;
493 	unsigned h;
494 	size_t sz, j = 0;
495 	enum readhash r;
496 
497 	rewind(fd);
498 
499 	sz = MIN(filesize, SIZE_MAX) / 25;
500 	if (sz < 100)
501 		sz = 100;
502 
503 	p = xcalloc(sz + 3, sizeof(*p));
504 	while ((r = readhash(fd, flags, &h)) != RH_EOF)
505 		switch (r) {
506 		case RH_EOF: /* otherwise clang complains */
507 		case RH_BINARY:
508 			return (false);
509 		case RH_OK:
510 			if (j == sz) {
511 				sz = sz * 3 / 2;
512 				p = xreallocarray(p, sz + 3, sizeof(*p));
513 			}
514 			p[++j].value = h;
515 		}
516 
517 	len[i] = j;
518 	file[i] = p;
519 
520 	return (true);
521 }
522 
523 static void
524 prune(void)
525 {
526 	int i, j;
527 
528 	for (pref = 0; pref < len[0] && pref < len[1] &&
529 	    file[0][pref + 1].value == file[1][pref + 1].value;
530 	    pref++)
531 		;
532 	for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
533 	    file[0][len[0] - suff].value == file[1][len[1] - suff].value;
534 	    suff++)
535 		;
536 	for (j = 0; j < 2; j++) {
537 		sfile[j] = file[j] + pref;
538 		slen[j] = len[j] - pref - suff;
539 		for (i = 0; i <= slen[j]; i++)
540 			sfile[j][i].serial = i;
541 	}
542 }
543 
544 static void
545 equiv(struct line *a, int n, struct line *b, int m, int *c)
546 {
547 	int i, j;
548 
549 	i = j = 1;
550 	while (i <= n && j <= m) {
551 		if (a[i].value < b[j].value)
552 			a[i++].value = 0;
553 		else if (a[i].value == b[j].value)
554 			a[i++].value = j;
555 		else
556 			j++;
557 	}
558 	while (i <= n)
559 		a[i++].value = 0;
560 	b[m + 1].value = 0;
561 	j = 0;
562 	while (++j <= m) {
563 		c[j] = -b[j].serial;
564 		while (b[j + 1].value == b[j].value) {
565 			j++;
566 			c[j] = b[j].serial;
567 		}
568 	}
569 	c[j] = -1;
570 }
571 
572 /* Code taken from ping.c */
573 static int
574 isqrt(int n)
575 {
576 	int y, x = 1;
577 
578 	if (n == 0)
579 		return (0);
580 
581 	do { /* newton was a stinker */
582 		y = x;
583 		x = n / x;
584 		x += y;
585 		x /= 2;
586 	} while ((x - y) > 1 || (x - y) < -1);
587 
588 	return (x);
589 }
590 
591 static int
592 stone(int *a, int n, int *b, int *c, int flags)
593 {
594 	int i, k, y, j, l;
595 	int oldc, tc, oldl, sq;
596 	unsigned numtries, bound;
597 
598 	if (flags & D_MINIMAL)
599 		bound = UINT_MAX;
600 	else {
601 		sq = isqrt(n);
602 		bound = MAX(256, sq);
603 	}
604 
605 	k = 0;
606 	c[0] = newcand(0, 0, 0);
607 	for (i = 1; i <= n; i++) {
608 		j = a[i];
609 		if (j == 0)
610 			continue;
611 		y = -b[j];
612 		oldl = 0;
613 		oldc = c[0];
614 		numtries = 0;
615 		do {
616 			if (y <= clist[oldc].y)
617 				continue;
618 			l = search(c, k, y);
619 			if (l != oldl + 1)
620 				oldc = c[l - 1];
621 			if (l <= k) {
622 				if (clist[c[l]].y <= y)
623 					continue;
624 				tc = c[l];
625 				c[l] = newcand(i, y, oldc);
626 				oldc = tc;
627 				oldl = l;
628 				numtries++;
629 			} else {
630 				c[l] = newcand(i, y, oldc);
631 				k++;
632 				break;
633 			}
634 		} while ((y = b[++j]) > 0 && numtries < bound);
635 	}
636 	return (k);
637 }
638 
639 static int
640 newcand(int x, int y, int pred)
641 {
642 	struct cand *q;
643 
644 	if (clen == clistlen) {
645 		clistlen = clistlen * 11 / 10;
646 		clist = xreallocarray(clist, clistlen, sizeof(*clist));
647 	}
648 	q = clist + clen;
649 	q->x = x;
650 	q->y = y;
651 	q->pred = pred;
652 	return (clen++);
653 }
654 
655 static int
656 search(int *c, int k, int y)
657 {
658 	int i, j, l, t;
659 
660 	if (clist[c[k]].y < y)	/* quick look for typical case */
661 		return (k + 1);
662 	i = 0;
663 	j = k + 1;
664 	for (;;) {
665 		l = (i + j) / 2;
666 		if (l <= i)
667 			break;
668 		t = clist[c[l]].y;
669 		if (t > y)
670 			j = l;
671 		else if (t < y)
672 			i = l;
673 		else
674 			return (l);
675 	}
676 	return (l + 1);
677 }
678 
679 static void
680 unravel(int p)
681 {
682 	struct cand *q;
683 	int i;
684 
685 	for (i = 0; i <= len[0]; i++)
686 		J[i] = i <= pref ? i :
687 		    i > len[0] - suff ? i + len[1] - len[0] : 0;
688 	for (q = clist + p; q->y != 0; q = clist + q->pred)
689 		J[q->x + pref] = q->y + pref;
690 }
691 
692 /*
693  * Check does double duty:
694  *  1. ferret out any fortuitous correspondences due to confounding by
695  *     hashing (which result in "jackpot")
696  *  2. collect random access indexes to the two files
697  */
698 static void
699 check(FILE *f1, FILE *f2, int flags)
700 {
701 	int i, j, jackpot, c, d;
702 	long ctold, ctnew;
703 
704 	rewind(f1);
705 	rewind(f2);
706 	j = 1;
707 	ixold[0] = ixnew[0] = 0;
708 	jackpot = 0;
709 	ctold = ctnew = 0;
710 	for (i = 1; i <= len[0]; i++) {
711 		if (J[i] == 0) {
712 			ixold[i] = ctold += skipline(f1);
713 			continue;
714 		}
715 		while (j < J[i]) {
716 			ixnew[j] = ctnew += skipline(f2);
717 			j++;
718 		}
719 		if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_STRIPCR)) {
720 			for (;;) {
721 				c = getc(f1);
722 				d = getc(f2);
723 				/*
724 				 * GNU diff ignores a missing newline
725 				 * in one file for -b or -w.
726 				 */
727 				if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) {
728 					if (c == EOF && d == '\n') {
729 						ctnew++;
730 						break;
731 					} else if (c == '\n' && d == EOF) {
732 						ctold++;
733 						break;
734 					}
735 				}
736 				ctold++;
737 				ctnew++;
738 				if (flags & D_STRIPCR && (c == '\r' || d == '\r')) {
739 					if (c == '\r') {
740 						if ((c = getc(f1)) == '\n') {
741 							ctold++;
742 						} else {
743 							ungetc(c, f1);
744 						}
745 					}
746 					if (d == '\r') {
747 						if ((d = getc(f2)) == '\n') {
748 							ctnew++;
749 						} else {
750 							ungetc(d, f2);
751 						}
752 					}
753 					break;
754 				}
755 				if ((flags & D_FOLDBLANKS) && isspace(c) &&
756 				    isspace(d)) {
757 					do {
758 						if (c == '\n')
759 							break;
760 						ctold++;
761 					} while (isspace(c = getc(f1)));
762 					do {
763 						if (d == '\n')
764 							break;
765 						ctnew++;
766 					} while (isspace(d = getc(f2)));
767 				} else if (flags & D_IGNOREBLANKS) {
768 					while (isspace(c) && c != '\n') {
769 						c = getc(f1);
770 						ctold++;
771 					}
772 					while (isspace(d) && d != '\n') {
773 						d = getc(f2);
774 						ctnew++;
775 					}
776 				}
777 				if (chrtran(c) != chrtran(d)) {
778 					jackpot++;
779 					J[i] = 0;
780 					if (c != '\n' && c != EOF)
781 						ctold += skipline(f1);
782 					if (d != '\n' && c != EOF)
783 						ctnew += skipline(f2);
784 					break;
785 				}
786 				if (c == '\n' || c == EOF)
787 					break;
788 			}
789 		} else {
790 			for (;;) {
791 				ctold++;
792 				ctnew++;
793 				if ((c = getc(f1)) != (d = getc(f2))) {
794 					/* jackpot++; */
795 					J[i] = 0;
796 					if (c != '\n' && c != EOF)
797 						ctold += skipline(f1);
798 					if (d != '\n' && c != EOF)
799 						ctnew += skipline(f2);
800 					break;
801 				}
802 				if (c == '\n' || c == EOF)
803 					break;
804 			}
805 		}
806 		ixold[i] = ctold;
807 		ixnew[j] = ctnew;
808 		j++;
809 	}
810 	for (; j <= len[1]; j++) {
811 		ixnew[j] = ctnew += skipline(f2);
812 	}
813 	/*
814 	 * if (jackpot)
815 	 *	fprintf(stderr, "jackpot\n");
816 	 */
817 }
818 
819 /* shellsort CACM #201 */
820 static void
821 sort(struct line *a, int n)
822 {
823 	struct line *ai, *aim, w;
824 	int j, m = 0, k;
825 
826 	if (n == 0)
827 		return;
828 	for (j = 1; j <= n; j *= 2)
829 		m = 2 * j - 1;
830 	for (m /= 2; m != 0; m /= 2) {
831 		k = n - m;
832 		for (j = 1; j <= k; j++) {
833 			for (ai = &a[j]; ai > a; ai -= m) {
834 				aim = &ai[m];
835 				if (aim < ai)
836 					break;	/* wraparound */
837 				if (aim->value > ai[0].value ||
838 				    (aim->value == ai[0].value &&
839 					aim->serial > ai[0].serial))
840 					break;
841 				w.value = ai[0].value;
842 				ai[0].value = aim->value;
843 				aim->value = w.value;
844 				w.serial = ai[0].serial;
845 				ai[0].serial = aim->serial;
846 				aim->serial = w.serial;
847 			}
848 		}
849 	}
850 }
851 
852 static void
853 unsort(struct line *f, int l, int *b)
854 {
855 	int *a, i;
856 
857 	a = xcalloc(l + 1, sizeof(*a));
858 	for (i = 1; i <= l; i++)
859 		a[f[i].serial] = f[i].value;
860 	for (i = 1; i <= l; i++)
861 		b[i] = a[i];
862 	free(a);
863 }
864 
865 static int
866 skipline(FILE *f)
867 {
868 	int i, c;
869 
870 	for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
871 		continue;
872 	return (i);
873 }
874 
875 static void
876 output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
877 {
878 	int i, j, m, i0, i1, j0, j1, nc;
879 
880 	rewind(f1);
881 	rewind(f2);
882 	m = len[0];
883 	J[0] = 0;
884 	J[m + 1] = len[1] + 1;
885 	if (diff_format != D_EDIT) {
886 		for (i0 = 1; i0 <= m; i0 = i1 + 1) {
887 			while (i0 <= m && J[i0] == J[i0 - 1] + 1) {
888 				if (diff_format == D_SIDEBYSIDE && suppress_common != 1) {
889 					nc = fetch(ixold, i0, i0, f1, '\0', 1, flags);
890 					print_space(nc, (hw - nc) + (padding << 1) + 1, flags);
891 					fetch(ixnew, J[i0], J[i0], f2, '\0', 0, flags);
892 					printf("\n");
893 				}
894 				i0++;
895 			}
896 			j0 = J[i0 - 1] + 1;
897 			i1 = i0 - 1;
898 			while (i1 < m && J[i1 + 1] == 0)
899 				i1++;
900 			j1 = J[i1 + 1] - 1;
901 			J[i1] = j1;
902 
903 			/*
904 			 * When using side-by-side, lines from both of the files are
905 			 * printed. The algorithm used by diff(1) identifies the ranges
906 			 * in which two files differ.
907 			 * See the change() function below.
908 			 * The for loop below consumes the shorter range, whereas one of
909 			 * the while loops deals with the longer one.
910 			 */
911 			if (diff_format == D_SIDEBYSIDE) {
912 				for (i = i0, j = j0; i <= i1 && j <= j1; i++, j++)
913 					change(file1, f1, file2, f2, i, i, j, j, &flags);
914 
915 				while (i <= i1) {
916 					change(file1, f1, file2, f2, i, i, j + 1, j, &flags);
917 					i++;
918 				}
919 
920 				while (j <= j1) {
921 					change(file1, f1, file2, f2, i + 1, i, j, j, &flags);
922 					j++;
923 				}
924 			} else
925 				change(file1, f1, file2, f2, i0, i1, j0, j1, &flags);
926 		}
927 	} else {
928 		for (i0 = m; i0 >= 1; i0 = i1 - 1) {
929 			while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
930 				i0--;
931 			j0 = J[i0 + 1] - 1;
932 			i1 = i0 + 1;
933 			while (i1 > 1 && J[i1 - 1] == 0)
934 				i1--;
935 			j1 = J[i1 - 1] + 1;
936 			J[i1] = j1;
937 			change(file1, f1, file2, f2, i1, i0, j1, j0, &flags);
938 		}
939 	}
940 	if (m == 0)
941 		change(file1, f1, file2, f2, 1, 0, 1, len[1], &flags);
942 	if (diff_format == D_IFDEF || diff_format == D_GFORMAT) {
943 		for (;;) {
944 #define	c i0
945 			if ((c = getc(f1)) == EOF)
946 				return;
947 			printf("%c", c);
948 		}
949 #undef c
950 	}
951 	if (anychange != 0) {
952 		if (diff_format == D_CONTEXT)
953 			dump_context_vec(f1, f2, flags);
954 		else if (diff_format == D_UNIFIED)
955 			dump_unified_vec(f1, f2, flags);
956 	}
957 }
958 
959 static void
960 range(int a, int b, const char *separator)
961 {
962 	printf("%d", a > b ? b : a);
963 	if (a < b)
964 		printf("%s%d", separator, b);
965 }
966 
967 static void
968 uni_range(int a, int b)
969 {
970 	if (a < b)
971 		printf("%d,%d", a, b - a + 1);
972 	else if (a == b)
973 		printf("%d", b);
974 	else
975 		printf("%d,0", b);
976 }
977 
978 static char *
979 preadline(int fd, size_t rlen, off_t off)
980 {
981 	char *line;
982 	ssize_t nr;
983 
984 	line = xmalloc(rlen + 1);
985 	if ((nr = pread(fd, line, rlen, off)) == -1)
986 		err(2, "preadline");
987 	if (nr > 0 && line[nr-1] == '\n')
988 		nr--;
989 	line[nr] = '\0';
990 	return (line);
991 }
992 
993 static bool
994 ignoreline_pattern(char *line)
995 {
996 	int ret;
997 
998 	ret = regexec(&ignore_re, line, 0, NULL, 0);
999 	free(line);
1000 	return (ret == 0);	/* if it matched, it should be ignored. */
1001 }
1002 
1003 static bool
1004 ignoreline(char *line, bool skip_blanks)
1005 {
1006 
1007 	if (ignore_pats != NULL && skip_blanks)
1008 		return (ignoreline_pattern(line) || *line == '\0');
1009 	if (ignore_pats != NULL)
1010 		return (ignoreline_pattern(line));
1011 	if (skip_blanks)
1012 		return (*line == '\0');
1013 	/* No ignore criteria specified */
1014 	return (false);
1015 }
1016 
1017 /*
1018  * Indicate that there is a difference between lines a and b of the from file
1019  * to get to lines c to d of the to file.  If a is greater then b then there
1020  * are no lines in the from file involved and this means that there were
1021  * lines appended (beginning at b).  If c is greater than d then there are
1022  * lines missing from the to file.
1023  */
1024 static void
1025 change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
1026     int *pflags)
1027 {
1028 	static size_t max_context = 64;
1029 	long curpos;
1030 	int i, nc;
1031 	const char *walk;
1032 	bool skip_blanks;
1033 
1034 	skip_blanks = (*pflags & D_SKIPBLANKLINES);
1035 restart:
1036 	if ((diff_format != D_IFDEF || diff_format == D_GFORMAT) &&
1037 	    a > b && c > d)
1038 		return;
1039 	if (ignore_pats != NULL || skip_blanks) {
1040 		char *line;
1041 		/*
1042 		 * All lines in the change, insert, or delete must match an ignore
1043 		 * pattern for the change to be ignored.
1044 		 */
1045 		if (a <= b) {		/* Changes and deletes. */
1046 			for (i = a; i <= b; i++) {
1047 				line = preadline(fileno(f1),
1048 				    ixold[i] - ixold[i - 1], ixold[i - 1]);
1049 				if (!ignoreline(line, skip_blanks))
1050 					goto proceed;
1051 			}
1052 		}
1053 		if (a > b || c <= d) {	/* Changes and inserts. */
1054 			for (i = c; i <= d; i++) {
1055 				line = preadline(fileno(f2),
1056 				    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1057 				if (!ignoreline(line, skip_blanks))
1058 					goto proceed;
1059 			}
1060 		}
1061 		return;
1062 	}
1063 proceed:
1064 	if (*pflags & D_HEADER && diff_format != D_BRIEF) {
1065 		printf("%s %s %s\n", diffargs, file1, file2);
1066 		*pflags &= ~D_HEADER;
1067 	}
1068 	if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1069 		/*
1070 		 * Allocate change records as needed.
1071 		 */
1072 		if (context_vec_ptr == context_vec_end - 1) {
1073 			ptrdiff_t offset = context_vec_ptr - context_vec_start;
1074 			max_context <<= 1;
1075 			context_vec_start = xreallocarray(context_vec_start,
1076 			    max_context, sizeof(*context_vec_start));
1077 			context_vec_end = context_vec_start + max_context;
1078 			context_vec_ptr = context_vec_start + offset;
1079 		}
1080 		if (anychange == 0) {
1081 			/*
1082 			 * Print the context/unidiff header first time through.
1083 			 */
1084 			print_header(file1, file2);
1085 			anychange = 1;
1086 		} else if (a > context_vec_ptr->b + (2 * diff_context) + 1 &&
1087 		    c > context_vec_ptr->d + (2 * diff_context) + 1) {
1088 			/*
1089 			 * If this change is more than 'diff_context' lines from the
1090 			 * previous change, dump the record and reset it.
1091 			 */
1092 			if (diff_format == D_CONTEXT)
1093 				dump_context_vec(f1, f2, *pflags);
1094 			else
1095 				dump_unified_vec(f1, f2, *pflags);
1096 		}
1097 		context_vec_ptr++;
1098 		context_vec_ptr->a = a;
1099 		context_vec_ptr->b = b;
1100 		context_vec_ptr->c = c;
1101 		context_vec_ptr->d = d;
1102 		return;
1103 	}
1104 	if (anychange == 0)
1105 		anychange = 1;
1106 	switch (diff_format) {
1107 	case D_BRIEF:
1108 		return;
1109 	case D_NORMAL:
1110 	case D_EDIT:
1111 		range(a, b, ",");
1112 		printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1113 		if (diff_format == D_NORMAL)
1114 			range(c, d, ",");
1115 		printf("\n");
1116 		break;
1117 	case D_REVERSE:
1118 		printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1119 		range(a, b, " ");
1120 		printf("\n");
1121 		break;
1122 	case D_NREVERSE:
1123 		if (a > b)
1124 			printf("a%d %d\n", b, d - c + 1);
1125 		else {
1126 			printf("d%d %d\n", a, b - a + 1);
1127 			if (!(c > d))
1128 				/* add changed lines */
1129 				printf("a%d %d\n", b, d - c + 1);
1130 		}
1131 		break;
1132 	}
1133 	if (diff_format == D_GFORMAT) {
1134 		curpos = ftell(f1);
1135 		/* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1136 		nc = ixold[a > b ? b : a - 1] - curpos;
1137 		for (i = 0; i < nc; i++)
1138 			printf("%c", getc(f1));
1139 		for (walk = group_format; *walk != '\0'; walk++) {
1140 			if (*walk == '%') {
1141 				walk++;
1142 				switch (*walk) {
1143 				case '<':
1144 					fetch(ixold, a, b, f1, '<', 1, *pflags);
1145 					break;
1146 				case '>':
1147 					fetch(ixnew, c, d, f2, '>', 0, *pflags);
1148 					break;
1149 				default:
1150 					printf("%%%c", *walk);
1151 					break;
1152 				}
1153 				continue;
1154 			}
1155 			printf("%c", *walk);
1156 		}
1157 	}
1158 	if (diff_format == D_SIDEBYSIDE) {
1159 		if (a > b) {
1160 			print_space(0, hw + padding , *pflags);
1161 		} else {
1162 			nc = fetch(ixold, a, b, f1, '\0', 1, *pflags);
1163 			print_space(nc, hw - nc + padding, *pflags);
1164 		}
1165 		printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|'));
1166 		print_space(hw + padding + 1 , padding, *pflags);
1167 		fetch(ixnew, c, d, f2, '\0', 0, *pflags);
1168 		printf("\n");
1169 	}
1170 	if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1171 		fetch(ixold, a, b, f1, '<', 1, *pflags);
1172 		if (a <= b && c <= d && diff_format == D_NORMAL)
1173 			printf("---\n");
1174 	}
1175 	if (diff_format != D_GFORMAT && diff_format != D_SIDEBYSIDE)
1176 		fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags);
1177 	if (edoffset != 0 && diff_format == D_EDIT) {
1178 		/*
1179 		 * A non-zero edoffset value for D_EDIT indicates that the last line
1180 		 * printed was a bare dot (".") that has been escaped as ".." to
1181 		 * prevent ed(1) from misinterpreting it.  We have to add a
1182 		 * substitute command to change this back and restart where we left
1183 		 * off.
1184 		 */
1185 		printf(".\n");
1186 		printf("%ds/.//\n", a + edoffset - 1);
1187 		b = a + edoffset - 1;
1188 		a = b + 1;
1189 		c += edoffset;
1190 		goto restart;
1191 	}
1192 	if ((diff_format == D_EDIT || diff_format == D_REVERSE) && c <= d)
1193 		printf(".\n");
1194 	if (inifdef) {
1195 		printf("#endif /* %s */\n", ifdefname);
1196 		inifdef = 0;
1197 	}
1198 }
1199 
1200 static int
1201 fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
1202 {
1203 	int i, j, c, lastc, col, nc, newcol;
1204 
1205 	edoffset = 0;
1206 	nc = 0;
1207 	/*
1208 	 * When doing #ifdef's, copy down to current line
1209 	 * if this is the first file, so that stuff makes it to output.
1210 	 */
1211 	if ((diff_format == D_IFDEF) && oldfile) {
1212 		long curpos = ftell(lb);
1213 		/* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1214 		nc = f[a > b ? b : a - 1] - curpos;
1215 		for (i = 0; i < nc; i++)
1216 			printf("%c", getc(lb));
1217 	}
1218 	if (a > b)
1219 		return (0);
1220 	if (diff_format == D_IFDEF) {
1221 		if (inifdef) {
1222 			printf("#else /* %s%s */\n",
1223 			    oldfile == 1 ? "!" : "", ifdefname);
1224 		} else {
1225 			if (oldfile)
1226 				printf("#ifndef %s\n", ifdefname);
1227 			else
1228 				printf("#ifdef %s\n", ifdefname);
1229 		}
1230 		inifdef = 1 + oldfile;
1231 	}
1232 	for (i = a; i <= b; i++) {
1233 		fseek(lb, f[i - 1], SEEK_SET);
1234 		nc = f[i] - f[i - 1];
1235 		if (diff_format == D_SIDEBYSIDE && hw < nc)
1236 			nc = hw;
1237 		if (diff_format != D_IFDEF && diff_format != D_GFORMAT &&
1238 		    ch != '\0') {
1239 			printf("%c", ch);
1240 			if (Tflag && (diff_format == D_NORMAL ||
1241 			    diff_format == D_CONTEXT ||
1242 			    diff_format == D_UNIFIED))
1243 				printf("\t");
1244 			else if (diff_format != D_UNIFIED)
1245 				printf(" ");
1246 		}
1247 		col = 0;
1248 		for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1249 			c = getc(lb);
1250 			if (flags & D_STRIPCR && c == '\r') {
1251 				if ((c = getc(lb)) == '\n')
1252 					j++;
1253 				else {
1254 					ungetc(c, lb);
1255 					c = '\r';
1256 				}
1257 			}
1258 			if (c == EOF) {
1259 				if (diff_format == D_EDIT ||
1260 				    diff_format == D_REVERSE ||
1261 				    diff_format == D_NREVERSE)
1262 					warnx("No newline at end of file");
1263 				else
1264 					printf("\n\\ No newline at end of file\n");
1265 				return (col);
1266 			}
1267 			/*
1268 			 * when using --side-by-side, col needs to be increased
1269 			 * in any case to keep the columns aligned
1270 			 */
1271 			if (c == '\t') {
1272 				if (flags & D_EXPANDTABS) {
1273 					newcol = ((col / tabsize) + 1) * tabsize;
1274 					do {
1275 						if (diff_format == D_SIDEBYSIDE)
1276 							j++;
1277 						printf(" ");
1278 					} while (++col < newcol && j < nc);
1279 				} else {
1280 					if (diff_format == D_SIDEBYSIDE) {
1281 						if ((j + tabsize) > nc) {
1282 							printf("%*s", nc - j, "");
1283 							j = col = nc;
1284 						} else {
1285 							printf("\t");
1286 							col += tabsize - 1;
1287 							j += tabsize - 1;
1288 						}
1289 					} else {
1290 						printf("\t");
1291 						col++;
1292 					}
1293 				}
1294 			} else {
1295 				if (diff_format == D_EDIT && j == 1 && c == '\n' &&
1296 				    lastc == '.') {
1297 					/*
1298 					 * Don't print a bare "." line since that will confuse
1299 					 * ed(1). Print ".." instead and set the, global variable
1300 					 * edoffset to an offset from which to restart. The
1301 					 * caller must check the value of edoffset
1302 					 */
1303 					printf(".\n");
1304 					edoffset = i - a + 1;
1305 					return (edoffset);
1306 				}
1307 				/* when side-by-side, do not print a newline */
1308 				if (diff_format != D_SIDEBYSIDE || c != '\n') {
1309 					printf("%c", c);
1310 					col++;
1311 				}
1312 			}
1313 		}
1314 	}
1315 	return (col);
1316 }
1317 
1318 /*
1319  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1320  */
1321 static enum readhash
1322 readhash(FILE *f, int flags, unsigned *hash)
1323 {
1324 	int i, t, space;
1325 	unsigned sum;
1326 
1327 	sum = 1;
1328 	space = 0;
1329 	for (i = 0;;) {
1330 		switch (t = getc(f)) {
1331 		case '\0':
1332 			if ((flags & D_FORCEASCII) == 0)
1333 				return (RH_BINARY);
1334 		case '\r':
1335 			if (flags & D_STRIPCR) {
1336 				t = getc(f);
1337 				if (t == '\n')
1338 					break;
1339 				ungetc(t, f);
1340 			}
1341 			/* FALLTHROUGH */
1342 		case '\t':
1343 		case '\v':
1344 		case '\f':
1345 		case ' ':
1346 			if ((flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) != 0) {
1347 				space++;
1348 				continue;
1349 			}
1350 			/* FALLTHROUGH */
1351 		default:
1352 			if (space && (flags & D_IGNOREBLANKS) == 0) {
1353 				i++;
1354 				space = 0;
1355 			}
1356 			sum = sum * 127 + chrtran(t);
1357 			i++;
1358 			continue;
1359 		case EOF:
1360 			if (i == 0)
1361 				return (RH_EOF);
1362 			/* FALLTHROUGH */
1363 		case '\n':
1364 			break;
1365 		}
1366 		break;
1367 	}
1368 	*hash = sum;
1369 	return (RH_OK);
1370 }
1371 
1372 static int
1373 asciifile(FILE *f)
1374 {
1375 	unsigned char buf[BUFSIZ];
1376 	size_t cnt;
1377 
1378 	if (f == NULL)
1379 		return (1);
1380 
1381 	rewind(f);
1382 	cnt = fread(buf, 1, sizeof(buf), f);
1383 	return (memchr(buf, '\0', cnt) == NULL);
1384 }
1385 
1386 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre) - 1) == 0)
1387 
1388 static char *
1389 match_function(const long *f, int pos, FILE *fp)
1390 {
1391 	unsigned char buf[FUNCTION_CONTEXT_SIZE];
1392 	size_t nc;
1393 	int last = lastline;
1394 	const char *state = NULL;
1395 
1396 	lastline = pos;
1397 	while (pos > last) {
1398 		fseek(fp, f[pos - 1], SEEK_SET);
1399 		nc = f[pos] - f[pos - 1];
1400 		if (nc >= sizeof(buf))
1401 			nc = sizeof(buf) - 1;
1402 		nc = fread(buf, 1, nc, fp);
1403 		if (nc > 0) {
1404 			buf[nc] = '\0';
1405 			buf[strcspn(buf, "\n")] = '\0';
1406 			if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1407 				if (begins_with(buf, "private:")) {
1408 					if (!state)
1409 						state = " (private)";
1410 				} else if (begins_with(buf, "protected:")) {
1411 					if (!state)
1412 						state = " (protected)";
1413 				} else if (begins_with(buf, "public:")) {
1414 					if (!state)
1415 						state = " (public)";
1416 				} else {
1417 					strlcpy(lastbuf, buf, sizeof(lastbuf));
1418 					if (state)
1419 						strlcat(lastbuf, state, sizeof(lastbuf));
1420 					lastmatchline = pos;
1421 					return (lastbuf);
1422 				}
1423 			}
1424 		}
1425 		pos--;
1426 	}
1427 	return (lastmatchline > 0 ? lastbuf : NULL);
1428 }
1429 
1430 /* dump accumulated "context" diff changes */
1431 static void
1432 dump_context_vec(FILE *f1, FILE *f2, int flags)
1433 {
1434 	struct context_vec *cvp = context_vec_start;
1435 	int lowa, upb, lowc, upd, do_output;
1436 	int a, b, c, d;
1437 	char ch, *f;
1438 
1439 	if (context_vec_start > context_vec_ptr)
1440 		return;
1441 
1442 	b = d = 0;		/* gcc */
1443 	lowa = MAX(1, cvp->a - diff_context);
1444 	upb = MIN(len[0], context_vec_ptr->b + diff_context);
1445 	lowc = MAX(1, cvp->c - diff_context);
1446 	upd = MIN(len[1], context_vec_ptr->d + diff_context);
1447 
1448 	printf("***************");
1449 	if ((flags & D_PROTOTYPE)) {
1450 		f = match_function(ixold, lowa - 1, f1);
1451 		if (f != NULL)
1452 			printf(" %s", f);
1453 	}
1454 	printf("\n*** ");
1455 	range(lowa, upb, ",");
1456 	printf(" ****\n");
1457 
1458 	/*
1459 	 * Output changes to the "old" file.  The first loop suppresses
1460 	 * output if there were no changes to the "old" file (we'll see
1461 	 * the "old" lines as context in the "new" list).
1462 	 */
1463 	do_output = 0;
1464 	for (; cvp <= context_vec_ptr; cvp++)
1465 		if (cvp->a <= cvp->b) {
1466 			cvp = context_vec_start;
1467 			do_output++;
1468 			break;
1469 		}
1470 	if (do_output) {
1471 		while (cvp <= context_vec_ptr) {
1472 			a = cvp->a;
1473 			b = cvp->b;
1474 			c = cvp->c;
1475 			d = cvp->d;
1476 
1477 			if (a <= b && c <= d)
1478 				ch = 'c';
1479 			else
1480 				ch = (a <= b) ? 'd' : 'a';
1481 
1482 			if (ch == 'a')
1483 				fetch(ixold, lowa, b, f1, ' ', 0, flags);
1484 			else {
1485 				fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1486 				fetch(ixold, a, b, f1,
1487 				    ch == 'c' ? '!' : '-', 0, flags);
1488 			}
1489 			lowa = b + 1;
1490 			cvp++;
1491 		}
1492 		fetch(ixold, b + 1, upb, f1, ' ', 0, flags);
1493 	}
1494 	/* output changes to the "new" file */
1495 	printf("--- ");
1496 	range(lowc, upd, ",");
1497 	printf(" ----\n");
1498 
1499 	do_output = 0;
1500 	for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1501 		if (cvp->c <= cvp->d) {
1502 			cvp = context_vec_start;
1503 			do_output++;
1504 			break;
1505 		}
1506 	if (do_output) {
1507 		while (cvp <= context_vec_ptr) {
1508 			a = cvp->a;
1509 			b = cvp->b;
1510 			c = cvp->c;
1511 			d = cvp->d;
1512 
1513 			if (a <= b && c <= d)
1514 				ch = 'c';
1515 			else
1516 				ch = (a <= b) ? 'd' : 'a';
1517 
1518 			if (ch == 'd')
1519 				fetch(ixnew, lowc, d, f2, ' ', 0, flags);
1520 			else {
1521 				fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1522 				fetch(ixnew, c, d, f2,
1523 				    ch == 'c' ? '!' : '+', 0, flags);
1524 			}
1525 			lowc = d + 1;
1526 			cvp++;
1527 		}
1528 		fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1529 	}
1530 	context_vec_ptr = context_vec_start - 1;
1531 }
1532 
1533 /* dump accumulated "unified" diff changes */
1534 static void
1535 dump_unified_vec(FILE *f1, FILE *f2, int flags)
1536 {
1537 	struct context_vec *cvp = context_vec_start;
1538 	int lowa, upb, lowc, upd;
1539 	int a, b, c, d;
1540 	char ch, *f;
1541 
1542 	if (context_vec_start > context_vec_ptr)
1543 		return;
1544 
1545 	b = d = 0;		/* gcc */
1546 	lowa = MAX(1, cvp->a - diff_context);
1547 	upb = MIN(len[0], context_vec_ptr->b + diff_context);
1548 	lowc = MAX(1, cvp->c - diff_context);
1549 	upd = MIN(len[1], context_vec_ptr->d + diff_context);
1550 
1551 	printf("@@ -");
1552 	uni_range(lowa, upb);
1553 	printf(" +");
1554 	uni_range(lowc, upd);
1555 	printf(" @@");
1556 	if ((flags & D_PROTOTYPE)) {
1557 		f = match_function(ixold, lowa - 1, f1);
1558 		if (f != NULL)
1559 			printf(" %s", f);
1560 	}
1561 	printf("\n");
1562 
1563 	/*
1564 	 * Output changes in "unified" diff format--the old and new lines
1565 	 * are printed together.
1566 	 */
1567 	for (; cvp <= context_vec_ptr; cvp++) {
1568 		a = cvp->a;
1569 		b = cvp->b;
1570 		c = cvp->c;
1571 		d = cvp->d;
1572 
1573 		/*
1574 		 * c: both new and old changes
1575 		 * d: only changes in the old file
1576 		 * a: only changes in the new file
1577 		 */
1578 		if (a <= b && c <= d)
1579 			ch = 'c';
1580 		else
1581 			ch = (a <= b) ? 'd' : 'a';
1582 
1583 		switch (ch) {
1584 		case 'c':
1585 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1586 			fetch(ixold, a, b, f1, '-', 0, flags);
1587 			fetch(ixnew, c, d, f2, '+', 0, flags);
1588 			break;
1589 		case 'd':
1590 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1591 			fetch(ixold, a, b, f1, '-', 0, flags);
1592 			break;
1593 		case 'a':
1594 			fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1595 			fetch(ixnew, c, d, f2, '+', 0, flags);
1596 			break;
1597 		}
1598 		lowa = b + 1;
1599 		lowc = d + 1;
1600 	}
1601 	fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1602 
1603 	context_vec_ptr = context_vec_start - 1;
1604 }
1605 
1606 static void
1607 print_header(const char *file1, const char *file2)
1608 {
1609 	const char *time_format;
1610 	char buf1[256];
1611 	char buf2[256];
1612 	char end1[10];
1613 	char end2[10];
1614 	struct tm tm1, tm2, *tm_ptr1, *tm_ptr2;
1615 	int nsec1 = stb1.st_mtim.tv_nsec;
1616 	int nsec2 = stb2.st_mtim.tv_nsec;
1617 
1618 	time_format = "%Y-%m-%d %H:%M:%S";
1619 
1620 	if (cflag)
1621 		time_format = "%c";
1622 	tm_ptr1 = localtime_r(&stb1.st_mtime, &tm1);
1623 	tm_ptr2 = localtime_r(&stb2.st_mtime, &tm2);
1624 	strftime(buf1, 256, time_format, tm_ptr1);
1625 	strftime(buf2, 256, time_format, tm_ptr2);
1626 	if (!cflag) {
1627 		strftime(end1, 10, "%z", tm_ptr1);
1628 		strftime(end2, 10, "%z", tm_ptr2);
1629 		sprintf(buf1, "%s.%.9d %s", buf1, nsec1, end1);
1630 		sprintf(buf2, "%s.%.9d %s", buf2, nsec2, end2);
1631 	}
1632 	if (label[0] != NULL)
1633 		printf("%s %s\n", diff_format == D_CONTEXT ? "***" : "---",
1634 		    label[0]);
1635 	else
1636 		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "***" : "---",
1637 		    file1, buf1);
1638 	if (label[1] != NULL)
1639 		printf("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
1640 		    label[1]);
1641 	else
1642 		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "---" : "+++",
1643 		    file2, buf2);
1644 }
1645 
1646 /*
1647  * Prints n number of space characters either by using tab
1648  * or single space characters.
1649  * nc is the preceding number of characters
1650  */
1651 static void
1652 print_space(int nc, int n, int flags) {
1653 	int i, col;
1654 
1655 	col = n;
1656 	if ((flags & D_EXPANDTABS) == 0) {
1657 		/* first tabstop may be closer than tabsize */
1658 		i = tabsize - (nc % tabsize);
1659 		while (col >= tabsize) {
1660 			printf("\t");
1661 			col -= i;
1662 			i = tabsize;
1663 		}
1664 	}
1665 	printf("%*s", col, "");
1666 }
1667