Blame


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