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