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