xref: /freebsd-src/usr.bin/diff/diffreg.c (revision 03582021117f05fa562a2a43028b931ec21c5237)
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 char *
490 splice(char *dir, char *path)
491 {
492 	char *tail, *buf;
493 	size_t dirlen;
494 
495 	dirlen = strlen(dir);
496 	while (dirlen != 0 && dir[dirlen - 1] == '/')
497 	    dirlen--;
498 	if ((tail = strrchr(path, '/')) == NULL)
499 		tail = path;
500 	else
501 		tail++;
502 	xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
503 	return (buf);
504 }
505 
506 static bool
507 prepare(int i, FILE *fd, size_t filesize, int flags)
508 {
509 	struct line *p;
510 	unsigned h;
511 	size_t sz, j = 0;
512 	enum readhash r;
513 
514 	rewind(fd);
515 
516 	sz = MIN(filesize, SIZE_MAX) / 25;
517 	if (sz < 100)
518 		sz = 100;
519 
520 	p = xcalloc(sz + 3, sizeof(*p));
521 	while ((r = readhash(fd, flags, &h)) != RH_EOF)
522 		switch (r) {
523 		case RH_EOF: /* otherwise clang complains */
524 		case RH_BINARY:
525 			return (false);
526 		case RH_OK:
527 			if (j == sz) {
528 				sz = sz * 3 / 2;
529 				p = xreallocarray(p, sz + 3, sizeof(*p));
530 			}
531 			p[++j].value = h;
532 		}
533 
534 	len[i] = j;
535 	file[i] = p;
536 
537 	return (true);
538 }
539 
540 static void
541 prune(void)
542 {
543 	int i, j;
544 
545 	for (pref = 0; pref < len[0] && pref < len[1] &&
546 	    file[0][pref + 1].value == file[1][pref + 1].value;
547 	    pref++)
548 		;
549 	for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
550 	    file[0][len[0] - suff].value == file[1][len[1] - suff].value;
551 	    suff++)
552 		;
553 	for (j = 0; j < 2; j++) {
554 		sfile[j] = file[j] + pref;
555 		slen[j] = len[j] - pref - suff;
556 		for (i = 0; i <= slen[j]; i++)
557 			sfile[j][i].serial = i;
558 	}
559 }
560 
561 static void
562 equiv(struct line *a, int n, struct line *b, int m, int *c)
563 {
564 	int i, j;
565 
566 	i = j = 1;
567 	while (i <= n && j <= m) {
568 		if (a[i].value < b[j].value)
569 			a[i++].value = 0;
570 		else if (a[i].value == b[j].value)
571 			a[i++].value = j;
572 		else
573 			j++;
574 	}
575 	while (i <= n)
576 		a[i++].value = 0;
577 	b[m + 1].value = 0;
578 	j = 0;
579 	while (++j <= m) {
580 		c[j] = -b[j].serial;
581 		while (b[j + 1].value == b[j].value) {
582 			j++;
583 			c[j] = b[j].serial;
584 		}
585 	}
586 	c[j] = -1;
587 }
588 
589 /* Code taken from ping.c */
590 static int
591 isqrt(int n)
592 {
593 	int y, x = 1;
594 
595 	if (n == 0)
596 		return (0);
597 
598 	do { /* newton was a stinker */
599 		y = x;
600 		x = n / x;
601 		x += y;
602 		x /= 2;
603 	} while ((x - y) > 1 || (x - y) < -1);
604 
605 	return (x);
606 }
607 
608 static int
609 stone(int *a, int n, int *b, int *c, int flags)
610 {
611 	int i, k, y, j, l;
612 	int oldc, tc, oldl, sq;
613 	unsigned numtries, bound;
614 
615 	if (flags & D_MINIMAL)
616 		bound = UINT_MAX;
617 	else {
618 		sq = isqrt(n);
619 		bound = MAX(256, sq);
620 	}
621 
622 	k = 0;
623 	c[0] = newcand(0, 0, 0);
624 	for (i = 1; i <= n; i++) {
625 		j = a[i];
626 		if (j == 0)
627 			continue;
628 		y = -b[j];
629 		oldl = 0;
630 		oldc = c[0];
631 		numtries = 0;
632 		do {
633 			if (y <= clist[oldc].y)
634 				continue;
635 			l = search(c, k, y);
636 			if (l != oldl + 1)
637 				oldc = c[l - 1];
638 			if (l <= k) {
639 				if (clist[c[l]].y <= y)
640 					continue;
641 				tc = c[l];
642 				c[l] = newcand(i, y, oldc);
643 				oldc = tc;
644 				oldl = l;
645 				numtries++;
646 			} else {
647 				c[l] = newcand(i, y, oldc);
648 				k++;
649 				break;
650 			}
651 		} while ((y = b[++j]) > 0 && numtries < bound);
652 	}
653 	return (k);
654 }
655 
656 static int
657 newcand(int x, int y, int pred)
658 {
659 	struct cand *q;
660 
661 	if (clen == clistlen) {
662 		clistlen = clistlen * 11 / 10;
663 		clist = xreallocarray(clist, clistlen, sizeof(*clist));
664 	}
665 	q = clist + clen;
666 	q->x = x;
667 	q->y = y;
668 	q->pred = pred;
669 	return (clen++);
670 }
671 
672 static int
673 search(int *c, int k, int y)
674 {
675 	int i, j, l, t;
676 
677 	if (clist[c[k]].y < y)	/* quick look for typical case */
678 		return (k + 1);
679 	i = 0;
680 	j = k + 1;
681 	for (;;) {
682 		l = (i + j) / 2;
683 		if (l <= i)
684 			break;
685 		t = clist[c[l]].y;
686 		if (t > y)
687 			j = l;
688 		else if (t < y)
689 			i = l;
690 		else
691 			return (l);
692 	}
693 	return (l + 1);
694 }
695 
696 static void
697 unravel(int p)
698 {
699 	struct cand *q;
700 	int i;
701 
702 	for (i = 0; i <= len[0]; i++)
703 		J[i] = i <= pref ? i :
704 		    i > len[0] - suff ? i + len[1] - len[0] : 0;
705 	for (q = clist + p; q->y != 0; q = clist + q->pred)
706 		J[q->x + pref] = q->y + pref;
707 }
708 
709 /*
710  * Check does double duty:
711  *  1. ferret out any fortuitous correspondences due to confounding by
712  *     hashing (which result in "jackpot")
713  *  2. collect random access indexes to the two files
714  */
715 static void
716 check(FILE *f1, FILE *f2, int flags)
717 {
718 	int i, j, jackpot, c, d;
719 	long ctold, ctnew;
720 
721 	rewind(f1);
722 	rewind(f2);
723 	j = 1;
724 	ixold[0] = ixnew[0] = 0;
725 	jackpot = 0;
726 	ctold = ctnew = 0;
727 	for (i = 1; i <= len[0]; i++) {
728 		if (J[i] == 0) {
729 			ixold[i] = ctold += skipline(f1);
730 			continue;
731 		}
732 		while (j < J[i]) {
733 			ixnew[j] = ctnew += skipline(f2);
734 			j++;
735 		}
736 		if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_STRIPCR)) {
737 			for (;;) {
738 				c = getc(f1);
739 				d = getc(f2);
740 				/*
741 				 * GNU diff ignores a missing newline
742 				 * in one file for -b or -w.
743 				 */
744 				if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) {
745 					if (c == EOF && d == '\n') {
746 						ctnew++;
747 						break;
748 					} else if (c == '\n' && d == EOF) {
749 						ctold++;
750 						break;
751 					}
752 				}
753 				ctold++;
754 				ctnew++;
755 				if (flags & D_STRIPCR && (c == '\r' || d == '\r')) {
756 					if (c == '\r') {
757 						if ((c = getc(f1)) == '\n') {
758 							ctold++;
759 						} else {
760 							ungetc(c, f1);
761 						}
762 					}
763 					if (d == '\r') {
764 						if ((d = getc(f2)) == '\n') {
765 							ctnew++;
766 						} else {
767 							ungetc(d, f2);
768 						}
769 					}
770 					break;
771 				}
772 				if ((flags & D_FOLDBLANKS) && isspace(c) &&
773 				    isspace(d)) {
774 					do {
775 						if (c == '\n')
776 							break;
777 						ctold++;
778 					} while (isspace(c = getc(f1)));
779 					do {
780 						if (d == '\n')
781 							break;
782 						ctnew++;
783 					} while (isspace(d = getc(f2)));
784 				} else if (flags & D_IGNOREBLANKS) {
785 					while (isspace(c) && c != '\n') {
786 						c = getc(f1);
787 						ctold++;
788 					}
789 					while (isspace(d) && d != '\n') {
790 						d = getc(f2);
791 						ctnew++;
792 					}
793 				}
794 				if (chrtran(c) != chrtran(d)) {
795 					jackpot++;
796 					J[i] = 0;
797 					if (c != '\n' && c != EOF)
798 						ctold += skipline(f1);
799 					if (d != '\n' && c != EOF)
800 						ctnew += skipline(f2);
801 					break;
802 				}
803 				if (c == '\n' || c == EOF)
804 					break;
805 			}
806 		} else {
807 			for (;;) {
808 				ctold++;
809 				ctnew++;
810 				if ((c = getc(f1)) != (d = getc(f2))) {
811 					/* jackpot++; */
812 					J[i] = 0;
813 					if (c != '\n' && c != EOF)
814 						ctold += skipline(f1);
815 					if (d != '\n' && c != EOF)
816 						ctnew += skipline(f2);
817 					break;
818 				}
819 				if (c == '\n' || c == EOF)
820 					break;
821 			}
822 		}
823 		ixold[i] = ctold;
824 		ixnew[j] = ctnew;
825 		j++;
826 	}
827 	for (; j <= len[1]; j++) {
828 		ixnew[j] = ctnew += skipline(f2);
829 	}
830 	/*
831 	 * if (jackpot)
832 	 *	fprintf(stderr, "jackpot\n");
833 	 */
834 }
835 
836 /* shellsort CACM #201 */
837 static void
838 sort(struct line *a, int n)
839 {
840 	struct line *ai, *aim, w;
841 	int j, m = 0, k;
842 
843 	if (n == 0)
844 		return;
845 	for (j = 1; j <= n; j *= 2)
846 		m = 2 * j - 1;
847 	for (m /= 2; m != 0; m /= 2) {
848 		k = n - m;
849 		for (j = 1; j <= k; j++) {
850 			for (ai = &a[j]; ai > a; ai -= m) {
851 				aim = &ai[m];
852 				if (aim < ai)
853 					break;	/* wraparound */
854 				if (aim->value > ai[0].value ||
855 				    (aim->value == ai[0].value &&
856 					aim->serial > ai[0].serial))
857 					break;
858 				w.value = ai[0].value;
859 				ai[0].value = aim->value;
860 				aim->value = w.value;
861 				w.serial = ai[0].serial;
862 				ai[0].serial = aim->serial;
863 				aim->serial = w.serial;
864 			}
865 		}
866 	}
867 }
868 
869 static void
870 unsort(struct line *f, int l, int *b)
871 {
872 	int *a, i;
873 
874 	a = xcalloc(l + 1, sizeof(*a));
875 	for (i = 1; i <= l; i++)
876 		a[f[i].serial] = f[i].value;
877 	for (i = 1; i <= l; i++)
878 		b[i] = a[i];
879 	free(a);
880 }
881 
882 static int
883 skipline(FILE *f)
884 {
885 	int i, c;
886 
887 	for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
888 		continue;
889 	return (i);
890 }
891 
892 static void
893 output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
894 {
895 	int i, j, m, i0, i1, j0, j1, nc;
896 
897 	rewind(f1);
898 	rewind(f2);
899 	m = len[0];
900 	J[0] = 0;
901 	J[m + 1] = len[1] + 1;
902 	if (diff_format != D_EDIT) {
903 		for (i0 = 1; i0 <= m; i0 = i1 + 1) {
904 			while (i0 <= m && J[i0] == J[i0 - 1] + 1) {
905 				if (diff_format == D_SIDEBYSIDE && suppress_common != 1) {
906 					nc = fetch(ixold, i0, i0, f1, '\0', 1, flags);
907 					print_space(nc, (hw - nc) + (padding << 1) + 1, flags);
908 					fetch(ixnew, J[i0], J[i0], f2, '\0', 0, flags);
909 					printf("\n");
910 				}
911 				i0++;
912 			}
913 			j0 = J[i0 - 1] + 1;
914 			i1 = i0 - 1;
915 			while (i1 < m && J[i1 + 1] == 0)
916 				i1++;
917 			j1 = J[i1 + 1] - 1;
918 			J[i1] = j1;
919 
920 			/*
921 			 * When using side-by-side, lines from both of the files are
922 			 * printed. The algorithm used by diff(1) identifies the ranges
923 			 * in which two files differ.
924 			 * See the change() function below.
925 			 * The for loop below consumes the shorter range, whereas one of
926 			 * the while loops deals with the longer one.
927 			 */
928 			if (diff_format == D_SIDEBYSIDE) {
929 				for (i = i0, j = j0; i <= i1 && j <= j1; i++, j++)
930 					change(file1, f1, file2, f2, i, i, j, j, &flags);
931 
932 				while (i <= i1) {
933 					change(file1, f1, file2, f2, i, i, j + 1, j, &flags);
934 					i++;
935 				}
936 
937 				while (j <= j1) {
938 					change(file1, f1, file2, f2, i + 1, i, j, j, &flags);
939 					j++;
940 				}
941 			} else
942 				change(file1, f1, file2, f2, i0, i1, j0, j1, &flags);
943 		}
944 	} else {
945 		for (i0 = m; i0 >= 1; i0 = i1 - 1) {
946 			while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
947 				i0--;
948 			j0 = J[i0 + 1] - 1;
949 			i1 = i0 + 1;
950 			while (i1 > 1 && J[i1 - 1] == 0)
951 				i1--;
952 			j1 = J[i1 - 1] + 1;
953 			J[i1] = j1;
954 			change(file1, f1, file2, f2, i1, i0, j1, j0, &flags);
955 		}
956 	}
957 	if (m == 0)
958 		change(file1, f1, file2, f2, 1, 0, 1, len[1], &flags);
959 	if (diff_format == D_IFDEF || diff_format == D_GFORMAT) {
960 		for (;;) {
961 #define	c i0
962 			if ((c = getc(f1)) == EOF)
963 				return;
964 			printf("%c", c);
965 		}
966 #undef c
967 	}
968 	if (anychange != 0) {
969 		if (diff_format == D_CONTEXT)
970 			dump_context_vec(f1, f2, flags);
971 		else if (diff_format == D_UNIFIED)
972 			dump_unified_vec(f1, f2, flags);
973 	}
974 }
975 
976 static void
977 range(int a, int b, const char *separator)
978 {
979 	printf("%d", a > b ? b : a);
980 	if (a < b)
981 		printf("%s%d", separator, b);
982 }
983 
984 static void
985 uni_range(int a, int b)
986 {
987 	if (a < b)
988 		printf("%d,%d", a, b - a + 1);
989 	else if (a == b)
990 		printf("%d", b);
991 	else
992 		printf("%d,0", b);
993 }
994 
995 static char *
996 preadline(int fd, size_t rlen, off_t off)
997 {
998 	char *line;
999 	ssize_t nr;
1000 
1001 	line = xmalloc(rlen + 1);
1002 	if ((nr = pread(fd, line, rlen, off)) == -1)
1003 		err(2, "preadline");
1004 	if (nr > 0 && line[nr-1] == '\n')
1005 		nr--;
1006 	line[nr] = '\0';
1007 	return (line);
1008 }
1009 
1010 static bool
1011 ignoreline_pattern(char *line)
1012 {
1013 	int ret;
1014 
1015 	ret = regexec(&ignore_re, line, 0, NULL, 0);
1016 	free(line);
1017 	return (ret == 0);	/* if it matched, it should be ignored. */
1018 }
1019 
1020 static bool
1021 ignoreline(char *line, bool skip_blanks)
1022 {
1023 
1024 	if (ignore_pats != NULL && skip_blanks)
1025 		return (ignoreline_pattern(line) || *line == '\0');
1026 	if (ignore_pats != NULL)
1027 		return (ignoreline_pattern(line));
1028 	if (skip_blanks)
1029 		return (*line == '\0');
1030 	/* No ignore criteria specified */
1031 	return (false);
1032 }
1033 
1034 /*
1035  * Indicate that there is a difference between lines a and b of the from file
1036  * to get to lines c to d of the to file.  If a is greater then b then there
1037  * are no lines in the from file involved and this means that there were
1038  * lines appended (beginning at b).  If c is greater than d then there are
1039  * lines missing from the to file.
1040  */
1041 static void
1042 change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
1043     int *pflags)
1044 {
1045 	static size_t max_context = 64;
1046 	long curpos;
1047 	int i, nc;
1048 	const char *walk;
1049 	bool skip_blanks;
1050 
1051 	skip_blanks = (*pflags & D_SKIPBLANKLINES);
1052 restart:
1053 	if ((diff_format != D_IFDEF || diff_format == D_GFORMAT) &&
1054 	    a > b && c > d)
1055 		return;
1056 	if (ignore_pats != NULL || skip_blanks) {
1057 		char *line;
1058 		/*
1059 		 * All lines in the change, insert, or delete must match an ignore
1060 		 * pattern for the change to be ignored.
1061 		 */
1062 		if (a <= b) {		/* Changes and deletes. */
1063 			for (i = a; i <= b; i++) {
1064 				line = preadline(fileno(f1),
1065 				    ixold[i] - ixold[i - 1], ixold[i - 1]);
1066 				if (!ignoreline(line, skip_blanks))
1067 					goto proceed;
1068 			}
1069 		}
1070 		if (a > b || c <= d) {	/* Changes and inserts. */
1071 			for (i = c; i <= d; i++) {
1072 				line = preadline(fileno(f2),
1073 				    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1074 				if (!ignoreline(line, skip_blanks))
1075 					goto proceed;
1076 			}
1077 		}
1078 		return;
1079 	}
1080 proceed:
1081 	if (*pflags & D_HEADER && diff_format != D_BRIEF) {
1082 		printf("%s %s %s\n", diffargs, file1, file2);
1083 		*pflags &= ~D_HEADER;
1084 	}
1085 	if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1086 		/*
1087 		 * Allocate change records as needed.
1088 		 */
1089 		if (context_vec_ptr == context_vec_end - 1) {
1090 			ptrdiff_t offset = context_vec_ptr - context_vec_start;
1091 			max_context <<= 1;
1092 			context_vec_start = xreallocarray(context_vec_start,
1093 			    max_context, sizeof(*context_vec_start));
1094 			context_vec_end = context_vec_start + max_context;
1095 			context_vec_ptr = context_vec_start + offset;
1096 		}
1097 		if (anychange == 0) {
1098 			/*
1099 			 * Print the context/unidiff header first time through.
1100 			 */
1101 			print_header(file1, file2);
1102 			anychange = 1;
1103 		} else if (a > context_vec_ptr->b + (2 * diff_context) + 1 &&
1104 		    c > context_vec_ptr->d + (2 * diff_context) + 1) {
1105 			/*
1106 			 * If this change is more than 'diff_context' lines from the
1107 			 * previous change, dump the record and reset it.
1108 			 */
1109 			if (diff_format == D_CONTEXT)
1110 				dump_context_vec(f1, f2, *pflags);
1111 			else
1112 				dump_unified_vec(f1, f2, *pflags);
1113 		}
1114 		context_vec_ptr++;
1115 		context_vec_ptr->a = a;
1116 		context_vec_ptr->b = b;
1117 		context_vec_ptr->c = c;
1118 		context_vec_ptr->d = d;
1119 		return;
1120 	}
1121 	if (anychange == 0)
1122 		anychange = 1;
1123 	switch (diff_format) {
1124 	case D_BRIEF:
1125 		return;
1126 	case D_NORMAL:
1127 	case D_EDIT:
1128 		range(a, b, ",");
1129 		printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1130 		if (diff_format == D_NORMAL)
1131 			range(c, d, ",");
1132 		printf("\n");
1133 		break;
1134 	case D_REVERSE:
1135 		printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1136 		range(a, b, " ");
1137 		printf("\n");
1138 		break;
1139 	case D_NREVERSE:
1140 		if (a > b)
1141 			printf("a%d %d\n", b, d - c + 1);
1142 		else {
1143 			printf("d%d %d\n", a, b - a + 1);
1144 			if (!(c > d))
1145 				/* add changed lines */
1146 				printf("a%d %d\n", b, d - c + 1);
1147 		}
1148 		break;
1149 	}
1150 	if (diff_format == D_GFORMAT) {
1151 		curpos = ftell(f1);
1152 		/* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1153 		nc = ixold[a > b ? b : a - 1] - curpos;
1154 		for (i = 0; i < nc; i++)
1155 			printf("%c", getc(f1));
1156 		for (walk = group_format; *walk != '\0'; walk++) {
1157 			if (*walk == '%') {
1158 				walk++;
1159 				switch (*walk) {
1160 				case '<':
1161 					fetch(ixold, a, b, f1, '<', 1, *pflags);
1162 					break;
1163 				case '>':
1164 					fetch(ixnew, c, d, f2, '>', 0, *pflags);
1165 					break;
1166 				default:
1167 					printf("%%%c", *walk);
1168 					break;
1169 				}
1170 				continue;
1171 			}
1172 			printf("%c", *walk);
1173 		}
1174 	}
1175 	if (diff_format == D_SIDEBYSIDE) {
1176 		if (a > b) {
1177 			print_space(0, hw + padding , *pflags);
1178 		} else {
1179 			nc = fetch(ixold, a, b, f1, '\0', 1, *pflags);
1180 			print_space(nc, hw - nc + padding, *pflags);
1181 		}
1182 		printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|'));
1183 		print_space(hw + padding + 1 , padding, *pflags);
1184 		fetch(ixnew, c, d, f2, '\0', 0, *pflags);
1185 		printf("\n");
1186 	}
1187 	if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1188 		fetch(ixold, a, b, f1, '<', 1, *pflags);
1189 		if (a <= b && c <= d && diff_format == D_NORMAL)
1190 			printf("---\n");
1191 	}
1192 	if (diff_format != D_GFORMAT && diff_format != D_SIDEBYSIDE)
1193 		fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags);
1194 	if (edoffset != 0 && diff_format == D_EDIT) {
1195 		/*
1196 		 * A non-zero edoffset value for D_EDIT indicates that the last line
1197 		 * printed was a bare dot (".") that has been escaped as ".." to
1198 		 * prevent ed(1) from misinterpreting it.  We have to add a
1199 		 * substitute command to change this back and restart where we left
1200 		 * off.
1201 		 */
1202 		printf(".\n");
1203 		printf("%ds/.//\n", a + edoffset - 1);
1204 		b = a + edoffset - 1;
1205 		a = b + 1;
1206 		c += edoffset;
1207 		goto restart;
1208 	}
1209 	if ((diff_format == D_EDIT || diff_format == D_REVERSE) && c <= d)
1210 		printf(".\n");
1211 	if (inifdef) {
1212 		printf("#endif /* %s */\n", ifdefname);
1213 		inifdef = 0;
1214 	}
1215 }
1216 
1217 static int
1218 fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
1219 {
1220 	int i, j, c, lastc, col, nc, newcol;
1221 
1222 	edoffset = 0;
1223 	nc = 0;
1224 	/*
1225 	 * When doing #ifdef's, copy down to current line
1226 	 * if this is the first file, so that stuff makes it to output.
1227 	 */
1228 	if ((diff_format == D_IFDEF) && oldfile) {
1229 		long curpos = ftell(lb);
1230 		/* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1231 		nc = f[a > b ? b : a - 1] - curpos;
1232 		for (i = 0; i < nc; i++)
1233 			printf("%c", getc(lb));
1234 	}
1235 	if (a > b)
1236 		return (0);
1237 	if (diff_format == D_IFDEF) {
1238 		if (inifdef) {
1239 			printf("#else /* %s%s */\n",
1240 			    oldfile == 1 ? "!" : "", ifdefname);
1241 		} else {
1242 			if (oldfile)
1243 				printf("#ifndef %s\n", ifdefname);
1244 			else
1245 				printf("#ifdef %s\n", ifdefname);
1246 		}
1247 		inifdef = 1 + oldfile;
1248 	}
1249 	for (i = a; i <= b; i++) {
1250 		fseek(lb, f[i - 1], SEEK_SET);
1251 		nc = f[i] - f[i - 1];
1252 		if (diff_format == D_SIDEBYSIDE && hw < nc)
1253 			nc = hw;
1254 		if (diff_format != D_IFDEF && diff_format != D_GFORMAT &&
1255 		    ch != '\0') {
1256 			printf("%c", ch);
1257 			if (Tflag && (diff_format == D_NORMAL ||
1258 			    diff_format == D_CONTEXT ||
1259 			    diff_format == D_UNIFIED))
1260 				printf("\t");
1261 			else if (diff_format != D_UNIFIED)
1262 				printf(" ");
1263 		}
1264 		col = 0;
1265 		for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1266 			c = getc(lb);
1267 			if (flags & D_STRIPCR && c == '\r') {
1268 				if ((c = getc(lb)) == '\n')
1269 					j++;
1270 				else {
1271 					ungetc(c, lb);
1272 					c = '\r';
1273 				}
1274 			}
1275 			if (c == EOF) {
1276 				if (diff_format == D_EDIT ||
1277 				    diff_format == D_REVERSE ||
1278 				    diff_format == D_NREVERSE)
1279 					warnx("No newline at end of file");
1280 				else
1281 					printf("\n\\ No newline at end of file\n");
1282 				return (col);
1283 			}
1284 			/*
1285 			 * when using --side-by-side, col needs to be increased
1286 			 * in any case to keep the columns aligned
1287 			 */
1288 			if (c == '\t') {
1289 				if (flags & D_EXPANDTABS) {
1290 					newcol = ((col / tabsize) + 1) * tabsize;
1291 					do {
1292 						if (diff_format == D_SIDEBYSIDE)
1293 							j++;
1294 						printf(" ");
1295 					} while (++col < newcol && j < nc);
1296 				} else {
1297 					if (diff_format == D_SIDEBYSIDE) {
1298 						if ((j + tabsize) > nc) {
1299 							printf("%*s", nc - j, "");
1300 							j = col = nc;
1301 						} else {
1302 							printf("\t");
1303 							col += tabsize - 1;
1304 							j += tabsize - 1;
1305 						}
1306 					} else {
1307 						printf("\t");
1308 						col++;
1309 					}
1310 				}
1311 			} else {
1312 				if (diff_format == D_EDIT && j == 1 && c == '\n' &&
1313 				    lastc == '.') {
1314 					/*
1315 					 * Don't print a bare "." line since that will confuse
1316 					 * ed(1). Print ".." instead and set the, global variable
1317 					 * edoffset to an offset from which to restart. The
1318 					 * caller must check the value of edoffset
1319 					 */
1320 					printf(".\n");
1321 					edoffset = i - a + 1;
1322 					return (edoffset);
1323 				}
1324 				/* when side-by-side, do not print a newline */
1325 				if (diff_format != D_SIDEBYSIDE || c != '\n') {
1326 					printf("%c", c);
1327 					col++;
1328 				}
1329 			}
1330 		}
1331 	}
1332 	return (col);
1333 }
1334 
1335 /*
1336  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1337  */
1338 static enum readhash
1339 readhash(FILE *f, int flags, unsigned *hash)
1340 {
1341 	int i, t, space;
1342 	unsigned sum;
1343 
1344 	sum = 1;
1345 	space = 0;
1346 	for (i = 0;;) {
1347 		switch (t = getc(f)) {
1348 		case '\0':
1349 			if ((flags & D_FORCEASCII) == 0)
1350 				return (RH_BINARY);
1351 		case '\r':
1352 			if (flags & D_STRIPCR) {
1353 				t = getc(f);
1354 				if (t == '\n')
1355 					break;
1356 				ungetc(t, f);
1357 			}
1358 			/* FALLTHROUGH */
1359 		case '\t':
1360 		case '\v':
1361 		case '\f':
1362 		case ' ':
1363 			if ((flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) != 0) {
1364 				space++;
1365 				continue;
1366 			}
1367 			/* FALLTHROUGH */
1368 		default:
1369 			if (space && (flags & D_IGNOREBLANKS) == 0) {
1370 				i++;
1371 				space = 0;
1372 			}
1373 			sum = sum * 127 + chrtran(t);
1374 			i++;
1375 			continue;
1376 		case EOF:
1377 			if (i == 0)
1378 				return (RH_EOF);
1379 			/* FALLTHROUGH */
1380 		case '\n':
1381 			break;
1382 		}
1383 		break;
1384 	}
1385 	*hash = sum;
1386 	return (RH_OK);
1387 }
1388 
1389 static int
1390 asciifile(FILE *f)
1391 {
1392 	unsigned char buf[BUFSIZ];
1393 	size_t cnt;
1394 
1395 	if (f == NULL)
1396 		return (1);
1397 
1398 	rewind(f);
1399 	cnt = fread(buf, 1, sizeof(buf), f);
1400 	return (memchr(buf, '\0', cnt) == NULL);
1401 }
1402 
1403 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre) - 1) == 0)
1404 
1405 static char *
1406 match_function(const long *f, int pos, FILE *fp)
1407 {
1408 	unsigned char buf[FUNCTION_CONTEXT_SIZE];
1409 	size_t nc;
1410 	int last = lastline;
1411 	const char *state = NULL;
1412 
1413 	lastline = pos;
1414 	while (pos > last) {
1415 		fseek(fp, f[pos - 1], SEEK_SET);
1416 		nc = f[pos] - f[pos - 1];
1417 		if (nc >= sizeof(buf))
1418 			nc = sizeof(buf) - 1;
1419 		nc = fread(buf, 1, nc, fp);
1420 		if (nc > 0) {
1421 			buf[nc] = '\0';
1422 			buf[strcspn(buf, "\n")] = '\0';
1423 			if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1424 				if (begins_with(buf, "private:")) {
1425 					if (!state)
1426 						state = " (private)";
1427 				} else if (begins_with(buf, "protected:")) {
1428 					if (!state)
1429 						state = " (protected)";
1430 				} else if (begins_with(buf, "public:")) {
1431 					if (!state)
1432 						state = " (public)";
1433 				} else {
1434 					strlcpy(lastbuf, buf, sizeof(lastbuf));
1435 					if (state)
1436 						strlcat(lastbuf, state, sizeof(lastbuf));
1437 					lastmatchline = pos;
1438 					return (lastbuf);
1439 				}
1440 			}
1441 		}
1442 		pos--;
1443 	}
1444 	return (lastmatchline > 0 ? lastbuf : NULL);
1445 }
1446 
1447 /* dump accumulated "context" diff changes */
1448 static void
1449 dump_context_vec(FILE *f1, FILE *f2, int flags)
1450 {
1451 	struct context_vec *cvp = context_vec_start;
1452 	int lowa, upb, lowc, upd, do_output;
1453 	int a, b, c, d;
1454 	char ch, *f;
1455 
1456 	if (context_vec_start > context_vec_ptr)
1457 		return;
1458 
1459 	b = d = 0;		/* gcc */
1460 	lowa = MAX(1, cvp->a - diff_context);
1461 	upb = MIN(len[0], context_vec_ptr->b + diff_context);
1462 	lowc = MAX(1, cvp->c - diff_context);
1463 	upd = MIN(len[1], context_vec_ptr->d + diff_context);
1464 
1465 	printf("***************");
1466 	if ((flags & D_PROTOTYPE)) {
1467 		f = match_function(ixold, lowa - 1, f1);
1468 		if (f != NULL)
1469 			printf(" %s", f);
1470 	}
1471 	printf("\n*** ");
1472 	range(lowa, upb, ",");
1473 	printf(" ****\n");
1474 
1475 	/*
1476 	 * Output changes to the "old" file.  The first loop suppresses
1477 	 * output if there were no changes to the "old" file (we'll see
1478 	 * the "old" lines as context in the "new" list).
1479 	 */
1480 	do_output = 0;
1481 	for (; cvp <= context_vec_ptr; cvp++)
1482 		if (cvp->a <= cvp->b) {
1483 			cvp = context_vec_start;
1484 			do_output++;
1485 			break;
1486 		}
1487 	if (do_output) {
1488 		while (cvp <= context_vec_ptr) {
1489 			a = cvp->a;
1490 			b = cvp->b;
1491 			c = cvp->c;
1492 			d = cvp->d;
1493 
1494 			if (a <= b && c <= d)
1495 				ch = 'c';
1496 			else
1497 				ch = (a <= b) ? 'd' : 'a';
1498 
1499 			if (ch == 'a')
1500 				fetch(ixold, lowa, b, f1, ' ', 0, flags);
1501 			else {
1502 				fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1503 				fetch(ixold, a, b, f1,
1504 				    ch == 'c' ? '!' : '-', 0, flags);
1505 			}
1506 			lowa = b + 1;
1507 			cvp++;
1508 		}
1509 		fetch(ixold, b + 1, upb, f1, ' ', 0, flags);
1510 	}
1511 	/* output changes to the "new" file */
1512 	printf("--- ");
1513 	range(lowc, upd, ",");
1514 	printf(" ----\n");
1515 
1516 	do_output = 0;
1517 	for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1518 		if (cvp->c <= cvp->d) {
1519 			cvp = context_vec_start;
1520 			do_output++;
1521 			break;
1522 		}
1523 	if (do_output) {
1524 		while (cvp <= context_vec_ptr) {
1525 			a = cvp->a;
1526 			b = cvp->b;
1527 			c = cvp->c;
1528 			d = cvp->d;
1529 
1530 			if (a <= b && c <= d)
1531 				ch = 'c';
1532 			else
1533 				ch = (a <= b) ? 'd' : 'a';
1534 
1535 			if (ch == 'd')
1536 				fetch(ixnew, lowc, d, f2, ' ', 0, flags);
1537 			else {
1538 				fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1539 				fetch(ixnew, c, d, f2,
1540 				    ch == 'c' ? '!' : '+', 0, flags);
1541 			}
1542 			lowc = d + 1;
1543 			cvp++;
1544 		}
1545 		fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1546 	}
1547 	context_vec_ptr = context_vec_start - 1;
1548 }
1549 
1550 /* dump accumulated "unified" diff changes */
1551 static void
1552 dump_unified_vec(FILE *f1, FILE *f2, int flags)
1553 {
1554 	struct context_vec *cvp = context_vec_start;
1555 	int lowa, upb, lowc, upd;
1556 	int a, b, c, d;
1557 	char ch, *f;
1558 
1559 	if (context_vec_start > context_vec_ptr)
1560 		return;
1561 
1562 	b = d = 0;		/* gcc */
1563 	lowa = MAX(1, cvp->a - diff_context);
1564 	upb = MIN(len[0], context_vec_ptr->b + diff_context);
1565 	lowc = MAX(1, cvp->c - diff_context);
1566 	upd = MIN(len[1], context_vec_ptr->d + diff_context);
1567 
1568 	printf("@@ -");
1569 	uni_range(lowa, upb);
1570 	printf(" +");
1571 	uni_range(lowc, upd);
1572 	printf(" @@");
1573 	if ((flags & D_PROTOTYPE)) {
1574 		f = match_function(ixold, lowa - 1, f1);
1575 		if (f != NULL)
1576 			printf(" %s", f);
1577 	}
1578 	printf("\n");
1579 
1580 	/*
1581 	 * Output changes in "unified" diff format--the old and new lines
1582 	 * are printed together.
1583 	 */
1584 	for (; cvp <= context_vec_ptr; cvp++) {
1585 		a = cvp->a;
1586 		b = cvp->b;
1587 		c = cvp->c;
1588 		d = cvp->d;
1589 
1590 		/*
1591 		 * c: both new and old changes
1592 		 * d: only changes in the old file
1593 		 * a: only changes in the new file
1594 		 */
1595 		if (a <= b && c <= d)
1596 			ch = 'c';
1597 		else
1598 			ch = (a <= b) ? 'd' : 'a';
1599 
1600 		switch (ch) {
1601 		case 'c':
1602 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1603 			fetch(ixold, a, b, f1, '-', 0, flags);
1604 			fetch(ixnew, c, d, f2, '+', 0, flags);
1605 			break;
1606 		case 'd':
1607 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1608 			fetch(ixold, a, b, f1, '-', 0, flags);
1609 			break;
1610 		case 'a':
1611 			fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1612 			fetch(ixnew, c, d, f2, '+', 0, flags);
1613 			break;
1614 		}
1615 		lowa = b + 1;
1616 		lowc = d + 1;
1617 	}
1618 	fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1619 
1620 	context_vec_ptr = context_vec_start - 1;
1621 }
1622 
1623 static void
1624 print_header(const char *file1, const char *file2)
1625 {
1626 	const char *time_format;
1627 	char buf1[256];
1628 	char buf2[256];
1629 	char end1[10];
1630 	char end2[10];
1631 	struct tm tm1, tm2, *tm_ptr1, *tm_ptr2;
1632 	int nsec1 = stb1.st_mtim.tv_nsec;
1633 	int nsec2 = stb2.st_mtim.tv_nsec;
1634 
1635 	time_format = "%Y-%m-%d %H:%M:%S";
1636 
1637 	if (cflag)
1638 		time_format = "%c";
1639 	tm_ptr1 = localtime_r(&stb1.st_mtime, &tm1);
1640 	tm_ptr2 = localtime_r(&stb2.st_mtime, &tm2);
1641 	strftime(buf1, 256, time_format, tm_ptr1);
1642 	strftime(buf2, 256, time_format, tm_ptr2);
1643 	if (!cflag) {
1644 		strftime(end1, 10, "%z", tm_ptr1);
1645 		strftime(end2, 10, "%z", tm_ptr2);
1646 		sprintf(buf1, "%s.%.9d %s", buf1, nsec1, end1);
1647 		sprintf(buf2, "%s.%.9d %s", buf2, nsec2, end2);
1648 	}
1649 	if (label[0] != NULL)
1650 		printf("%s %s\n", diff_format == D_CONTEXT ? "***" : "---",
1651 		    label[0]);
1652 	else
1653 		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "***" : "---",
1654 		    file1, buf1);
1655 	if (label[1] != NULL)
1656 		printf("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
1657 		    label[1]);
1658 	else
1659 		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "---" : "+++",
1660 		    file2, buf2);
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