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