xref: /dflybsd-src/usr.bin/diff/diffreg.c (revision c9733229451fac5faa53b1a016b01866eae75a1c)
1*c9733229SMatthew Dillon /*	$OpenBSD: diffreg.c,v 1.95 2021/10/24 21:24:16 deraadt Exp $	*/
2*c9733229SMatthew Dillon 
3*c9733229SMatthew Dillon /*
4*c9733229SMatthew Dillon  * Copyright (C) Caldera International Inc.  2001-2002.
5*c9733229SMatthew Dillon  * All rights reserved.
6*c9733229SMatthew Dillon  *
7*c9733229SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*c9733229SMatthew Dillon  * modification, are permitted provided that the following conditions
9*c9733229SMatthew Dillon  * are met:
10*c9733229SMatthew Dillon  * 1. Redistributions of source code and documentation must retain the above
11*c9733229SMatthew Dillon  *    copyright notice, this list of conditions and the following disclaimer.
12*c9733229SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
13*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
14*c9733229SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
15*c9733229SMatthew Dillon  * 3. All advertising materials mentioning features or use of this software
16*c9733229SMatthew Dillon  *    must display the following acknowledgement:
17*c9733229SMatthew Dillon  *	This product includes software developed or owned by Caldera
18*c9733229SMatthew Dillon  *	International, Inc.
19*c9733229SMatthew Dillon  * 4. Neither the name of Caldera International, Inc. nor the names of other
20*c9733229SMatthew Dillon  *    contributors may be used to endorse or promote products derived from
21*c9733229SMatthew Dillon  *    this software without specific prior written permission.
22*c9733229SMatthew Dillon  *
23*c9733229SMatthew Dillon  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24*c9733229SMatthew Dillon  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25*c9733229SMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26*c9733229SMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27*c9733229SMatthew Dillon  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28*c9733229SMatthew Dillon  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*c9733229SMatthew Dillon  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30*c9733229SMatthew Dillon  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*c9733229SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32*c9733229SMatthew Dillon  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33*c9733229SMatthew Dillon  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*c9733229SMatthew Dillon  * POSSIBILITY OF SUCH DAMAGE.
35*c9733229SMatthew Dillon  */
36*c9733229SMatthew Dillon /*-
37*c9733229SMatthew Dillon  * Copyright (c) 1991, 1993
38*c9733229SMatthew Dillon  *	The Regents of the University of California.  All rights reserved.
39*c9733229SMatthew Dillon  *
40*c9733229SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
41*c9733229SMatthew Dillon  * modification, are permitted provided that the following conditions
42*c9733229SMatthew Dillon  * are met:
43*c9733229SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
44*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
45*c9733229SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
46*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
47*c9733229SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
48*c9733229SMatthew Dillon  * 3. Neither the name of the University nor the names of its contributors
49*c9733229SMatthew Dillon  *    may be used to endorse or promote products derived from this software
50*c9733229SMatthew Dillon  *    without specific prior written permission.
51*c9733229SMatthew Dillon  *
52*c9733229SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53*c9733229SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54*c9733229SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55*c9733229SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56*c9733229SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57*c9733229SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58*c9733229SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59*c9733229SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60*c9733229SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61*c9733229SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62*c9733229SMatthew Dillon  * SUCH DAMAGE.
63*c9733229SMatthew Dillon  *
64*c9733229SMatthew Dillon  *	@(#)diffreg.c   8.1 (Berkeley) 6/6/93
65*c9733229SMatthew Dillon  */
66*c9733229SMatthew Dillon 
67*c9733229SMatthew Dillon #include <sys/stat.h>
68*c9733229SMatthew Dillon #include <sys/wait.h>
69*c9733229SMatthew Dillon 
70*c9733229SMatthew Dillon #include <ctype.h>
71*c9733229SMatthew Dillon #include <err.h>
72*c9733229SMatthew Dillon #include <errno.h>
73*c9733229SMatthew Dillon #include <fcntl.h>
74*c9733229SMatthew Dillon #include <paths.h>
75*c9733229SMatthew Dillon #include <stddef.h>
76*c9733229SMatthew Dillon #include <stdint.h>
77*c9733229SMatthew Dillon #include <stdio.h>
78*c9733229SMatthew Dillon #include <stdlib.h>
79*c9733229SMatthew Dillon #include <string.h>
80*c9733229SMatthew Dillon #include <unistd.h>
81*c9733229SMatthew Dillon #include <limits.h>
82*c9733229SMatthew Dillon 
83*c9733229SMatthew Dillon #include "diff.h"
84*c9733229SMatthew Dillon #include "xmalloc.h"
85*c9733229SMatthew Dillon 
86*c9733229SMatthew Dillon #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
87*c9733229SMatthew Dillon #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
88*c9733229SMatthew Dillon 
89*c9733229SMatthew Dillon /*
90*c9733229SMatthew Dillon  * diff - compare two files.
91*c9733229SMatthew Dillon  */
92*c9733229SMatthew Dillon 
93*c9733229SMatthew Dillon /*
94*c9733229SMatthew Dillon  *	Uses an algorithm due to Harold Stone, which finds
95*c9733229SMatthew Dillon  *	a pair of longest identical subsequences in the two
96*c9733229SMatthew Dillon  *	files.
97*c9733229SMatthew Dillon  *
98*c9733229SMatthew Dillon  *	The major goal is to generate the match vector J.
99*c9733229SMatthew Dillon  *	J[i] is the index of the line in file1 corresponding
100*c9733229SMatthew Dillon  *	to line i file0. J[i] = 0 if there is no
101*c9733229SMatthew Dillon  *	such line in file1.
102*c9733229SMatthew Dillon  *
103*c9733229SMatthew Dillon  *	Lines are hashed so as to work in core. All potential
104*c9733229SMatthew Dillon  *	matches are located by sorting the lines of each file
105*c9733229SMatthew Dillon  *	on the hash (called ``value''). In particular, this
106*c9733229SMatthew Dillon  *	collects the equivalence classes in file1 together.
107*c9733229SMatthew Dillon  *	Subroutine equiv replaces the value of each line in
108*c9733229SMatthew Dillon  *	file0 by the index of the first element of its
109*c9733229SMatthew Dillon  *	matching equivalence in (the reordered) file1.
110*c9733229SMatthew Dillon  *	To save space equiv squeezes file1 into a single
111*c9733229SMatthew Dillon  *	array member in which the equivalence classes
112*c9733229SMatthew Dillon  *	are simply concatenated, except that their first
113*c9733229SMatthew Dillon  *	members are flagged by changing sign.
114*c9733229SMatthew Dillon  *
115*c9733229SMatthew Dillon  *	Next the indices that point into member are unsorted into
116*c9733229SMatthew Dillon  *	array class according to the original order of file0.
117*c9733229SMatthew Dillon  *
118*c9733229SMatthew Dillon  *	The cleverness lies in routine stone. This marches
119*c9733229SMatthew Dillon  *	through the lines of file0, developing a vector klist
120*c9733229SMatthew Dillon  *	of "k-candidates". At step i a k-candidate is a matched
121*c9733229SMatthew Dillon  *	pair of lines x,y (x in file0 y in file1) such that
122*c9733229SMatthew Dillon  *	there is a common subsequence of length k
123*c9733229SMatthew Dillon  *	between the first i lines of file0 and the first y
124*c9733229SMatthew Dillon  *	lines of file1, but there is no such subsequence for
125*c9733229SMatthew Dillon  *	any smaller y. x is the earliest possible mate to y
126*c9733229SMatthew Dillon  *	that occurs in such a subsequence.
127*c9733229SMatthew Dillon  *
128*c9733229SMatthew Dillon  *	Whenever any of the members of the equivalence class of
129*c9733229SMatthew Dillon  *	lines in file1 matable to a line in file0 has serial number
130*c9733229SMatthew Dillon  *	less than the y of some k-candidate, that k-candidate
131*c9733229SMatthew Dillon  *	with the smallest such y is replaced. The new
132*c9733229SMatthew Dillon  *	k-candidate is chained (via pred) to the current
133*c9733229SMatthew Dillon  *	k-1 candidate so that the actual subsequence can
134*c9733229SMatthew Dillon  *	be recovered. When a member has serial number greater
135*c9733229SMatthew Dillon  *	that the y of all k-candidates, the klist is extended.
136*c9733229SMatthew Dillon  *	At the end, the longest subsequence is pulled out
137*c9733229SMatthew Dillon  *	and placed in the array J by unravel
138*c9733229SMatthew Dillon  *
139*c9733229SMatthew Dillon  *	With J in hand, the matches there recorded are
140*c9733229SMatthew Dillon  *	check'ed against reality to assure that no spurious
141*c9733229SMatthew Dillon  *	matches have crept in due to hashing. If they have,
142*c9733229SMatthew Dillon  *	they are broken, and "jackpot" is recorded--a harmless
143*c9733229SMatthew Dillon  *	matter except that a true match for a spuriously
144*c9733229SMatthew Dillon  *	mated line may now be unnecessarily reported as a change.
145*c9733229SMatthew Dillon  *
146*c9733229SMatthew Dillon  *	Much of the complexity of the program comes simply
147*c9733229SMatthew Dillon  *	from trying to minimize core utilization and
148*c9733229SMatthew Dillon  *	maximize the range of doable problems by dynamically
149*c9733229SMatthew Dillon  *	allocating what is needed and reusing what is not.
150*c9733229SMatthew Dillon  *	The core requirements for problems larger than somewhat
151*c9733229SMatthew Dillon  *	are (in words) 2*length(file0) + length(file1) +
152*c9733229SMatthew Dillon  *	3*(number of k-candidates installed),  typically about
153*c9733229SMatthew Dillon  *	6n words for files of length n.
154*c9733229SMatthew Dillon  */
155*c9733229SMatthew Dillon 
156*c9733229SMatthew Dillon struct cand {
157*c9733229SMatthew Dillon 	int	x;
158*c9733229SMatthew Dillon 	int	y;
159*c9733229SMatthew Dillon 	int	pred;
160*c9733229SMatthew Dillon };
161*c9733229SMatthew Dillon 
162*c9733229SMatthew Dillon struct line {
163*c9733229SMatthew Dillon 	int	serial;
164*c9733229SMatthew Dillon 	int	value;
165*c9733229SMatthew Dillon } *file[2];
166*c9733229SMatthew Dillon 
167*c9733229SMatthew Dillon /*
168*c9733229SMatthew Dillon  * The following struct is used to record change information when
169*c9733229SMatthew Dillon  * doing a "context" or "unified" diff.  (see routine "change" to
170*c9733229SMatthew Dillon  * understand the highly mnemonic field names)
171*c9733229SMatthew Dillon  */
172*c9733229SMatthew Dillon struct context_vec {
173*c9733229SMatthew Dillon 	int	a;		/* start line in old file */
174*c9733229SMatthew Dillon 	int	b;		/* end line in old file */
175*c9733229SMatthew Dillon 	int	c;		/* start line in new file */
176*c9733229SMatthew Dillon 	int	d;		/* end line in new file */
177*c9733229SMatthew Dillon };
178*c9733229SMatthew Dillon 
179*c9733229SMatthew Dillon #define	diff_output	printf
180*c9733229SMatthew Dillon static FILE	*opentemp(const char *);
181*c9733229SMatthew Dillon static void	 output(char *, FILE *, char *, FILE *, int);
182*c9733229SMatthew Dillon static void	 check(FILE *, FILE *, int);
183*c9733229SMatthew Dillon static void	 range(int, int, const char *);
184*c9733229SMatthew Dillon static void	 uni_range(int, int);
185*c9733229SMatthew Dillon static void	 dump_context_vec(FILE *, FILE *, int);
186*c9733229SMatthew Dillon static void	 dump_unified_vec(FILE *, FILE *, int);
187*c9733229SMatthew Dillon static void	 prepare(int, FILE *, size_t, int);
188*c9733229SMatthew Dillon static void	 prune(void);
189*c9733229SMatthew Dillon static void	 equiv(struct line *, int, struct line *, int, int *);
190*c9733229SMatthew Dillon static void	 unravel(int);
191*c9733229SMatthew Dillon static void	 unsort(struct line *, int, int *);
192*c9733229SMatthew Dillon static void	 change(char *, FILE *, char *, FILE *, int, int, int, int, int *);
193*c9733229SMatthew Dillon static void	 sort(struct line *, int);
194*c9733229SMatthew Dillon static void	 print_header(const char *, const char *);
195*c9733229SMatthew Dillon static int	 ignoreline(char *);
196*c9733229SMatthew Dillon static int	 asciifile(FILE *);
197*c9733229SMatthew Dillon static int	 fetch(long *, int, int, FILE *, int, int, int);
198*c9733229SMatthew Dillon static int	 newcand(int, int, int);
199*c9733229SMatthew Dillon static int	 search(int *, int, int);
200*c9733229SMatthew Dillon static int	 skipline(FILE *);
201*c9733229SMatthew Dillon static int	 isqrt(int);
202*c9733229SMatthew Dillon static int	 stone(int *, int, int *, int *, int);
203*c9733229SMatthew Dillon static int	 readhash(FILE *, int);
204*c9733229SMatthew Dillon static int	 files_differ(FILE *, FILE *, int);
205*c9733229SMatthew Dillon static char	*match_function(const long *, int, FILE *);
206*c9733229SMatthew Dillon static char	*preadline(int, size_t, off_t);
207*c9733229SMatthew Dillon 
208*c9733229SMatthew Dillon static int  *J;			/* will be overlaid on class */
209*c9733229SMatthew Dillon static int  *class;		/* will be overlaid on file[0] */
210*c9733229SMatthew Dillon static int  *klist;		/* will be overlaid on file[0] after class */
211*c9733229SMatthew Dillon static int  *member;		/* will be overlaid on file[1] */
212*c9733229SMatthew Dillon static int   clen;
213*c9733229SMatthew Dillon static int   inifdef;		/* whether or not we are in a #ifdef block */
214*c9733229SMatthew Dillon static int   len[2];
215*c9733229SMatthew Dillon static int   pref, suff;	/* length of prefix and suffix */
216*c9733229SMatthew Dillon static int   slen[2];
217*c9733229SMatthew Dillon static int   anychange;
218*c9733229SMatthew Dillon static long *ixnew;		/* will be overlaid on file[1] */
219*c9733229SMatthew Dillon static long *ixold;		/* will be overlaid on klist */
220*c9733229SMatthew Dillon static struct cand *clist;	/* merely a free storage pot for candidates */
221*c9733229SMatthew Dillon static int   clistlen;		/* the length of clist */
222*c9733229SMatthew Dillon static struct line *sfile[2];	/* shortened by pruning common prefix/suffix */
223*c9733229SMatthew Dillon static u_char *chrtran;		/* translation table for case-folding */
224*c9733229SMatthew Dillon static struct context_vec *context_vec_start;
225*c9733229SMatthew Dillon static struct context_vec *context_vec_end;
226*c9733229SMatthew Dillon static struct context_vec *context_vec_ptr;
227*c9733229SMatthew Dillon 
228*c9733229SMatthew Dillon #define FUNCTION_CONTEXT_SIZE	55
229*c9733229SMatthew Dillon static char lastbuf[FUNCTION_CONTEXT_SIZE];
230*c9733229SMatthew Dillon static int lastline;
231*c9733229SMatthew Dillon static int lastmatchline;
232*c9733229SMatthew Dillon 
233*c9733229SMatthew Dillon 
234*c9733229SMatthew Dillon /*
235*c9733229SMatthew Dillon  * chrtran points to one of 2 translation tables: cup2low if folding upper to
236*c9733229SMatthew Dillon  * lower case clow2low if not folding case
237*c9733229SMatthew Dillon  */
238*c9733229SMatthew Dillon u_char clow2low[256] = {
239*c9733229SMatthew Dillon 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
240*c9733229SMatthew Dillon 	0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
241*c9733229SMatthew Dillon 	0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
242*c9733229SMatthew Dillon 	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
243*c9733229SMatthew Dillon 	0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
244*c9733229SMatthew Dillon 	0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
245*c9733229SMatthew Dillon 	0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
246*c9733229SMatthew Dillon 	0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
247*c9733229SMatthew Dillon 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
248*c9733229SMatthew Dillon 	0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
249*c9733229SMatthew Dillon 	0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
250*c9733229SMatthew Dillon 	0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
251*c9733229SMatthew Dillon 	0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
252*c9733229SMatthew Dillon 	0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
253*c9733229SMatthew Dillon 	0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
254*c9733229SMatthew Dillon 	0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
255*c9733229SMatthew Dillon 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
256*c9733229SMatthew Dillon 	0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
257*c9733229SMatthew Dillon 	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
258*c9733229SMatthew Dillon 	0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
259*c9733229SMatthew Dillon 	0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
260*c9733229SMatthew Dillon 	0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
261*c9733229SMatthew Dillon 	0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
262*c9733229SMatthew Dillon 	0xfd, 0xfe, 0xff
263*c9733229SMatthew Dillon };
264*c9733229SMatthew Dillon 
265*c9733229SMatthew Dillon u_char cup2low[256] = {
266*c9733229SMatthew Dillon 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
267*c9733229SMatthew Dillon 	0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
268*c9733229SMatthew Dillon 	0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
269*c9733229SMatthew Dillon 	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
270*c9733229SMatthew Dillon 	0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
271*c9733229SMatthew Dillon 	0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
272*c9733229SMatthew Dillon 	0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
273*c9733229SMatthew Dillon 	0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
274*c9733229SMatthew Dillon 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
275*c9733229SMatthew Dillon 	0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
276*c9733229SMatthew Dillon 	0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
277*c9733229SMatthew Dillon 	0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
278*c9733229SMatthew Dillon 	0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
279*c9733229SMatthew Dillon 	0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
280*c9733229SMatthew Dillon 	0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
281*c9733229SMatthew Dillon 	0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
282*c9733229SMatthew Dillon 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
283*c9733229SMatthew Dillon 	0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
284*c9733229SMatthew Dillon 	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
285*c9733229SMatthew Dillon 	0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
286*c9733229SMatthew Dillon 	0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
287*c9733229SMatthew Dillon 	0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
288*c9733229SMatthew Dillon 	0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
289*c9733229SMatthew Dillon 	0xfd, 0xfe, 0xff
290*c9733229SMatthew Dillon };
291*c9733229SMatthew Dillon 
292*c9733229SMatthew Dillon int
diffreg(char * file1,char * file2,int flags)293*c9733229SMatthew Dillon diffreg(char *file1, char *file2, int flags)
294*c9733229SMatthew Dillon {
295*c9733229SMatthew Dillon 	FILE *f1, *f2;
296*c9733229SMatthew Dillon 	int i, rval;
297*c9733229SMatthew Dillon 
298*c9733229SMatthew Dillon 	f1 = f2 = NULL;
299*c9733229SMatthew Dillon 	rval = D_SAME;
300*c9733229SMatthew Dillon 	anychange = 0;
301*c9733229SMatthew Dillon 	lastline = 0;
302*c9733229SMatthew Dillon 	lastmatchline = 0;
303*c9733229SMatthew Dillon 	context_vec_ptr = context_vec_start - 1;
304*c9733229SMatthew Dillon 	if (flags & D_IGNORECASE)
305*c9733229SMatthew Dillon 		chrtran = cup2low;
306*c9733229SMatthew Dillon 	else
307*c9733229SMatthew Dillon 		chrtran = clow2low;
308*c9733229SMatthew Dillon 	if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
309*c9733229SMatthew Dillon 		return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
310*c9733229SMatthew Dillon 	if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
311*c9733229SMatthew Dillon 		goto closem;
312*c9733229SMatthew Dillon 
313*c9733229SMatthew Dillon 	if (flags & D_EMPTY1)
314*c9733229SMatthew Dillon 		f1 = fopen(_PATH_DEVNULL, "r");
315*c9733229SMatthew Dillon 	else {
316*c9733229SMatthew Dillon 		if (!S_ISREG(stb1.st_mode)) {
317*c9733229SMatthew Dillon 			if ((f1 = opentemp(file1)) == NULL ||
318*c9733229SMatthew Dillon 			    fstat(fileno(f1), &stb1) == -1) {
319*c9733229SMatthew Dillon 				warn("%s", file1);
320*c9733229SMatthew Dillon 				status |= 2;
321*c9733229SMatthew Dillon 				goto closem;
322*c9733229SMatthew Dillon 			}
323*c9733229SMatthew Dillon 		} else if (strcmp(file1, "-") == 0)
324*c9733229SMatthew Dillon 			f1 = stdin;
325*c9733229SMatthew Dillon 		else
326*c9733229SMatthew Dillon 			f1 = fopen(file1, "r");
327*c9733229SMatthew Dillon 	}
328*c9733229SMatthew Dillon 	if (f1 == NULL) {
329*c9733229SMatthew Dillon 		warn("%s", file1);
330*c9733229SMatthew Dillon 		status |= 2;
331*c9733229SMatthew Dillon 		goto closem;
332*c9733229SMatthew Dillon 	}
333*c9733229SMatthew Dillon 
334*c9733229SMatthew Dillon 	if (flags & D_EMPTY2)
335*c9733229SMatthew Dillon 		f2 = fopen(_PATH_DEVNULL, "r");
336*c9733229SMatthew Dillon 	else {
337*c9733229SMatthew Dillon 		if (!S_ISREG(stb2.st_mode)) {
338*c9733229SMatthew Dillon 			if ((f2 = opentemp(file2)) == NULL ||
339*c9733229SMatthew Dillon 			    fstat(fileno(f2), &stb2) == -1) {
340*c9733229SMatthew Dillon 				warn("%s", file2);
341*c9733229SMatthew Dillon 				status |= 2;
342*c9733229SMatthew Dillon 				goto closem;
343*c9733229SMatthew Dillon 			}
344*c9733229SMatthew Dillon 		} else if (strcmp(file2, "-") == 0)
345*c9733229SMatthew Dillon 			f2 = stdin;
346*c9733229SMatthew Dillon 		else
347*c9733229SMatthew Dillon 			f2 = fopen(file2, "r");
348*c9733229SMatthew Dillon 	}
349*c9733229SMatthew Dillon 	if (f2 == NULL) {
350*c9733229SMatthew Dillon 		warn("%s", file2);
351*c9733229SMatthew Dillon 		status |= 2;
352*c9733229SMatthew Dillon 		goto closem;
353*c9733229SMatthew Dillon 	}
354*c9733229SMatthew Dillon 
355*c9733229SMatthew Dillon 	switch (files_differ(f1, f2, flags)) {
356*c9733229SMatthew Dillon 	case 0:
357*c9733229SMatthew Dillon 		goto closem;
358*c9733229SMatthew Dillon 	case 1:
359*c9733229SMatthew Dillon 		break;
360*c9733229SMatthew Dillon 	default:
361*c9733229SMatthew Dillon 		/* error */
362*c9733229SMatthew Dillon 		status |= 2;
363*c9733229SMatthew Dillon 		goto closem;
364*c9733229SMatthew Dillon 	}
365*c9733229SMatthew Dillon 
366*c9733229SMatthew Dillon 	if ((flags & D_FORCEASCII) == 0 &&
367*c9733229SMatthew Dillon 	    (!asciifile(f1) || !asciifile(f2))) {
368*c9733229SMatthew Dillon 		rval = D_BINARY;
369*c9733229SMatthew Dillon 		status |= 1;
370*c9733229SMatthew Dillon 		goto closem;
371*c9733229SMatthew Dillon 	}
372*c9733229SMatthew Dillon 	prepare(0, f1, stb1.st_size, flags);
373*c9733229SMatthew Dillon 	prepare(1, f2, stb2.st_size, flags);
374*c9733229SMatthew Dillon 
375*c9733229SMatthew Dillon 	prune();
376*c9733229SMatthew Dillon 	sort(sfile[0], slen[0]);
377*c9733229SMatthew Dillon 	sort(sfile[1], slen[1]);
378*c9733229SMatthew Dillon 
379*c9733229SMatthew Dillon 	member = (int *)file[1];
380*c9733229SMatthew Dillon 	equiv(sfile[0], slen[0], sfile[1], slen[1], member);
381*c9733229SMatthew Dillon 	member = xreallocarray(member, slen[1] + 2, sizeof(*member));
382*c9733229SMatthew Dillon 
383*c9733229SMatthew Dillon 	class = (int *)file[0];
384*c9733229SMatthew Dillon 	unsort(sfile[0], slen[0], class);
385*c9733229SMatthew Dillon 	class = xreallocarray(class, slen[0] + 2, sizeof(*class));
386*c9733229SMatthew Dillon 
387*c9733229SMatthew Dillon 	klist = xcalloc(slen[0] + 2, sizeof(*klist));
388*c9733229SMatthew Dillon 	clen = 0;
389*c9733229SMatthew Dillon 	clistlen = 100;
390*c9733229SMatthew Dillon 	clist = xcalloc(clistlen, sizeof(*clist));
391*c9733229SMatthew Dillon 	i = stone(class, slen[0], member, klist, flags);
392*c9733229SMatthew Dillon 	free(member);
393*c9733229SMatthew Dillon 	free(class);
394*c9733229SMatthew Dillon 
395*c9733229SMatthew Dillon 	J = xreallocarray(J, len[0] + 2, sizeof(*J));
396*c9733229SMatthew Dillon 	unravel(klist[i]);
397*c9733229SMatthew Dillon 	free(clist);
398*c9733229SMatthew Dillon 	free(klist);
399*c9733229SMatthew Dillon 
400*c9733229SMatthew Dillon 	ixold = xreallocarray(ixold, len[0] + 2, sizeof(*ixold));
401*c9733229SMatthew Dillon 	ixnew = xreallocarray(ixnew, len[1] + 2, sizeof(*ixnew));
402*c9733229SMatthew Dillon 	check(f1, f2, flags);
403*c9733229SMatthew Dillon 	output(file1, f1, file2, f2, flags);
404*c9733229SMatthew Dillon closem:
405*c9733229SMatthew Dillon 	if (anychange) {
406*c9733229SMatthew Dillon 		status |= 1;
407*c9733229SMatthew Dillon 		if (rval == D_SAME)
408*c9733229SMatthew Dillon 			rval = D_DIFFER;
409*c9733229SMatthew Dillon 	}
410*c9733229SMatthew Dillon 	if (f1 != NULL)
411*c9733229SMatthew Dillon 		fclose(f1);
412*c9733229SMatthew Dillon 	if (f2 != NULL)
413*c9733229SMatthew Dillon 		fclose(f2);
414*c9733229SMatthew Dillon 
415*c9733229SMatthew Dillon 	return (rval);
416*c9733229SMatthew Dillon }
417*c9733229SMatthew Dillon 
418*c9733229SMatthew Dillon /*
419*c9733229SMatthew Dillon  * Check to see if the given files differ.
420*c9733229SMatthew Dillon  * Returns 0 if they are the same, 1 if different, and -1 on error.
421*c9733229SMatthew Dillon  * XXX - could use code from cmp(1) [faster]
422*c9733229SMatthew Dillon  */
423*c9733229SMatthew Dillon static int
files_differ(FILE * f1,FILE * f2,int flags)424*c9733229SMatthew Dillon files_differ(FILE *f1, FILE *f2, int flags)
425*c9733229SMatthew Dillon {
426*c9733229SMatthew Dillon 	char buf1[BUFSIZ], buf2[BUFSIZ];
427*c9733229SMatthew Dillon 	size_t i, j;
428*c9733229SMatthew Dillon 
429*c9733229SMatthew Dillon 	if ((flags & (D_EMPTY1|D_EMPTY2)) || stb1.st_size != stb2.st_size ||
430*c9733229SMatthew Dillon 	    (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
431*c9733229SMatthew Dillon 		return (1);
432*c9733229SMatthew Dillon 
433*c9733229SMatthew Dillon 	if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
434*c9733229SMatthew Dillon 		return (0);
435*c9733229SMatthew Dillon 
436*c9733229SMatthew Dillon 	for (;;) {
437*c9733229SMatthew Dillon 		i = fread(buf1, 1, sizeof(buf1), f1);
438*c9733229SMatthew Dillon 		j = fread(buf2, 1, sizeof(buf2), f2);
439*c9733229SMatthew Dillon 		if ((!i && ferror(f1)) || (!j && ferror(f2)))
440*c9733229SMatthew Dillon 			return (-1);
441*c9733229SMatthew Dillon 		if (i != j)
442*c9733229SMatthew Dillon 			return (1);
443*c9733229SMatthew Dillon 		if (i == 0)
444*c9733229SMatthew Dillon 			return (0);
445*c9733229SMatthew Dillon 		if (memcmp(buf1, buf2, i) != 0)
446*c9733229SMatthew Dillon 			return (1);
447*c9733229SMatthew Dillon 	}
448*c9733229SMatthew Dillon }
449*c9733229SMatthew Dillon 
450*c9733229SMatthew Dillon static FILE *
opentemp(const char * tmp_file)451*c9733229SMatthew Dillon opentemp(const char *tmp_file)
452*c9733229SMatthew Dillon {
453*c9733229SMatthew Dillon 	char buf[BUFSIZ], tempfile[PATH_MAX];
454*c9733229SMatthew Dillon 	ssize_t nread;
455*c9733229SMatthew Dillon 	int ifd, ofd;
456*c9733229SMatthew Dillon 
457*c9733229SMatthew Dillon 	if (strcmp(tmp_file, "-") == 0)
458*c9733229SMatthew Dillon 		ifd = STDIN_FILENO;
459*c9733229SMatthew Dillon 	else if ((ifd = open(tmp_file, O_RDONLY)) == -1)
460*c9733229SMatthew Dillon 		return (NULL);
461*c9733229SMatthew Dillon 
462*c9733229SMatthew Dillon 	(void)strlcpy(tempfile, _PATH_TMP "/diff.XXXXXXXX", sizeof(tempfile));
463*c9733229SMatthew Dillon 
464*c9733229SMatthew Dillon 	if ((ofd = mkstemp(tempfile)) == -1) {
465*c9733229SMatthew Dillon 		close(ifd);
466*c9733229SMatthew Dillon 		return (NULL);
467*c9733229SMatthew Dillon 	}
468*c9733229SMatthew Dillon 	unlink(tempfile);
469*c9733229SMatthew Dillon 	while ((nread = read(ifd, buf, BUFSIZ)) > 0) {
470*c9733229SMatthew Dillon 		if (write(ofd, buf, nread) != nread) {
471*c9733229SMatthew Dillon 			close(ifd);
472*c9733229SMatthew Dillon 			close(ofd);
473*c9733229SMatthew Dillon 			return (NULL);
474*c9733229SMatthew Dillon 		}
475*c9733229SMatthew Dillon 	}
476*c9733229SMatthew Dillon 	close(ifd);
477*c9733229SMatthew Dillon 	lseek(ofd, (off_t)0, SEEK_SET);
478*c9733229SMatthew Dillon 	return (fdopen(ofd, "r"));
479*c9733229SMatthew Dillon }
480*c9733229SMatthew Dillon 
481*c9733229SMatthew Dillon char *
splice(char * dir,char * splice_file)482*c9733229SMatthew Dillon splice(char *dir, char *splice_file)
483*c9733229SMatthew Dillon {
484*c9733229SMatthew Dillon 	char *tail, *buf;
485*c9733229SMatthew Dillon 	size_t dirlen;
486*c9733229SMatthew Dillon 
487*c9733229SMatthew Dillon 	dirlen = strlen(dir);
488*c9733229SMatthew Dillon 	while (dirlen != 0 && dir[dirlen - 1] == '/')
489*c9733229SMatthew Dillon 	    dirlen--;
490*c9733229SMatthew Dillon 	if ((tail = strrchr(splice_file, '/')) == NULL)
491*c9733229SMatthew Dillon 		tail = splice_file;
492*c9733229SMatthew Dillon 	else
493*c9733229SMatthew Dillon 		tail++;
494*c9733229SMatthew Dillon 	xasprintf(&buf, "%.*s/%s", (int)dirlen, dir, tail);
495*c9733229SMatthew Dillon 	return (buf);
496*c9733229SMatthew Dillon }
497*c9733229SMatthew Dillon 
498*c9733229SMatthew Dillon static void
prepare(int i,FILE * fd,size_t filesize,int flags)499*c9733229SMatthew Dillon prepare(int i, FILE *fd, size_t filesize, int flags)
500*c9733229SMatthew Dillon {
501*c9733229SMatthew Dillon 	struct line *p;
502*c9733229SMatthew Dillon 	int h;
503*c9733229SMatthew Dillon 	size_t sz, j;
504*c9733229SMatthew Dillon 
505*c9733229SMatthew Dillon 	rewind(fd);
506*c9733229SMatthew Dillon 
507*c9733229SMatthew Dillon 	sz = (filesize <= SIZE_MAX ? filesize : SIZE_MAX) / 25;
508*c9733229SMatthew Dillon 	if (sz < 100)
509*c9733229SMatthew Dillon 		sz = 100;
510*c9733229SMatthew Dillon 
511*c9733229SMatthew Dillon 	p = xcalloc(sz + 3, sizeof(*p));
512*c9733229SMatthew Dillon 	for (j = 0; (h = readhash(fd, flags));) {
513*c9733229SMatthew Dillon 		if (j == sz) {
514*c9733229SMatthew Dillon 			sz = sz * 3 / 2;
515*c9733229SMatthew Dillon 			p = xreallocarray(p, sz + 3, sizeof(*p));
516*c9733229SMatthew Dillon 		}
517*c9733229SMatthew Dillon 		p[++j].value = h;
518*c9733229SMatthew Dillon 	}
519*c9733229SMatthew Dillon 	len[i] = j;
520*c9733229SMatthew Dillon 	file[i] = p;
521*c9733229SMatthew Dillon }
522*c9733229SMatthew Dillon 
523*c9733229SMatthew Dillon static void
prune(void)524*c9733229SMatthew Dillon prune(void)
525*c9733229SMatthew Dillon {
526*c9733229SMatthew Dillon 	int i, j;
527*c9733229SMatthew Dillon 
528*c9733229SMatthew Dillon 	for (pref = 0; pref < len[0] && pref < len[1] &&
529*c9733229SMatthew Dillon 	    file[0][pref + 1].value == file[1][pref + 1].value;
530*c9733229SMatthew Dillon 	    pref++)
531*c9733229SMatthew Dillon 		;
532*c9733229SMatthew Dillon 	for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
533*c9733229SMatthew Dillon 	    file[0][len[0] - suff].value == file[1][len[1] - suff].value;
534*c9733229SMatthew Dillon 	    suff++)
535*c9733229SMatthew Dillon 		;
536*c9733229SMatthew Dillon 	for (j = 0; j < 2; j++) {
537*c9733229SMatthew Dillon 		sfile[j] = file[j] + pref;
538*c9733229SMatthew Dillon 		slen[j] = len[j] - pref - suff;
539*c9733229SMatthew Dillon 		for (i = 0; i <= slen[j]; i++)
540*c9733229SMatthew Dillon 			sfile[j][i].serial = i;
541*c9733229SMatthew Dillon 	}
542*c9733229SMatthew Dillon }
543*c9733229SMatthew Dillon 
544*c9733229SMatthew Dillon static void
equiv(struct line * a,int n,struct line * b,int m,int * c)545*c9733229SMatthew Dillon equiv(struct line *a, int n, struct line *b, int m, int *c)
546*c9733229SMatthew Dillon {
547*c9733229SMatthew Dillon 	int i, j;
548*c9733229SMatthew Dillon 
549*c9733229SMatthew Dillon 	i = j = 1;
550*c9733229SMatthew Dillon 	while (i <= n && j <= m) {
551*c9733229SMatthew Dillon 		if (a[i].value < b[j].value)
552*c9733229SMatthew Dillon 			a[i++].value = 0;
553*c9733229SMatthew Dillon 		else if (a[i].value == b[j].value)
554*c9733229SMatthew Dillon 			a[i++].value = j;
555*c9733229SMatthew Dillon 		else
556*c9733229SMatthew Dillon 			j++;
557*c9733229SMatthew Dillon 	}
558*c9733229SMatthew Dillon 	while (i <= n)
559*c9733229SMatthew Dillon 		a[i++].value = 0;
560*c9733229SMatthew Dillon 	b[m + 1].value = 0;
561*c9733229SMatthew Dillon 	j = 0;
562*c9733229SMatthew Dillon 	while (++j <= m) {
563*c9733229SMatthew Dillon 		c[j] = -b[j].serial;
564*c9733229SMatthew Dillon 		while (b[j + 1].value == b[j].value) {
565*c9733229SMatthew Dillon 			j++;
566*c9733229SMatthew Dillon 			c[j] = b[j].serial;
567*c9733229SMatthew Dillon 		}
568*c9733229SMatthew Dillon 	}
569*c9733229SMatthew Dillon 	c[j] = -1;
570*c9733229SMatthew Dillon }
571*c9733229SMatthew Dillon 
572*c9733229SMatthew Dillon /* Code taken from ping.c */
573*c9733229SMatthew Dillon static int
isqrt(int n)574*c9733229SMatthew Dillon isqrt(int n)
575*c9733229SMatthew Dillon {
576*c9733229SMatthew Dillon 	int y, x = 1;
577*c9733229SMatthew Dillon 
578*c9733229SMatthew Dillon 	if (n == 0)
579*c9733229SMatthew Dillon 		return (0);
580*c9733229SMatthew Dillon 
581*c9733229SMatthew Dillon 	do { /* newton was a stinker */
582*c9733229SMatthew Dillon 		y = x;
583*c9733229SMatthew Dillon 		x = n / x;
584*c9733229SMatthew Dillon 		x += y;
585*c9733229SMatthew Dillon 		x /= 2;
586*c9733229SMatthew Dillon 	} while ((x - y) > 1 || (x - y) < -1);
587*c9733229SMatthew Dillon 
588*c9733229SMatthew Dillon 	return (x);
589*c9733229SMatthew Dillon }
590*c9733229SMatthew Dillon 
591*c9733229SMatthew Dillon static int
stone(int * a,int n,int * b,int * c,int flags)592*c9733229SMatthew Dillon stone(int *a, int n, int *b, int *c, int flags)
593*c9733229SMatthew Dillon {
594*c9733229SMatthew Dillon 	int i, k, y, j, l;
595*c9733229SMatthew Dillon 	int oldc, tc, oldl, sq;
596*c9733229SMatthew Dillon 	u_int numtries, bound;
597*c9733229SMatthew Dillon 
598*c9733229SMatthew Dillon 	if (flags & D_MINIMAL)
599*c9733229SMatthew Dillon 		bound = UINT_MAX;
600*c9733229SMatthew Dillon 	else {
601*c9733229SMatthew Dillon 		sq = isqrt(n);
602*c9733229SMatthew Dillon 		bound = MAXIMUM(256, sq);
603*c9733229SMatthew Dillon 	}
604*c9733229SMatthew Dillon 
605*c9733229SMatthew Dillon 	k = 0;
606*c9733229SMatthew Dillon 	c[0] = newcand(0, 0, 0);
607*c9733229SMatthew Dillon 	for (i = 1; i <= n; i++) {
608*c9733229SMatthew Dillon 		j = a[i];
609*c9733229SMatthew Dillon 		if (j == 0)
610*c9733229SMatthew Dillon 			continue;
611*c9733229SMatthew Dillon 		y = -b[j];
612*c9733229SMatthew Dillon 		oldl = 0;
613*c9733229SMatthew Dillon 		oldc = c[0];
614*c9733229SMatthew Dillon 		numtries = 0;
615*c9733229SMatthew Dillon 		do {
616*c9733229SMatthew Dillon 			if (y <= clist[oldc].y)
617*c9733229SMatthew Dillon 				continue;
618*c9733229SMatthew Dillon 			l = search(c, k, y);
619*c9733229SMatthew Dillon 			if (l != oldl + 1)
620*c9733229SMatthew Dillon 				oldc = c[l - 1];
621*c9733229SMatthew Dillon 			if (l <= k) {
622*c9733229SMatthew Dillon 				if (clist[c[l]].y <= y)
623*c9733229SMatthew Dillon 					continue;
624*c9733229SMatthew Dillon 				tc = c[l];
625*c9733229SMatthew Dillon 				c[l] = newcand(i, y, oldc);
626*c9733229SMatthew Dillon 				oldc = tc;
627*c9733229SMatthew Dillon 				oldl = l;
628*c9733229SMatthew Dillon 				numtries++;
629*c9733229SMatthew Dillon 			} else {
630*c9733229SMatthew Dillon 				c[l] = newcand(i, y, oldc);
631*c9733229SMatthew Dillon 				k++;
632*c9733229SMatthew Dillon 				break;
633*c9733229SMatthew Dillon 			}
634*c9733229SMatthew Dillon 		} while ((y = b[++j]) > 0 && numtries < bound);
635*c9733229SMatthew Dillon 	}
636*c9733229SMatthew Dillon 	return (k);
637*c9733229SMatthew Dillon }
638*c9733229SMatthew Dillon 
639*c9733229SMatthew Dillon static int
newcand(int x,int y,int pred)640*c9733229SMatthew Dillon newcand(int x, int y, int pred)
641*c9733229SMatthew Dillon {
642*c9733229SMatthew Dillon 	struct cand *q;
643*c9733229SMatthew Dillon 
644*c9733229SMatthew Dillon 	if (clen == clistlen) {
645*c9733229SMatthew Dillon 		clistlen = clistlen * 11 / 10;
646*c9733229SMatthew Dillon 		clist = xreallocarray(clist, clistlen, sizeof(*clist));
647*c9733229SMatthew Dillon 	}
648*c9733229SMatthew Dillon 	q = clist + clen;
649*c9733229SMatthew Dillon 	q->x = x;
650*c9733229SMatthew Dillon 	q->y = y;
651*c9733229SMatthew Dillon 	q->pred = pred;
652*c9733229SMatthew Dillon 	return (clen++);
653*c9733229SMatthew Dillon }
654*c9733229SMatthew Dillon 
655*c9733229SMatthew Dillon static int
search(int * c,int k,int y)656*c9733229SMatthew Dillon search(int *c, int k, int y)
657*c9733229SMatthew Dillon {
658*c9733229SMatthew Dillon 	int i, j, l, t;
659*c9733229SMatthew Dillon 
660*c9733229SMatthew Dillon 	if (clist[c[k]].y < y)	/* quick look for typical case */
661*c9733229SMatthew Dillon 		return (k + 1);
662*c9733229SMatthew Dillon 	i = 0;
663*c9733229SMatthew Dillon 	j = k + 1;
664*c9733229SMatthew Dillon 	for (;;) {
665*c9733229SMatthew Dillon 		l = (i + j) / 2;
666*c9733229SMatthew Dillon 		if (l <= i)
667*c9733229SMatthew Dillon 			break;
668*c9733229SMatthew Dillon 		t = clist[c[l]].y;
669*c9733229SMatthew Dillon 		if (t > y)
670*c9733229SMatthew Dillon 			j = l;
671*c9733229SMatthew Dillon 		else if (t < y)
672*c9733229SMatthew Dillon 			i = l;
673*c9733229SMatthew Dillon 		else
674*c9733229SMatthew Dillon 			return (l);
675*c9733229SMatthew Dillon 	}
676*c9733229SMatthew Dillon 	return (l + 1);
677*c9733229SMatthew Dillon }
678*c9733229SMatthew Dillon 
679*c9733229SMatthew Dillon static void
unravel(int p)680*c9733229SMatthew Dillon unravel(int p)
681*c9733229SMatthew Dillon {
682*c9733229SMatthew Dillon 	struct cand *q;
683*c9733229SMatthew Dillon 	int i;
684*c9733229SMatthew Dillon 
685*c9733229SMatthew Dillon 	for (i = 0; i <= len[0]; i++)
686*c9733229SMatthew Dillon 		J[i] = i <= pref ? i :
687*c9733229SMatthew Dillon 		    i > len[0] - suff ? i + len[1] - len[0] : 0;
688*c9733229SMatthew Dillon 	for (q = clist + p; q->y != 0; q = clist + q->pred)
689*c9733229SMatthew Dillon 		J[q->x + pref] = q->y + pref;
690*c9733229SMatthew Dillon }
691*c9733229SMatthew Dillon 
692*c9733229SMatthew Dillon /*
693*c9733229SMatthew Dillon  * Check does double duty:
694*c9733229SMatthew Dillon  *  1.	ferret out any fortuitous correspondences due
695*c9733229SMatthew Dillon  *	to confounding by hashing (which result in "jackpot")
696*c9733229SMatthew Dillon  *  2.  collect random access indexes to the two files
697*c9733229SMatthew Dillon  */
698*c9733229SMatthew Dillon static void
check(FILE * f1,FILE * f2,int flags)699*c9733229SMatthew Dillon check(FILE *f1, FILE *f2, int flags)
700*c9733229SMatthew Dillon {
701*c9733229SMatthew Dillon 	int i, j, jackpot, c, d;
702*c9733229SMatthew Dillon 	long ctold, ctnew;
703*c9733229SMatthew Dillon 
704*c9733229SMatthew Dillon 	rewind(f1);
705*c9733229SMatthew Dillon 	rewind(f2);
706*c9733229SMatthew Dillon 	j = 1;
707*c9733229SMatthew Dillon 	ixold[0] = ixnew[0] = 0;
708*c9733229SMatthew Dillon 	jackpot = 0;
709*c9733229SMatthew Dillon 	ctold = ctnew = 0;
710*c9733229SMatthew Dillon 	for (i = 1; i <= len[0]; i++) {
711*c9733229SMatthew Dillon 		if (J[i] == 0) {
712*c9733229SMatthew Dillon 			ixold[i] = ctold += skipline(f1);
713*c9733229SMatthew Dillon 			continue;
714*c9733229SMatthew Dillon 		}
715*c9733229SMatthew Dillon 		while (j < J[i]) {
716*c9733229SMatthew Dillon 			ixnew[j] = ctnew += skipline(f2);
717*c9733229SMatthew Dillon 			j++;
718*c9733229SMatthew Dillon 		}
719*c9733229SMatthew Dillon 		if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS|D_IGNORECASE)) {
720*c9733229SMatthew Dillon 			for (;;) {
721*c9733229SMatthew Dillon 				c = getc(f1);
722*c9733229SMatthew Dillon 				d = getc(f2);
723*c9733229SMatthew Dillon 				/*
724*c9733229SMatthew Dillon 				 * GNU diff ignores a missing newline
725*c9733229SMatthew Dillon 				 * in one file for -b or -w.
726*c9733229SMatthew Dillon 				 */
727*c9733229SMatthew Dillon 				if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) {
728*c9733229SMatthew Dillon 					if (c == EOF && d == '\n') {
729*c9733229SMatthew Dillon 						ctnew++;
730*c9733229SMatthew Dillon 						break;
731*c9733229SMatthew Dillon 					} else if (c == '\n' && d == EOF) {
732*c9733229SMatthew Dillon 						ctold++;
733*c9733229SMatthew Dillon 						break;
734*c9733229SMatthew Dillon 					}
735*c9733229SMatthew Dillon 				}
736*c9733229SMatthew Dillon 				ctold++;
737*c9733229SMatthew Dillon 				ctnew++;
738*c9733229SMatthew Dillon 				if ((flags & D_FOLDBLANKS) && isspace(c) &&
739*c9733229SMatthew Dillon 				    isspace(d)) {
740*c9733229SMatthew Dillon 					do {
741*c9733229SMatthew Dillon 						if (c == '\n')
742*c9733229SMatthew Dillon 							break;
743*c9733229SMatthew Dillon 						ctold++;
744*c9733229SMatthew Dillon 					} while (isspace(c = getc(f1)));
745*c9733229SMatthew Dillon 					do {
746*c9733229SMatthew Dillon 						if (d == '\n')
747*c9733229SMatthew Dillon 							break;
748*c9733229SMatthew Dillon 						ctnew++;
749*c9733229SMatthew Dillon 					} while (isspace(d = getc(f2)));
750*c9733229SMatthew Dillon 				} else if ((flags & D_IGNOREBLANKS)) {
751*c9733229SMatthew Dillon 					while (isspace(c) && c != '\n') {
752*c9733229SMatthew Dillon 						c = getc(f1);
753*c9733229SMatthew Dillon 						ctold++;
754*c9733229SMatthew Dillon 					}
755*c9733229SMatthew Dillon 					while (isspace(d) && d != '\n') {
756*c9733229SMatthew Dillon 						d = getc(f2);
757*c9733229SMatthew Dillon 						ctnew++;
758*c9733229SMatthew Dillon 					}
759*c9733229SMatthew Dillon 				}
760*c9733229SMatthew Dillon 				if (chrtran[c] != chrtran[d]) {
761*c9733229SMatthew Dillon 					jackpot++;
762*c9733229SMatthew Dillon 					J[i] = 0;
763*c9733229SMatthew Dillon 					if (c != '\n' && c != EOF)
764*c9733229SMatthew Dillon 						ctold += skipline(f1);
765*c9733229SMatthew Dillon 					if (d != '\n' && c != EOF)
766*c9733229SMatthew Dillon 						ctnew += skipline(f2);
767*c9733229SMatthew Dillon 					break;
768*c9733229SMatthew Dillon 				}
769*c9733229SMatthew Dillon 				if (c == '\n' || c == EOF)
770*c9733229SMatthew Dillon 					break;
771*c9733229SMatthew Dillon 			}
772*c9733229SMatthew Dillon 		} else {
773*c9733229SMatthew Dillon 			for (;;) {
774*c9733229SMatthew Dillon 				ctold++;
775*c9733229SMatthew Dillon 				ctnew++;
776*c9733229SMatthew Dillon 				if ((c = getc(f1)) != (d = getc(f2))) {
777*c9733229SMatthew Dillon 					/* jackpot++; */
778*c9733229SMatthew Dillon 					J[i] = 0;
779*c9733229SMatthew Dillon 					if (c != '\n' && c != EOF)
780*c9733229SMatthew Dillon 						ctold += skipline(f1);
781*c9733229SMatthew Dillon 					if (d != '\n' && c != EOF)
782*c9733229SMatthew Dillon 						ctnew += skipline(f2);
783*c9733229SMatthew Dillon 					break;
784*c9733229SMatthew Dillon 				}
785*c9733229SMatthew Dillon 				if (c == '\n' || c == EOF)
786*c9733229SMatthew Dillon 					break;
787*c9733229SMatthew Dillon 			}
788*c9733229SMatthew Dillon 		}
789*c9733229SMatthew Dillon 		ixold[i] = ctold;
790*c9733229SMatthew Dillon 		ixnew[j] = ctnew;
791*c9733229SMatthew Dillon 		j++;
792*c9733229SMatthew Dillon 	}
793*c9733229SMatthew Dillon 	for (; j <= len[1]; j++)
794*c9733229SMatthew Dillon 		ixnew[j] = ctnew += skipline(f2);
795*c9733229SMatthew Dillon 	/*
796*c9733229SMatthew Dillon 	 * if (jackpot)
797*c9733229SMatthew Dillon 	 *	fprintf(stderr, "jackpot\n");
798*c9733229SMatthew Dillon 	 */
799*c9733229SMatthew Dillon }
800*c9733229SMatthew Dillon 
801*c9733229SMatthew Dillon /* shellsort CACM #201 */
802*c9733229SMatthew Dillon static void
sort(struct line * a,int n)803*c9733229SMatthew Dillon sort(struct line *a, int n)
804*c9733229SMatthew Dillon {
805*c9733229SMatthew Dillon 	struct line *ai, *aim, w;
806*c9733229SMatthew Dillon 	int j, m = 0, k;
807*c9733229SMatthew Dillon 
808*c9733229SMatthew Dillon 	if (n == 0)
809*c9733229SMatthew Dillon 		return;
810*c9733229SMatthew Dillon 	for (j = 1; j <= n; j *= 2)
811*c9733229SMatthew Dillon 		m = 2 * j - 1;
812*c9733229SMatthew Dillon 	for (m /= 2; m != 0; m /= 2) {
813*c9733229SMatthew Dillon 		k = n - m;
814*c9733229SMatthew Dillon 		for (j = 1; j <= k; j++) {
815*c9733229SMatthew Dillon 			for (ai = &a[j]; ai > a; ai -= m) {
816*c9733229SMatthew Dillon 				aim = &ai[m];
817*c9733229SMatthew Dillon 				if (aim < ai)
818*c9733229SMatthew Dillon 					break;	/* wraparound */
819*c9733229SMatthew Dillon 				if (aim->value > ai[0].value ||
820*c9733229SMatthew Dillon 				    (aim->value == ai[0].value &&
821*c9733229SMatthew Dillon 					aim->serial > ai[0].serial))
822*c9733229SMatthew Dillon 					break;
823*c9733229SMatthew Dillon 				w.value = ai[0].value;
824*c9733229SMatthew Dillon 				ai[0].value = aim->value;
825*c9733229SMatthew Dillon 				aim->value = w.value;
826*c9733229SMatthew Dillon 				w.serial = ai[0].serial;
827*c9733229SMatthew Dillon 				ai[0].serial = aim->serial;
828*c9733229SMatthew Dillon 				aim->serial = w.serial;
829*c9733229SMatthew Dillon 			}
830*c9733229SMatthew Dillon 		}
831*c9733229SMatthew Dillon 	}
832*c9733229SMatthew Dillon }
833*c9733229SMatthew Dillon 
834*c9733229SMatthew Dillon static void
unsort(struct line * f,int l,int * b)835*c9733229SMatthew Dillon unsort(struct line *f, int l, int *b)
836*c9733229SMatthew Dillon {
837*c9733229SMatthew Dillon 	int *a, i;
838*c9733229SMatthew Dillon 
839*c9733229SMatthew Dillon 	a = xcalloc(l + 1, sizeof(*a));
840*c9733229SMatthew Dillon 	for (i = 1; i <= l; i++)
841*c9733229SMatthew Dillon 		a[f[i].serial] = f[i].value;
842*c9733229SMatthew Dillon 	for (i = 1; i <= l; i++)
843*c9733229SMatthew Dillon 		b[i] = a[i];
844*c9733229SMatthew Dillon 	free(a);
845*c9733229SMatthew Dillon }
846*c9733229SMatthew Dillon 
847*c9733229SMatthew Dillon static int
skipline(FILE * f)848*c9733229SMatthew Dillon skipline(FILE *f)
849*c9733229SMatthew Dillon {
850*c9733229SMatthew Dillon 	int i, c;
851*c9733229SMatthew Dillon 
852*c9733229SMatthew Dillon 	for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
853*c9733229SMatthew Dillon 		continue;
854*c9733229SMatthew Dillon 	return (i);
855*c9733229SMatthew Dillon }
856*c9733229SMatthew Dillon 
857*c9733229SMatthew Dillon static void
output(char * file1,FILE * f1,char * file2,FILE * f2,int flags)858*c9733229SMatthew Dillon output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
859*c9733229SMatthew Dillon {
860*c9733229SMatthew Dillon 	int m, i0, i1, j0, j1;
861*c9733229SMatthew Dillon 
862*c9733229SMatthew Dillon 	rewind(f1);
863*c9733229SMatthew Dillon 	rewind(f2);
864*c9733229SMatthew Dillon 	m = len[0];
865*c9733229SMatthew Dillon 	J[0] = 0;
866*c9733229SMatthew Dillon 	J[m + 1] = len[1] + 1;
867*c9733229SMatthew Dillon 	if (diff_format != D_EDIT) {
868*c9733229SMatthew Dillon 		for (i0 = 1; i0 <= m; i0 = i1 + 1) {
869*c9733229SMatthew Dillon 			while (i0 <= m && J[i0] == J[i0 - 1] + 1)
870*c9733229SMatthew Dillon 				i0++;
871*c9733229SMatthew Dillon 			j0 = J[i0 - 1] + 1;
872*c9733229SMatthew Dillon 			i1 = i0 - 1;
873*c9733229SMatthew Dillon 			while (i1 < m && J[i1 + 1] == 0)
874*c9733229SMatthew Dillon 				i1++;
875*c9733229SMatthew Dillon 			j1 = J[i1 + 1] - 1;
876*c9733229SMatthew Dillon 			J[i1] = j1;
877*c9733229SMatthew Dillon 			change(file1, f1, file2, f2, i0, i1, j0, j1, &flags);
878*c9733229SMatthew Dillon 		}
879*c9733229SMatthew Dillon 	} else {
880*c9733229SMatthew Dillon 		for (i0 = m; i0 >= 1; i0 = i1 - 1) {
881*c9733229SMatthew Dillon 			while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
882*c9733229SMatthew Dillon 				i0--;
883*c9733229SMatthew Dillon 			j0 = J[i0 + 1] - 1;
884*c9733229SMatthew Dillon 			i1 = i0 + 1;
885*c9733229SMatthew Dillon 			while (i1 > 1 && J[i1 - 1] == 0)
886*c9733229SMatthew Dillon 				i1--;
887*c9733229SMatthew Dillon 			j1 = J[i1 - 1] + 1;
888*c9733229SMatthew Dillon 			J[i1] = j1;
889*c9733229SMatthew Dillon 			change(file1, f1, file2, f2, i1, i0, j1, j0, &flags);
890*c9733229SMatthew Dillon 		}
891*c9733229SMatthew Dillon 	}
892*c9733229SMatthew Dillon 	if (m == 0)
893*c9733229SMatthew Dillon 		change(file1, f1, file2, f2, 1, 0, 1, len[1], &flags);
894*c9733229SMatthew Dillon 	if (diff_format == D_IFDEF) {
895*c9733229SMatthew Dillon 		for (;;) {
896*c9733229SMatthew Dillon #define	c i0
897*c9733229SMatthew Dillon 			if ((c = getc(f1)) == EOF)
898*c9733229SMatthew Dillon 				return;
899*c9733229SMatthew Dillon 			diff_output("%c", c);
900*c9733229SMatthew Dillon 		}
901*c9733229SMatthew Dillon #undef c
902*c9733229SMatthew Dillon 	}
903*c9733229SMatthew Dillon 	if (anychange != 0) {
904*c9733229SMatthew Dillon 		if (diff_format == D_CONTEXT)
905*c9733229SMatthew Dillon 			dump_context_vec(f1, f2, flags);
906*c9733229SMatthew Dillon 		else if (diff_format == D_UNIFIED)
907*c9733229SMatthew Dillon 			dump_unified_vec(f1, f2, flags);
908*c9733229SMatthew Dillon 	}
909*c9733229SMatthew Dillon }
910*c9733229SMatthew Dillon 
911*c9733229SMatthew Dillon static void
range(int a,int b,const char * separator)912*c9733229SMatthew Dillon range(int a, int b, const char *separator)
913*c9733229SMatthew Dillon {
914*c9733229SMatthew Dillon 	diff_output("%d", a > b ? b : a);
915*c9733229SMatthew Dillon 	if (a < b)
916*c9733229SMatthew Dillon 		diff_output("%s%d", separator, b);
917*c9733229SMatthew Dillon }
918*c9733229SMatthew Dillon 
919*c9733229SMatthew Dillon static void
uni_range(int a,int b)920*c9733229SMatthew Dillon uni_range(int a, int b)
921*c9733229SMatthew Dillon {
922*c9733229SMatthew Dillon 	if (a < b)
923*c9733229SMatthew Dillon 		diff_output("%d,%d", a, b - a + 1);
924*c9733229SMatthew Dillon 	else if (a == b)
925*c9733229SMatthew Dillon 		diff_output("%d", b);
926*c9733229SMatthew Dillon 	else
927*c9733229SMatthew Dillon 		diff_output("%d,0", b);
928*c9733229SMatthew Dillon }
929*c9733229SMatthew Dillon 
930*c9733229SMatthew Dillon static char *
preadline(int fd,size_t rlen,off_t off)931*c9733229SMatthew Dillon preadline(int fd, size_t rlen, off_t off)
932*c9733229SMatthew Dillon {
933*c9733229SMatthew Dillon 	char *line;
934*c9733229SMatthew Dillon 	ssize_t nr;
935*c9733229SMatthew Dillon 
936*c9733229SMatthew Dillon 	line = xmalloc(rlen + 1);
937*c9733229SMatthew Dillon 	if ((nr = pread(fd, line, rlen, off)) == -1)
938*c9733229SMatthew Dillon 		err(2, "preadline");
939*c9733229SMatthew Dillon 	if (nr > 0 && line[nr-1] == '\n')
940*c9733229SMatthew Dillon 		nr--;
941*c9733229SMatthew Dillon 	line[nr] = '\0';
942*c9733229SMatthew Dillon 	return (line);
943*c9733229SMatthew Dillon }
944*c9733229SMatthew Dillon 
945*c9733229SMatthew Dillon static int
ignoreline(char * line)946*c9733229SMatthew Dillon ignoreline(char *line)
947*c9733229SMatthew Dillon {
948*c9733229SMatthew Dillon 	int ret;
949*c9733229SMatthew Dillon 
950*c9733229SMatthew Dillon 	ret = regexec(&ignore_re, line, 0, NULL, 0);
951*c9733229SMatthew Dillon 	free(line);
952*c9733229SMatthew Dillon 	return (ret == 0);	/* if it matched, it should be ignored. */
953*c9733229SMatthew Dillon }
954*c9733229SMatthew Dillon 
955*c9733229SMatthew Dillon /*
956*c9733229SMatthew Dillon  * Indicate that there is a difference between lines a and b of the from file
957*c9733229SMatthew Dillon  * to get to lines c to d of the to file.  If a is greater then b then there
958*c9733229SMatthew Dillon  * are no lines in the from file involved and this means that there were
959*c9733229SMatthew Dillon  * lines appended (beginning at b).  If c is greater than d then there are
960*c9733229SMatthew Dillon  * lines missing from the to file.
961*c9733229SMatthew Dillon  */
962*c9733229SMatthew Dillon static void
change(char * file1,FILE * f1,char * file2,FILE * f2,int a,int b,int c,int d,int * pflags)963*c9733229SMatthew Dillon change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
964*c9733229SMatthew Dillon     int *pflags)
965*c9733229SMatthew Dillon {
966*c9733229SMatthew Dillon 	static size_t max_context = 64;
967*c9733229SMatthew Dillon 	int i;
968*c9733229SMatthew Dillon 
969*c9733229SMatthew Dillon restart:
970*c9733229SMatthew Dillon 	if (diff_format != D_IFDEF && a > b && c > d)
971*c9733229SMatthew Dillon 		return;
972*c9733229SMatthew Dillon 	if (ignore_pats != NULL) {
973*c9733229SMatthew Dillon 		char *line;
974*c9733229SMatthew Dillon 		/*
975*c9733229SMatthew Dillon 		 * All lines in the change, insert, or delete must
976*c9733229SMatthew Dillon 		 * match an ignore pattern for the change to be
977*c9733229SMatthew Dillon 		 * ignored.
978*c9733229SMatthew Dillon 		 */
979*c9733229SMatthew Dillon 		if (a <= b) {		/* Changes and deletes. */
980*c9733229SMatthew Dillon 			for (i = a; i <= b; i++) {
981*c9733229SMatthew Dillon 				line = preadline(fileno(f1),
982*c9733229SMatthew Dillon 				    ixold[i] - ixold[i - 1], ixold[i - 1]);
983*c9733229SMatthew Dillon 				if (!ignoreline(line))
984*c9733229SMatthew Dillon 					goto proceed;
985*c9733229SMatthew Dillon 			}
986*c9733229SMatthew Dillon 		}
987*c9733229SMatthew Dillon 		if (a > b || c <= d) {	/* Changes and inserts. */
988*c9733229SMatthew Dillon 			for (i = c; i <= d; i++) {
989*c9733229SMatthew Dillon 				line = preadline(fileno(f2),
990*c9733229SMatthew Dillon 				    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
991*c9733229SMatthew Dillon 				if (!ignoreline(line))
992*c9733229SMatthew Dillon 					goto proceed;
993*c9733229SMatthew Dillon 			}
994*c9733229SMatthew Dillon 		}
995*c9733229SMatthew Dillon 		return;
996*c9733229SMatthew Dillon 	}
997*c9733229SMatthew Dillon proceed:
998*c9733229SMatthew Dillon 	if (*pflags & D_HEADER) {
999*c9733229SMatthew Dillon 		diff_output("%s %s %s\n", diffargs, file1, file2);
1000*c9733229SMatthew Dillon 		*pflags &= ~D_HEADER;
1001*c9733229SMatthew Dillon 	}
1002*c9733229SMatthew Dillon 	if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1003*c9733229SMatthew Dillon 		/*
1004*c9733229SMatthew Dillon 		 * Allocate change records as needed.
1005*c9733229SMatthew Dillon 		 */
1006*c9733229SMatthew Dillon 		if (context_vec_ptr == context_vec_end - 1) {
1007*c9733229SMatthew Dillon 			ptrdiff_t offset = context_vec_ptr - context_vec_start;
1008*c9733229SMatthew Dillon 			max_context <<= 1;
1009*c9733229SMatthew Dillon 			context_vec_start = xreallocarray(context_vec_start,
1010*c9733229SMatthew Dillon 			    max_context, sizeof(*context_vec_start));
1011*c9733229SMatthew Dillon 			context_vec_end = context_vec_start + max_context;
1012*c9733229SMatthew Dillon 			context_vec_ptr = context_vec_start + offset;
1013*c9733229SMatthew Dillon 		}
1014*c9733229SMatthew Dillon 		if (anychange == 0) {
1015*c9733229SMatthew Dillon 			/*
1016*c9733229SMatthew Dillon 			 * Print the context/unidiff header first time through.
1017*c9733229SMatthew Dillon 			 */
1018*c9733229SMatthew Dillon 			print_header(file1, file2);
1019*c9733229SMatthew Dillon 			anychange = 1;
1020*c9733229SMatthew Dillon 		} else if (a > context_vec_ptr->b + (2 * diff_context) + 1 &&
1021*c9733229SMatthew Dillon 		    c > context_vec_ptr->d + (2 * diff_context) + 1) {
1022*c9733229SMatthew Dillon 			/*
1023*c9733229SMatthew Dillon 			 * If this change is more than 'diff_context' lines from the
1024*c9733229SMatthew Dillon 			 * previous change, dump the record and reset it.
1025*c9733229SMatthew Dillon 			 */
1026*c9733229SMatthew Dillon 			if (diff_format == D_CONTEXT)
1027*c9733229SMatthew Dillon 				dump_context_vec(f1, f2, *pflags);
1028*c9733229SMatthew Dillon 			else
1029*c9733229SMatthew Dillon 				dump_unified_vec(f1, f2, *pflags);
1030*c9733229SMatthew Dillon 		}
1031*c9733229SMatthew Dillon 		context_vec_ptr++;
1032*c9733229SMatthew Dillon 		context_vec_ptr->a = a;
1033*c9733229SMatthew Dillon 		context_vec_ptr->b = b;
1034*c9733229SMatthew Dillon 		context_vec_ptr->c = c;
1035*c9733229SMatthew Dillon 		context_vec_ptr->d = d;
1036*c9733229SMatthew Dillon 		return;
1037*c9733229SMatthew Dillon 	}
1038*c9733229SMatthew Dillon 	if (anychange == 0)
1039*c9733229SMatthew Dillon 		anychange = 1;
1040*c9733229SMatthew Dillon 	switch (diff_format) {
1041*c9733229SMatthew Dillon 	case D_BRIEF:
1042*c9733229SMatthew Dillon 		return;
1043*c9733229SMatthew Dillon 	case D_NORMAL:
1044*c9733229SMatthew Dillon 	case D_EDIT:
1045*c9733229SMatthew Dillon 		range(a, b, ",");
1046*c9733229SMatthew Dillon 		diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1047*c9733229SMatthew Dillon 		if (diff_format == D_NORMAL)
1048*c9733229SMatthew Dillon 			range(c, d, ",");
1049*c9733229SMatthew Dillon 		diff_output("\n");
1050*c9733229SMatthew Dillon 		break;
1051*c9733229SMatthew Dillon 	case D_REVERSE:
1052*c9733229SMatthew Dillon 		diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1053*c9733229SMatthew Dillon 		range(a, b, " ");
1054*c9733229SMatthew Dillon 		diff_output("\n");
1055*c9733229SMatthew Dillon 		break;
1056*c9733229SMatthew Dillon 	case D_NREVERSE:
1057*c9733229SMatthew Dillon 		if (a > b)
1058*c9733229SMatthew Dillon 			diff_output("a%d %d\n", b, d - c + 1);
1059*c9733229SMatthew Dillon 		else {
1060*c9733229SMatthew Dillon 			diff_output("d%d %d\n", a, b - a + 1);
1061*c9733229SMatthew Dillon 			if (!(c > d))
1062*c9733229SMatthew Dillon 				/* add changed lines */
1063*c9733229SMatthew Dillon 				diff_output("a%d %d\n", b, d - c + 1);
1064*c9733229SMatthew Dillon 		}
1065*c9733229SMatthew Dillon 		break;
1066*c9733229SMatthew Dillon 	}
1067*c9733229SMatthew Dillon 	if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1068*c9733229SMatthew Dillon 		fetch(ixold, a, b, f1, '<', 1, *pflags);
1069*c9733229SMatthew Dillon 		if (a <= b && c <= d && diff_format == D_NORMAL)
1070*c9733229SMatthew Dillon 			diff_output("---\n");
1071*c9733229SMatthew Dillon 	}
1072*c9733229SMatthew Dillon 	i = fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags);
1073*c9733229SMatthew Dillon 	if (i != 0 && diff_format == D_EDIT) {
1074*c9733229SMatthew Dillon 		/*
1075*c9733229SMatthew Dillon 		 * A non-zero return value for D_EDIT indicates that the
1076*c9733229SMatthew Dillon 		 * last line printed was a bare dot (".") that has been
1077*c9733229SMatthew Dillon 		 * escaped as ".." to prevent ed(1) from misinterpreting
1078*c9733229SMatthew Dillon 		 * it.  We have to add a substitute command to change this
1079*c9733229SMatthew Dillon 		 * back and restart where we left off.
1080*c9733229SMatthew Dillon 		 */
1081*c9733229SMatthew Dillon 		diff_output(".\n");
1082*c9733229SMatthew Dillon 		diff_output("%ds/.//\n", a + i - 1);
1083*c9733229SMatthew Dillon 		b = a + i - 1;
1084*c9733229SMatthew Dillon 		a = b + 1;
1085*c9733229SMatthew Dillon 		c += i;
1086*c9733229SMatthew Dillon 		goto restart;
1087*c9733229SMatthew Dillon 	}
1088*c9733229SMatthew Dillon 	if ((diff_format == D_EDIT || diff_format == D_REVERSE) && c <= d)
1089*c9733229SMatthew Dillon 		diff_output(".\n");
1090*c9733229SMatthew Dillon 	if (inifdef) {
1091*c9733229SMatthew Dillon 		diff_output("#endif /* %s */\n", ifdefname);
1092*c9733229SMatthew Dillon 		inifdef = 0;
1093*c9733229SMatthew Dillon 	}
1094*c9733229SMatthew Dillon }
1095*c9733229SMatthew Dillon 
1096*c9733229SMatthew Dillon static int
fetch(long * f,int a,int b,FILE * lb,int ch,int oldfile,int flags)1097*c9733229SMatthew Dillon fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
1098*c9733229SMatthew Dillon {
1099*c9733229SMatthew Dillon 	int i, j, c, lastc, col, nc;
1100*c9733229SMatthew Dillon 
1101*c9733229SMatthew Dillon 	/*
1102*c9733229SMatthew Dillon 	 * When doing #ifdef's, copy down to current line
1103*c9733229SMatthew Dillon 	 * if this is the first file, so that stuff makes it to output.
1104*c9733229SMatthew Dillon 	 */
1105*c9733229SMatthew Dillon 	if (diff_format == D_IFDEF && oldfile) {
1106*c9733229SMatthew Dillon 		long curpos = ftell(lb);
1107*c9733229SMatthew Dillon 		/* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1108*c9733229SMatthew Dillon 		nc = f[a > b ? b : a - 1] - curpos;
1109*c9733229SMatthew Dillon 		for (i = 0; i < nc; i++)
1110*c9733229SMatthew Dillon 			diff_output("%c", getc(lb));
1111*c9733229SMatthew Dillon 	}
1112*c9733229SMatthew Dillon 	if (a > b)
1113*c9733229SMatthew Dillon 		return (0);
1114*c9733229SMatthew Dillon 	if (diff_format == D_IFDEF) {
1115*c9733229SMatthew Dillon 		if (inifdef) {
1116*c9733229SMatthew Dillon 			diff_output("#else /* %s%s */\n",
1117*c9733229SMatthew Dillon 			    oldfile == 1 ? "!" : "", ifdefname);
1118*c9733229SMatthew Dillon 		} else {
1119*c9733229SMatthew Dillon 			if (oldfile)
1120*c9733229SMatthew Dillon 				diff_output("#ifndef %s\n", ifdefname);
1121*c9733229SMatthew Dillon 			else
1122*c9733229SMatthew Dillon 				diff_output("#ifdef %s\n", ifdefname);
1123*c9733229SMatthew Dillon 		}
1124*c9733229SMatthew Dillon 		inifdef = 1 + oldfile;
1125*c9733229SMatthew Dillon 	}
1126*c9733229SMatthew Dillon 	for (i = a; i <= b; i++) {
1127*c9733229SMatthew Dillon 		fseek(lb, f[i - 1], SEEK_SET);
1128*c9733229SMatthew Dillon 		nc = f[i] - f[i - 1];
1129*c9733229SMatthew Dillon 		if (diff_format != D_IFDEF && ch != '\0') {
1130*c9733229SMatthew Dillon 			diff_output("%c", ch);
1131*c9733229SMatthew Dillon 			if (Tflag && (diff_format == D_NORMAL || diff_format == D_CONTEXT
1132*c9733229SMatthew Dillon 			    || diff_format == D_UNIFIED))
1133*c9733229SMatthew Dillon 				diff_output("\t");
1134*c9733229SMatthew Dillon 			else if (diff_format != D_UNIFIED)
1135*c9733229SMatthew Dillon 				diff_output(" ");
1136*c9733229SMatthew Dillon 		}
1137*c9733229SMatthew Dillon 		col = 0;
1138*c9733229SMatthew Dillon 		for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1139*c9733229SMatthew Dillon 			if ((c = getc(lb)) == EOF) {
1140*c9733229SMatthew Dillon 				if (diff_format == D_EDIT || diff_format == D_REVERSE ||
1141*c9733229SMatthew Dillon 				    diff_format == D_NREVERSE)
1142*c9733229SMatthew Dillon 					warnx("No newline at end of file");
1143*c9733229SMatthew Dillon 				else
1144*c9733229SMatthew Dillon 					diff_output("\n\\ No newline at end of "
1145*c9733229SMatthew Dillon 					    "file\n");
1146*c9733229SMatthew Dillon 				return (0);
1147*c9733229SMatthew Dillon 			}
1148*c9733229SMatthew Dillon 			if (c == '\t' && (flags & D_EXPANDTABS)) {
1149*c9733229SMatthew Dillon 				do {
1150*c9733229SMatthew Dillon 					diff_output(" ");
1151*c9733229SMatthew Dillon 				} while (++col & 7);
1152*c9733229SMatthew Dillon 			} else {
1153*c9733229SMatthew Dillon 				if (diff_format == D_EDIT && j == 1 && c == '\n'
1154*c9733229SMatthew Dillon 				    && lastc == '.') {
1155*c9733229SMatthew Dillon 					/*
1156*c9733229SMatthew Dillon 					 * Don't print a bare "." line
1157*c9733229SMatthew Dillon 					 * since that will confuse ed(1).
1158*c9733229SMatthew Dillon 					 * Print ".." instead and return,
1159*c9733229SMatthew Dillon 					 * giving the caller an offset
1160*c9733229SMatthew Dillon 					 * from which to restart.
1161*c9733229SMatthew Dillon 					 */
1162*c9733229SMatthew Dillon 					diff_output(".\n");
1163*c9733229SMatthew Dillon 					return (i - a + 1);
1164*c9733229SMatthew Dillon 				}
1165*c9733229SMatthew Dillon 				diff_output("%c", c);
1166*c9733229SMatthew Dillon 				col++;
1167*c9733229SMatthew Dillon 			}
1168*c9733229SMatthew Dillon 		}
1169*c9733229SMatthew Dillon 	}
1170*c9733229SMatthew Dillon 	return (0);
1171*c9733229SMatthew Dillon }
1172*c9733229SMatthew Dillon 
1173*c9733229SMatthew Dillon /*
1174*c9733229SMatthew Dillon  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1175*c9733229SMatthew Dillon  */
1176*c9733229SMatthew Dillon static int
readhash(FILE * f,int flags)1177*c9733229SMatthew Dillon readhash(FILE *f, int flags)
1178*c9733229SMatthew Dillon {
1179*c9733229SMatthew Dillon 	int i, t, space;
1180*c9733229SMatthew Dillon 	int sum;
1181*c9733229SMatthew Dillon 
1182*c9733229SMatthew Dillon 	sum = 1;
1183*c9733229SMatthew Dillon 	space = 0;
1184*c9733229SMatthew Dillon 	if ((flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) == 0) {
1185*c9733229SMatthew Dillon 		if (flags & D_IGNORECASE)
1186*c9733229SMatthew Dillon 			for (i = 0; (t = getc(f)) != '\n'; i++) {
1187*c9733229SMatthew Dillon 				if (t == EOF) {
1188*c9733229SMatthew Dillon 					if (i == 0)
1189*c9733229SMatthew Dillon 						return (0);
1190*c9733229SMatthew Dillon 					break;
1191*c9733229SMatthew Dillon 				}
1192*c9733229SMatthew Dillon 				sum = sum * 127 + chrtran[t];
1193*c9733229SMatthew Dillon 			}
1194*c9733229SMatthew Dillon 		else
1195*c9733229SMatthew Dillon 			for (i = 0; (t = getc(f)) != '\n'; i++) {
1196*c9733229SMatthew Dillon 				if (t == EOF) {
1197*c9733229SMatthew Dillon 					if (i == 0)
1198*c9733229SMatthew Dillon 						return (0);
1199*c9733229SMatthew Dillon 					break;
1200*c9733229SMatthew Dillon 				}
1201*c9733229SMatthew Dillon 				sum = sum * 127 + t;
1202*c9733229SMatthew Dillon 			}
1203*c9733229SMatthew Dillon 	} else {
1204*c9733229SMatthew Dillon 		for (i = 0;;) {
1205*c9733229SMatthew Dillon 			switch (t = getc(f)) {
1206*c9733229SMatthew Dillon 			case '\t':
1207*c9733229SMatthew Dillon 			case '\r':
1208*c9733229SMatthew Dillon 			case '\v':
1209*c9733229SMatthew Dillon 			case '\f':
1210*c9733229SMatthew Dillon 			case ' ':
1211*c9733229SMatthew Dillon 				space++;
1212*c9733229SMatthew Dillon 				continue;
1213*c9733229SMatthew Dillon 			default:
1214*c9733229SMatthew Dillon 				if (space && (flags & D_IGNOREBLANKS) == 0) {
1215*c9733229SMatthew Dillon 					i++;
1216*c9733229SMatthew Dillon 					space = 0;
1217*c9733229SMatthew Dillon 				}
1218*c9733229SMatthew Dillon 				sum = sum * 127 + chrtran[t];
1219*c9733229SMatthew Dillon 				i++;
1220*c9733229SMatthew Dillon 				continue;
1221*c9733229SMatthew Dillon 			case EOF:
1222*c9733229SMatthew Dillon 				if (i == 0)
1223*c9733229SMatthew Dillon 					return (0);
1224*c9733229SMatthew Dillon 				/* FALLTHROUGH */
1225*c9733229SMatthew Dillon 			case '\n':
1226*c9733229SMatthew Dillon 				break;
1227*c9733229SMatthew Dillon 			}
1228*c9733229SMatthew Dillon 			break;
1229*c9733229SMatthew Dillon 		}
1230*c9733229SMatthew Dillon 	}
1231*c9733229SMatthew Dillon 	/*
1232*c9733229SMatthew Dillon 	 * There is a remote possibility that we end up with a zero sum.
1233*c9733229SMatthew Dillon 	 * Zero is used as an EOF marker, so return 1 instead.
1234*c9733229SMatthew Dillon 	 */
1235*c9733229SMatthew Dillon 	return (sum == 0 ? 1 : sum);
1236*c9733229SMatthew Dillon }
1237*c9733229SMatthew Dillon 
1238*c9733229SMatthew Dillon static int
asciifile(FILE * f)1239*c9733229SMatthew Dillon asciifile(FILE *f)
1240*c9733229SMatthew Dillon {
1241*c9733229SMatthew Dillon 	unsigned char buf[BUFSIZ];
1242*c9733229SMatthew Dillon 	size_t cnt;
1243*c9733229SMatthew Dillon 
1244*c9733229SMatthew Dillon 	if (f == NULL)
1245*c9733229SMatthew Dillon 		return (1);
1246*c9733229SMatthew Dillon 
1247*c9733229SMatthew Dillon 	rewind(f);
1248*c9733229SMatthew Dillon 	cnt = fread(buf, 1, sizeof(buf), f);
1249*c9733229SMatthew Dillon 	return (memchr(buf, '\0', cnt) == NULL);
1250*c9733229SMatthew Dillon }
1251*c9733229SMatthew Dillon 
1252*c9733229SMatthew Dillon #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
1253*c9733229SMatthew Dillon 
1254*c9733229SMatthew Dillon static char *
match_function(const long * f,int pos,FILE * fp)1255*c9733229SMatthew Dillon match_function(const long *f, int pos, FILE *fp)
1256*c9733229SMatthew Dillon {
1257*c9733229SMatthew Dillon 	unsigned char buf[FUNCTION_CONTEXT_SIZE];
1258*c9733229SMatthew Dillon 	size_t nc;
1259*c9733229SMatthew Dillon 	int last = lastline;
1260*c9733229SMatthew Dillon 	const char *state = NULL;
1261*c9733229SMatthew Dillon 
1262*c9733229SMatthew Dillon 	lastline = pos;
1263*c9733229SMatthew Dillon 	while (pos > last) {
1264*c9733229SMatthew Dillon 		fseek(fp, f[pos - 1], SEEK_SET);
1265*c9733229SMatthew Dillon 		nc = f[pos] - f[pos - 1];
1266*c9733229SMatthew Dillon 		if (nc >= sizeof(buf))
1267*c9733229SMatthew Dillon 			nc = sizeof(buf) - 1;
1268*c9733229SMatthew Dillon 		nc = fread(buf, 1, nc, fp);
1269*c9733229SMatthew Dillon 		if (nc > 0) {
1270*c9733229SMatthew Dillon 			buf[nc] = '\0';
1271*c9733229SMatthew Dillon 			buf[strcspn(buf, "\n")] = '\0';
1272*c9733229SMatthew Dillon 			if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1273*c9733229SMatthew Dillon 				if (begins_with(buf, "private:")) {
1274*c9733229SMatthew Dillon 					if (!state)
1275*c9733229SMatthew Dillon 						state = " (private)";
1276*c9733229SMatthew Dillon 				} else if (begins_with(buf, "protected:")) {
1277*c9733229SMatthew Dillon 					if (!state)
1278*c9733229SMatthew Dillon 						state = " (protected)";
1279*c9733229SMatthew Dillon 				} else if (begins_with(buf, "public:")) {
1280*c9733229SMatthew Dillon 					if (!state)
1281*c9733229SMatthew Dillon 						state = " (public)";
1282*c9733229SMatthew Dillon 				} else {
1283*c9733229SMatthew Dillon 					strlcpy(lastbuf, buf, sizeof lastbuf);
1284*c9733229SMatthew Dillon 					if (state)
1285*c9733229SMatthew Dillon 						strlcat(lastbuf, state,
1286*c9733229SMatthew Dillon 						    sizeof lastbuf);
1287*c9733229SMatthew Dillon 					lastmatchline = pos;
1288*c9733229SMatthew Dillon 					return lastbuf;
1289*c9733229SMatthew Dillon 				}
1290*c9733229SMatthew Dillon 			}
1291*c9733229SMatthew Dillon 		}
1292*c9733229SMatthew Dillon 		pos--;
1293*c9733229SMatthew Dillon 	}
1294*c9733229SMatthew Dillon 	return lastmatchline > 0 ? lastbuf : NULL;
1295*c9733229SMatthew Dillon }
1296*c9733229SMatthew Dillon 
1297*c9733229SMatthew Dillon /* dump accumulated "context" diff changes */
1298*c9733229SMatthew Dillon static void
dump_context_vec(FILE * f1,FILE * f2,int flags)1299*c9733229SMatthew Dillon dump_context_vec(FILE *f1, FILE *f2, int flags)
1300*c9733229SMatthew Dillon {
1301*c9733229SMatthew Dillon 	struct context_vec *cvp = context_vec_start;
1302*c9733229SMatthew Dillon 	int lowa, upb, lowc, upd, do_output;
1303*c9733229SMatthew Dillon 	int a, b, c, d;
1304*c9733229SMatthew Dillon 	char ch, *f;
1305*c9733229SMatthew Dillon 
1306*c9733229SMatthew Dillon 	if (context_vec_start > context_vec_ptr)
1307*c9733229SMatthew Dillon 		return;
1308*c9733229SMatthew Dillon 
1309*c9733229SMatthew Dillon 	b = d = 0;		/* gcc */
1310*c9733229SMatthew Dillon 	lowa = MAXIMUM(1, cvp->a - diff_context);
1311*c9733229SMatthew Dillon 	upb = MINIMUM(len[0], context_vec_ptr->b + diff_context);
1312*c9733229SMatthew Dillon 	lowc = MAXIMUM(1, cvp->c - diff_context);
1313*c9733229SMatthew Dillon 	upd = MINIMUM(len[1], context_vec_ptr->d + diff_context);
1314*c9733229SMatthew Dillon 
1315*c9733229SMatthew Dillon 	diff_output("***************");
1316*c9733229SMatthew Dillon 	if ((flags & D_PROTOTYPE)) {
1317*c9733229SMatthew Dillon 		f = match_function(ixold, lowa-1, f1);
1318*c9733229SMatthew Dillon 		if (f != NULL)
1319*c9733229SMatthew Dillon 			diff_output(" %s", f);
1320*c9733229SMatthew Dillon 	}
1321*c9733229SMatthew Dillon 	diff_output("\n*** ");
1322*c9733229SMatthew Dillon 	range(lowa, upb, ",");
1323*c9733229SMatthew Dillon 	diff_output(" ****\n");
1324*c9733229SMatthew Dillon 
1325*c9733229SMatthew Dillon 	/*
1326*c9733229SMatthew Dillon 	 * Output changes to the "old" file.  The first loop suppresses
1327*c9733229SMatthew Dillon 	 * output if there were no changes to the "old" file (we'll see
1328*c9733229SMatthew Dillon 	 * the "old" lines as context in the "new" list).
1329*c9733229SMatthew Dillon 	 */
1330*c9733229SMatthew Dillon 	do_output = 0;
1331*c9733229SMatthew Dillon 	for (; cvp <= context_vec_ptr; cvp++)
1332*c9733229SMatthew Dillon 		if (cvp->a <= cvp->b) {
1333*c9733229SMatthew Dillon 			cvp = context_vec_start;
1334*c9733229SMatthew Dillon 			do_output++;
1335*c9733229SMatthew Dillon 			break;
1336*c9733229SMatthew Dillon 		}
1337*c9733229SMatthew Dillon 	if (do_output) {
1338*c9733229SMatthew Dillon 		while (cvp <= context_vec_ptr) {
1339*c9733229SMatthew Dillon 			a = cvp->a;
1340*c9733229SMatthew Dillon 			b = cvp->b;
1341*c9733229SMatthew Dillon 			c = cvp->c;
1342*c9733229SMatthew Dillon 			d = cvp->d;
1343*c9733229SMatthew Dillon 
1344*c9733229SMatthew Dillon 			if (a <= b && c <= d)
1345*c9733229SMatthew Dillon 				ch = 'c';
1346*c9733229SMatthew Dillon 			else
1347*c9733229SMatthew Dillon 				ch = (a <= b) ? 'd' : 'a';
1348*c9733229SMatthew Dillon 
1349*c9733229SMatthew Dillon 			if (ch == 'a')
1350*c9733229SMatthew Dillon 				fetch(ixold, lowa, b, f1, ' ', 0, flags);
1351*c9733229SMatthew Dillon 			else {
1352*c9733229SMatthew Dillon 				fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1353*c9733229SMatthew Dillon 				fetch(ixold, a, b, f1,
1354*c9733229SMatthew Dillon 				    ch == 'c' ? '!' : '-', 0, flags);
1355*c9733229SMatthew Dillon 			}
1356*c9733229SMatthew Dillon 			lowa = b + 1;
1357*c9733229SMatthew Dillon 			cvp++;
1358*c9733229SMatthew Dillon 		}
1359*c9733229SMatthew Dillon 		fetch(ixold, b + 1, upb, f1, ' ', 0, flags);
1360*c9733229SMatthew Dillon 	}
1361*c9733229SMatthew Dillon 	/* output changes to the "new" file */
1362*c9733229SMatthew Dillon 	diff_output("--- ");
1363*c9733229SMatthew Dillon 	range(lowc, upd, ",");
1364*c9733229SMatthew Dillon 	diff_output(" ----\n");
1365*c9733229SMatthew Dillon 
1366*c9733229SMatthew Dillon 	do_output = 0;
1367*c9733229SMatthew Dillon 	for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1368*c9733229SMatthew Dillon 		if (cvp->c <= cvp->d) {
1369*c9733229SMatthew Dillon 			cvp = context_vec_start;
1370*c9733229SMatthew Dillon 			do_output++;
1371*c9733229SMatthew Dillon 			break;
1372*c9733229SMatthew Dillon 		}
1373*c9733229SMatthew Dillon 	if (do_output) {
1374*c9733229SMatthew Dillon 		while (cvp <= context_vec_ptr) {
1375*c9733229SMatthew Dillon 			a = cvp->a;
1376*c9733229SMatthew Dillon 			b = cvp->b;
1377*c9733229SMatthew Dillon 			c = cvp->c;
1378*c9733229SMatthew Dillon 			d = cvp->d;
1379*c9733229SMatthew Dillon 
1380*c9733229SMatthew Dillon 			if (a <= b && c <= d)
1381*c9733229SMatthew Dillon 				ch = 'c';
1382*c9733229SMatthew Dillon 			else
1383*c9733229SMatthew Dillon 				ch = (a <= b) ? 'd' : 'a';
1384*c9733229SMatthew Dillon 
1385*c9733229SMatthew Dillon 			if (ch == 'd')
1386*c9733229SMatthew Dillon 				fetch(ixnew, lowc, d, f2, ' ', 0, flags);
1387*c9733229SMatthew Dillon 			else {
1388*c9733229SMatthew Dillon 				fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1389*c9733229SMatthew Dillon 				fetch(ixnew, c, d, f2,
1390*c9733229SMatthew Dillon 				    ch == 'c' ? '!' : '+', 0, flags);
1391*c9733229SMatthew Dillon 			}
1392*c9733229SMatthew Dillon 			lowc = d + 1;
1393*c9733229SMatthew Dillon 			cvp++;
1394*c9733229SMatthew Dillon 		}
1395*c9733229SMatthew Dillon 		fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1396*c9733229SMatthew Dillon 	}
1397*c9733229SMatthew Dillon 	context_vec_ptr = context_vec_start - 1;
1398*c9733229SMatthew Dillon }
1399*c9733229SMatthew Dillon 
1400*c9733229SMatthew Dillon /* dump accumulated "unified" diff changes */
1401*c9733229SMatthew Dillon static void
dump_unified_vec(FILE * f1,FILE * f2,int flags)1402*c9733229SMatthew Dillon dump_unified_vec(FILE *f1, FILE *f2, int flags)
1403*c9733229SMatthew Dillon {
1404*c9733229SMatthew Dillon 	struct context_vec *cvp = context_vec_start;
1405*c9733229SMatthew Dillon 	int lowa, upb, lowc, upd;
1406*c9733229SMatthew Dillon 	int a, b, c, d;
1407*c9733229SMatthew Dillon 	char ch, *f;
1408*c9733229SMatthew Dillon 
1409*c9733229SMatthew Dillon 	if (context_vec_start > context_vec_ptr)
1410*c9733229SMatthew Dillon 		return;
1411*c9733229SMatthew Dillon 
1412*c9733229SMatthew Dillon 	b = d = 0;		/* gcc */
1413*c9733229SMatthew Dillon 	lowa = MAXIMUM(1, cvp->a - diff_context);
1414*c9733229SMatthew Dillon 	upb = MINIMUM(len[0], context_vec_ptr->b + diff_context);
1415*c9733229SMatthew Dillon 	lowc = MAXIMUM(1, cvp->c - diff_context);
1416*c9733229SMatthew Dillon 	upd = MINIMUM(len[1], context_vec_ptr->d + diff_context);
1417*c9733229SMatthew Dillon 
1418*c9733229SMatthew Dillon 	diff_output("@@ -");
1419*c9733229SMatthew Dillon 	uni_range(lowa, upb);
1420*c9733229SMatthew Dillon 	diff_output(" +");
1421*c9733229SMatthew Dillon 	uni_range(lowc, upd);
1422*c9733229SMatthew Dillon 	diff_output(" @@");
1423*c9733229SMatthew Dillon 	if ((flags & D_PROTOTYPE)) {
1424*c9733229SMatthew Dillon 		f = match_function(ixold, lowa-1, f1);
1425*c9733229SMatthew Dillon 		if (f != NULL)
1426*c9733229SMatthew Dillon 			diff_output(" %s", f);
1427*c9733229SMatthew Dillon 	}
1428*c9733229SMatthew Dillon 	diff_output("\n");
1429*c9733229SMatthew Dillon 
1430*c9733229SMatthew Dillon 	/*
1431*c9733229SMatthew Dillon 	 * Output changes in "unified" diff format--the old and new lines
1432*c9733229SMatthew Dillon 	 * are printed together.
1433*c9733229SMatthew Dillon 	 */
1434*c9733229SMatthew Dillon 	for (; cvp <= context_vec_ptr; cvp++) {
1435*c9733229SMatthew Dillon 		a = cvp->a;
1436*c9733229SMatthew Dillon 		b = cvp->b;
1437*c9733229SMatthew Dillon 		c = cvp->c;
1438*c9733229SMatthew Dillon 		d = cvp->d;
1439*c9733229SMatthew Dillon 
1440*c9733229SMatthew Dillon 		/*
1441*c9733229SMatthew Dillon 		 * c: both new and old changes
1442*c9733229SMatthew Dillon 		 * d: only changes in the old file
1443*c9733229SMatthew Dillon 		 * a: only changes in the new file
1444*c9733229SMatthew Dillon 		 */
1445*c9733229SMatthew Dillon 		if (a <= b && c <= d)
1446*c9733229SMatthew Dillon 			ch = 'c';
1447*c9733229SMatthew Dillon 		else
1448*c9733229SMatthew Dillon 			ch = (a <= b) ? 'd' : 'a';
1449*c9733229SMatthew Dillon 
1450*c9733229SMatthew Dillon 		switch (ch) {
1451*c9733229SMatthew Dillon 		case 'c':
1452*c9733229SMatthew Dillon 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1453*c9733229SMatthew Dillon 			fetch(ixold, a, b, f1, '-', 0, flags);
1454*c9733229SMatthew Dillon 			fetch(ixnew, c, d, f2, '+', 0, flags);
1455*c9733229SMatthew Dillon 			break;
1456*c9733229SMatthew Dillon 		case 'd':
1457*c9733229SMatthew Dillon 			fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1458*c9733229SMatthew Dillon 			fetch(ixold, a, b, f1, '-', 0, flags);
1459*c9733229SMatthew Dillon 			break;
1460*c9733229SMatthew Dillon 		case 'a':
1461*c9733229SMatthew Dillon 			fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1462*c9733229SMatthew Dillon 			fetch(ixnew, c, d, f2, '+', 0, flags);
1463*c9733229SMatthew Dillon 			break;
1464*c9733229SMatthew Dillon 		}
1465*c9733229SMatthew Dillon 		lowa = b + 1;
1466*c9733229SMatthew Dillon 		lowc = d + 1;
1467*c9733229SMatthew Dillon 	}
1468*c9733229SMatthew Dillon 	fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1469*c9733229SMatthew Dillon 
1470*c9733229SMatthew Dillon 	context_vec_ptr = context_vec_start - 1;
1471*c9733229SMatthew Dillon }
1472*c9733229SMatthew Dillon 
1473*c9733229SMatthew Dillon static void
print_header(const char * file1,const char * file2)1474*c9733229SMatthew Dillon print_header(const char *file1, const char *file2)
1475*c9733229SMatthew Dillon {
1476*c9733229SMatthew Dillon 	if (label[0] != NULL)
1477*c9733229SMatthew Dillon 		diff_output("%s %s\n", diff_format == D_CONTEXT ? "***" : "---",
1478*c9733229SMatthew Dillon 		    label[0]);
1479*c9733229SMatthew Dillon 	else
1480*c9733229SMatthew Dillon 		diff_output("%s %s\t%s", diff_format == D_CONTEXT ? "***" : "---",
1481*c9733229SMatthew Dillon 		    file1, ctime(&stb1.st_mtime));
1482*c9733229SMatthew Dillon 	if (label[1] != NULL)
1483*c9733229SMatthew Dillon 		diff_output("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
1484*c9733229SMatthew Dillon 		    label[1]);
1485*c9733229SMatthew Dillon 	else
1486*c9733229SMatthew Dillon 		diff_output("%s %s\t%s", diff_format == D_CONTEXT ? "---" : "+++",
1487*c9733229SMatthew Dillon 		    file2, ctime(&stb2.st_mtime));
1488*c9733229SMatthew Dillon }
1489