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