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