Commit | Line | Data |
---|---|---|
2f861666 | 1 | static char sccsid[] = "@(#)diffreg.c 4.14 %G%"; |
43454f97 BJ |
2 | |
3 | #include "diff.h" | |
4 | /* | |
5 | * diff - compare two files. | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Uses an algorithm due to Harold Stone, which finds | |
10 | * a pair of longest identical subsequences in the two | |
11 | * files. | |
12 | * | |
13 | * The major goal is to generate the match vector J. | |
14 | * J[i] is the index of the line in file1 corresponding | |
15 | * to line i file0. J[i] = 0 if there is no | |
16 | * such line in file1. | |
17 | * | |
18 | * Lines are hashed so as to work in core. All potential | |
19 | * matches are located by sorting the lines of each file | |
20 | * on the hash (called ``value''). In particular, this | |
21 | * collects the equivalence classes in file1 together. | |
22 | * Subroutine equiv replaces the value of each line in | |
23 | * file0 by the index of the first element of its | |
24 | * matching equivalence in (the reordered) file1. | |
25 | * To save space equiv squeezes file1 into a single | |
26 | * array member in which the equivalence classes | |
27 | * are simply concatenated, except that their first | |
28 | * members are flagged by changing sign. | |
29 | * | |
30 | * Next the indices that point into member are unsorted into | |
31 | * array class according to the original order of file0. | |
32 | * | |
33 | * The cleverness lies in routine stone. This marches | |
34 | * through the lines of file0, developing a vector klist | |
35 | * of "k-candidates". At step i a k-candidate is a matched | |
36 | * pair of lines x,y (x in file0 y in file1) such that | |
37 | * there is a common subsequence of length k | |
38 | * between the first i lines of file0 and the first y | |
39 | * lines of file1, but there is no such subsequence for | |
40 | * any smaller y. x is the earliest possible mate to y | |
41 | * that occurs in such a subsequence. | |
42 | * | |
43 | * Whenever any of the members of the equivalence class of | |
44 | * lines in file1 matable to a line in file0 has serial number | |
45 | * less than the y of some k-candidate, that k-candidate | |
46 | * with the smallest such y is replaced. The new | |
47 | * k-candidate is chained (via pred) to the current | |
48 | * k-1 candidate so that the actual subsequence can | |
49 | * be recovered. When a member has serial number greater | |
50 | * that the y of all k-candidates, the klist is extended. | |
51 | * At the end, the longest subsequence is pulled out | |
52 | * and placed in the array J by unravel | |
53 | * | |
54 | * With J in hand, the matches there recorded are | |
55 | * check'ed against reality to assure that no spurious | |
56 | * matches have crept in due to hashing. If they have, | |
57 | * they are broken, and "jackpot" is recorded--a harmless | |
58 | * matter except that a true match for a spuriously | |
59 | * mated line may now be unnecessarily reported as a change. | |
60 | * | |
61 | * Much of the complexity of the program comes simply | |
62 | * from trying to minimize core utilization and | |
63 | * maximize the range of doable problems by dynamically | |
64 | * allocating what is needed and reusing what is not. | |
65 | * The core requirements for problems larger than somewhat | |
66 | * are (in words) 2*length(file0) + length(file1) + | |
67 | * 3*(number of k-candidates installed), typically about | |
68 | * 6n words for files of length n. | |
69 | */ | |
70 | ||
71 | #define prints(s) fputs(s,stdout) | |
72 | ||
73 | FILE *input[2]; | |
74 | FILE *fopen(); | |
75 | ||
76 | struct cand { | |
77 | int x; | |
78 | int y; | |
79 | int pred; | |
80 | } cand; | |
81 | struct line { | |
82 | int serial; | |
83 | int value; | |
84 | } *file[2], line; | |
85 | int len[2]; | |
86 | struct line *sfile[2]; /* shortened by pruning common prefix and suffix */ | |
87 | int slen[2]; | |
88 | int pref, suff; /* length of prefix and suffix */ | |
89 | int *class; /* will be overlaid on file[0] */ | |
90 | int *member; /* will be overlaid on file[1] */ | |
91 | int *klist; /* will be overlaid on file[0] after class */ | |
92 | struct cand *clist; /* merely a free storage pot for candidates */ | |
93 | int clen = 0; | |
94 | int *J; /* will be overlaid on class */ | |
95 | long *ixold; /* will be overlaid on klist */ | |
96 | long *ixnew; /* will be overlaid on file[1] */ | |
c470b1f1 VJ |
97 | char *chrtran; /* translation table for case-folding */ |
98 | ||
99 | /* chrtran points to one of 2 translation tables: | |
100 | * cup2low if folding upper to lower case | |
101 | * clow2low if not folding case | |
102 | */ | |
103 | char clow2low[256] = { | |
104 | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, | |
105 | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, | |
106 | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, | |
107 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, | |
108 | 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, | |
109 | 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f, | |
110 | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, | |
111 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f, | |
112 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, | |
113 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, | |
114 | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, | |
115 | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, | |
116 | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, | |
117 | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf, | |
118 | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, | |
119 | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff | |
120 | }; | |
121 | ||
122 | char cup2low[256] = { | |
123 | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, | |
124 | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, | |
125 | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, | |
126 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, | |
127 | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, | |
128 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f, | |
129 | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, | |
130 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f, | |
131 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, | |
132 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, | |
133 | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, | |
134 | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, | |
135 | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, | |
136 | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf, | |
137 | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, | |
138 | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff | |
139 | }; | |
43454f97 BJ |
140 | |
141 | diffreg() | |
142 | { | |
87f863b7 KM |
143 | register int i, j; |
144 | FILE *f1, *f2; | |
145 | char buf1[BUFSIZ], buf2[BUFSIZ]; | |
43454f97 BJ |
146 | |
147 | if (hflag) { | |
148 | diffargv[0] = "diffh"; | |
149 | execv(diffh, diffargv); | |
150 | fprintf(stderr, "diff: "); | |
151 | perror(diffh); | |
152 | done(); | |
153 | } | |
c470b1f1 | 154 | chrtran = (iflag? cup2low : clow2low); |
2f861666 | 155 | if ((stb1.st_mode & S_IFMT) == S_IFDIR) { |
43454f97 | 156 | file1 = splice(file1, file2); |
2f861666 KM |
157 | if (stat(file1, &stb1) < 0) { |
158 | fprintf(stderr, "diff: "); | |
159 | perror(file1); | |
160 | done(); | |
161 | } | |
162 | } else if ((stb2.st_mode & S_IFMT) == S_IFDIR) { | |
43454f97 | 163 | file2 = splice(file2, file1); |
2f861666 KM |
164 | if (stat(file2, &stb2) < 0) { |
165 | fprintf(stderr, "diff: "); | |
166 | perror(file2); | |
167 | done(); | |
168 | } | |
169 | } else if (!strcmp(file1, "-")) { | |
43454f97 BJ |
170 | if (!strcmp(file2, "-")) { |
171 | fprintf(stderr, "diff: can't specify - -\n"); | |
172 | done(); | |
173 | } | |
174 | file1 = copytemp(); | |
2f861666 KM |
175 | if (stat(file1, &stb1) < 0) { |
176 | fprintf(stderr, "diff: "); | |
177 | perror(file1); | |
178 | done(); | |
179 | } | |
180 | } else if (!strcmp(file2, "-")) { | |
43454f97 | 181 | file2 = copytemp(); |
2f861666 KM |
182 | if (stat(file2, &stb2) < 0) { |
183 | fprintf(stderr, "diff: "); | |
184 | perror(file2); | |
185 | done(); | |
186 | } | |
187 | } | |
87f863b7 KM |
188 | if ((f1 = fopen(file1, "r")) == NULL) { |
189 | fprintf(stderr, "diff: "); | |
190 | perror(file1); | |
87f863b7 KM |
191 | done(); |
192 | } | |
193 | if ((f2 = fopen(file2, "r")) == NULL) { | |
194 | fprintf(stderr, "diff: "); | |
195 | perror(file2); | |
196 | fclose(f1); | |
87f863b7 KM |
197 | done(); |
198 | } | |
199 | if (stb1.st_size != stb2.st_size) | |
200 | goto notsame; | |
201 | for (;;) { | |
6f7a12ee RH |
202 | i = fread(buf1, 1, BUFSIZ, f1); |
203 | j = fread(buf2, 1, BUFSIZ, f2); | |
87f863b7 KM |
204 | if (i < 0 || j < 0 || i != j) |
205 | goto notsame; | |
206 | if (i == 0 && j == 0) { | |
207 | fclose(f1); | |
208 | fclose(f2); | |
d6790f57 | 209 | status = 0; /* files don't differ */ |
87f863b7 KM |
210 | goto same; |
211 | } | |
212 | for (j = 0; j < i; j++) | |
213 | if (buf1[j] != buf2[j]) | |
214 | goto notsame; | |
215 | } | |
216 | notsame: | |
d6790f57 RH |
217 | /* |
218 | * Files certainly differ at this point; set status accordingly | |
219 | */ | |
220 | status = 1; | |
46235152 | 221 | if (!asciifile(f1) || !asciifile(f2)) { |
87f863b7 KM |
222 | printf("Binary files %s and %s differ\n", file1, file2); |
223 | fclose(f1); | |
224 | fclose(f2); | |
225 | done(); | |
226 | } | |
227 | prepare(0, f1); | |
228 | prepare(1, f2); | |
229 | fclose(f1); | |
230 | fclose(f2); | |
43454f97 BJ |
231 | prune(); |
232 | sort(sfile[0],slen[0]); | |
233 | sort(sfile[1],slen[1]); | |
234 | ||
235 | member = (int *)file[1]; | |
236 | equiv(sfile[0], slen[0], sfile[1], slen[1], member); | |
237 | member = (int *)ralloc((char *)member,(slen[1]+2)*sizeof(int)); | |
238 | ||
239 | class = (int *)file[0]; | |
240 | unsort(sfile[0], slen[0], class); | |
241 | class = (int *)ralloc((char *)class,(slen[0]+2)*sizeof(int)); | |
242 | ||
243 | klist = (int *)talloc((slen[0]+2)*sizeof(int)); | |
244 | clist = (struct cand *)talloc(sizeof(cand)); | |
87f863b7 | 245 | i = stone(class, slen[0], member, klist); |
43454f97 BJ |
246 | free((char *)member); |
247 | free((char *)class); | |
248 | ||
249 | J = (int *)talloc((len[0]+2)*sizeof(int)); | |
87f863b7 | 250 | unravel(klist[i]); |
43454f97 BJ |
251 | free((char *)clist); |
252 | free((char *)klist); | |
253 | ||
254 | ixold = (long *)talloc((len[0]+2)*sizeof(long)); | |
255 | ixnew = (long *)talloc((len[1]+2)*sizeof(long)); | |
256 | check(); | |
257 | output(); | |
258 | status = anychange; | |
87f863b7 | 259 | same: |
43454f97 BJ |
260 | if (opt == D_CONTEXT && anychange == 0) |
261 | printf("No differences encountered\n"); | |
262 | done(); | |
263 | } | |
264 | ||
265 | char * | |
266 | copytemp() | |
267 | { | |
268 | char buf[BUFSIZ]; | |
269 | register int i, f; | |
270 | ||
271 | signal(SIGHUP,done); | |
272 | signal(SIGINT,done); | |
273 | signal(SIGPIPE,done); | |
274 | signal(SIGTERM,done); | |
275 | tempfile = mktemp("/tmp/dXXXXX"); | |
276 | f = creat(tempfile,0600); | |
277 | if (f < 0) { | |
804c14e9 | 278 | fprintf(stderr, "diff: "); |
43454f97 BJ |
279 | perror(tempfile); |
280 | done(); | |
281 | } | |
282 | while ((i = read(0,buf,BUFSIZ)) > 0) | |
283 | if (write(f,buf,i) != i) { | |
284 | fprintf(stderr, "diff: "); | |
285 | perror(tempfile); | |
286 | done(); | |
287 | } | |
288 | close(f); | |
289 | return (tempfile); | |
290 | } | |
291 | ||
292 | char * | |
293 | splice(dir, file) | |
294 | char *dir, *file; | |
295 | { | |
296 | char *tail; | |
297 | char buf[BUFSIZ]; | |
298 | ||
299 | if (!strcmp(file, "-")) { | |
300 | fprintf(stderr, "diff: can't specify - with other arg directory\n"); | |
301 | done(); | |
302 | } | |
303 | tail = rindex(file, '/'); | |
304 | if (tail == 0) | |
305 | tail = file; | |
306 | else | |
307 | tail++; | |
308 | sprintf(buf, "%s/%s", dir, tail); | |
309 | return (savestr(buf)); | |
310 | } | |
311 | ||
87f863b7 KM |
312 | prepare(i, fd) |
313 | int i; | |
314 | FILE *fd; | |
43454f97 BJ |
315 | { |
316 | register struct line *p; | |
317 | register j,h; | |
87f863b7 | 318 | |
87f863b7 | 319 | fseek(fd, (long)0, 0); |
43454f97 | 320 | p = (struct line *)talloc(3*sizeof(line)); |
87f863b7 | 321 | for(j=0; h=readhash(fd);) { |
43454f97 BJ |
322 | p = (struct line *)ralloc((char *)p,(++j+3)*sizeof(line)); |
323 | p[j].value = h; | |
324 | } | |
325 | len[i] = j; | |
326 | file[i] = p; | |
43454f97 BJ |
327 | } |
328 | ||
329 | prune() | |
330 | { | |
331 | register i,j; | |
332 | for(pref=0;pref<len[0]&&pref<len[1]&& | |
333 | file[0][pref+1].value==file[1][pref+1].value; | |
334 | pref++ ) ; | |
335 | for(suff=0;suff<len[0]-pref&&suff<len[1]-pref&& | |
336 | file[0][len[0]-suff].value==file[1][len[1]-suff].value; | |
337 | suff++) ; | |
338 | for(j=0;j<2;j++) { | |
339 | sfile[j] = file[j]+pref; | |
340 | slen[j] = len[j]-pref-suff; | |
341 | for(i=0;i<=slen[j];i++) | |
342 | sfile[j][i].serial = i; | |
343 | } | |
344 | } | |
345 | ||
346 | equiv(a,n,b,m,c) | |
347 | struct line *a, *b; | |
348 | int *c; | |
349 | { | |
350 | register int i, j; | |
351 | i = j = 1; | |
352 | while(i<=n && j<=m) { | |
353 | if(a[i].value <b[j].value) | |
354 | a[i++].value = 0; | |
355 | else if(a[i].value == b[j].value) | |
356 | a[i++].value = j; | |
357 | else | |
358 | j++; | |
359 | } | |
360 | while(i <= n) | |
361 | a[i++].value = 0; | |
362 | b[m+1].value = 0; | |
363 | j = 0; | |
364 | while(++j <= m) { | |
365 | c[j] = -b[j].serial; | |
366 | while(b[j+1].value == b[j].value) { | |
367 | j++; | |
368 | c[j] = b[j].serial; | |
369 | } | |
370 | } | |
371 | c[j] = -1; | |
372 | } | |
373 | ||
374 | stone(a,n,b,c) | |
375 | int *a; | |
376 | int *b; | |
745638c1 | 377 | register int *c; |
43454f97 BJ |
378 | { |
379 | register int i, k,y; | |
380 | int j, l; | |
381 | int oldc, tc; | |
382 | int oldl; | |
383 | k = 0; | |
384 | c[0] = newcand(0,0,0); | |
385 | for(i=1; i<=n; i++) { | |
386 | j = a[i]; | |
387 | if(j==0) | |
388 | continue; | |
389 | y = -b[j]; | |
390 | oldl = 0; | |
391 | oldc = c[0]; | |
392 | do { | |
393 | if(y <= clist[oldc].y) | |
394 | continue; | |
395 | l = search(c, k, y); | |
396 | if(l!=oldl+1) | |
397 | oldc = c[l-1]; | |
398 | if(l<=k) { | |
399 | if(clist[c[l]].y <= y) | |
400 | continue; | |
401 | tc = c[l]; | |
402 | c[l] = newcand(i,y,oldc); | |
403 | oldc = tc; | |
404 | oldl = l; | |
405 | } else { | |
406 | c[l] = newcand(i,y,oldc); | |
407 | k++; | |
408 | break; | |
409 | } | |
410 | } while((y=b[++j]) > 0); | |
411 | } | |
412 | return(k); | |
413 | } | |
414 | ||
415 | newcand(x,y,pred) | |
416 | { | |
417 | register struct cand *q; | |
418 | clist = (struct cand *)ralloc((char *)clist,++clen*sizeof(cand)); | |
419 | q = clist + clen -1; | |
420 | q->x = x; | |
421 | q->y = y; | |
422 | q->pred = pred; | |
423 | return(clen-1); | |
424 | } | |
425 | ||
426 | search(c, k, y) | |
427 | int *c; | |
428 | { | |
429 | register int i, j, l; | |
430 | int t; | |
431 | if(clist[c[k]].y<y) /*quick look for typical case*/ | |
432 | return(k+1); | |
433 | i = 0; | |
434 | j = k+1; | |
745638c1 RC |
435 | while (1) { |
436 | l = i + j; | |
437 | if ((l >>= 1) <= i) | |
438 | break; | |
43454f97 BJ |
439 | t = clist[c[l]].y; |
440 | if(t > y) | |
441 | j = l; | |
442 | else if(t < y) | |
443 | i = l; | |
444 | else | |
445 | return(l); | |
446 | } | |
447 | return(l+1); | |
448 | } | |
449 | ||
450 | unravel(p) | |
451 | { | |
452 | register int i; | |
453 | register struct cand *q; | |
454 | for(i=0; i<=len[0]; i++) | |
455 | J[i] = i<=pref ? i: | |
456 | i>len[0]-suff ? i+len[1]-len[0]: | |
457 | 0; | |
458 | for(q=clist+p;q->y!=0;q=clist+q->pred) | |
459 | J[q->x+pref] = q->y+pref; | |
460 | } | |
461 | ||
462 | /* check does double duty: | |
463 | 1. ferret out any fortuitous correspondences due | |
464 | to confounding by hashing (which result in "jackpot") | |
465 | 2. collect random access indexes to the two files */ | |
466 | ||
467 | check() | |
468 | { | |
469 | register int i, j; | |
470 | int jackpot; | |
471 | long ctold, ctnew; | |
5427f51a | 472 | register int c,d; |
46235152 KM |
473 | |
474 | if ((input[0] = fopen(file1,"r")) == NULL) { | |
475 | perror(file1); | |
476 | done(); | |
477 | } | |
478 | if ((input[1] = fopen(file2,"r")) == NULL) { | |
479 | perror(file2); | |
480 | done(); | |
481 | } | |
43454f97 BJ |
482 | j = 1; |
483 | ixold[0] = ixnew[0] = 0; | |
484 | jackpot = 0; | |
485 | ctold = ctnew = 0; | |
486 | for(i=1;i<=len[0];i++) { | |
487 | if(J[i]==0) { | |
488 | ixold[i] = ctold += skipline(0); | |
489 | continue; | |
490 | } | |
491 | while(j<J[i]) { | |
492 | ixnew[j] = ctnew += skipline(1); | |
493 | j++; | |
494 | } | |
c470b1f1 VJ |
495 | if(bflag || wflag || iflag) { |
496 | for(;;) { | |
497 | c = getc(input[0]); | |
498 | d = getc(input[1]); | |
499 | ctold++; | |
500 | ctnew++; | |
501 | if(bflag && isspace(c) && isspace(d)) { | |
502 | do { | |
503 | if(c=='\n') | |
504 | break; | |
505 | ctold++; | |
506 | } while(isspace(c=getc(input[0]))); | |
507 | do { | |
508 | if(d=='\n') | |
509 | break; | |
510 | ctnew++; | |
511 | } while(isspace(d=getc(input[1]))); | |
512 | } else if ( wflag ) { | |
513 | while( isspace(c) && c!='\n' ) { | |
514 | c=getc(input[0]); | |
515 | ctold++; | |
516 | } | |
517 | while( isspace(d) && d!='\n' ) { | |
518 | d=getc(input[1]); | |
519 | ctnew++; | |
520 | } | |
521 | } | |
522 | if(chrtran[c] != chrtran[d]) { | |
523 | jackpot++; | |
524 | J[i] = 0; | |
525 | if(c!='\n') | |
526 | ctold += skipline(0); | |
527 | if(d!='\n') | |
528 | ctnew += skipline(1); | |
529 | break; | |
530 | } | |
531 | if(c=='\n') | |
532 | break; | |
43454f97 | 533 | } |
c470b1f1 VJ |
534 | } else { |
535 | for(;;) { | |
536 | ctold++; | |
537 | ctnew++; | |
538 | if((c=getc(input[0])) != (d=getc(input[1]))) { | |
539 | /* jackpot++; */ | |
540 | J[i] = 0; | |
541 | if(c!='\n') | |
542 | ctold += skipline(0); | |
543 | if(d!='\n') | |
544 | ctnew += skipline(1); | |
545 | break; | |
546 | } | |
547 | if(c=='\n') | |
548 | break; | |
43454f97 | 549 | } |
43454f97 BJ |
550 | } |
551 | ixold[i] = ctold; | |
552 | ixnew[j] = ctnew; | |
553 | j++; | |
554 | } | |
555 | for(;j<=len[1];j++) { | |
556 | ixnew[j] = ctnew += skipline(1); | |
557 | } | |
558 | fclose(input[0]); | |
559 | fclose(input[1]); | |
560 | /* | |
561 | if(jackpot) | |
562 | fprintf(stderr, "jackpot\n"); | |
563 | */ | |
564 | } | |
565 | ||
566 | sort(a,n) /*shellsort CACM #201*/ | |
567 | struct line *a; | |
568 | { | |
569 | struct line w; | |
570 | register int j,m; | |
571 | struct line *ai; | |
572 | register struct line *aim; | |
573 | int k; | |
574 | for(j=1;j<=n;j*= 2) | |
575 | m = 2*j - 1; | |
576 | for(m/=2;m!=0;m/=2) { | |
577 | k = n-m; | |
578 | for(j=1;j<=k;j++) { | |
579 | for(ai = &a[j]; ai > a; ai -= m) { | |
580 | aim = &ai[m]; | |
581 | if(aim < ai) | |
582 | break; /*wraparound*/ | |
583 | if(aim->value > ai[0].value || | |
584 | aim->value == ai[0].value && | |
585 | aim->serial > ai[0].serial) | |
586 | break; | |
587 | w.value = ai[0].value; | |
588 | ai[0].value = aim->value; | |
589 | aim->value = w.value; | |
590 | w.serial = ai[0].serial; | |
591 | ai[0].serial = aim->serial; | |
592 | aim->serial = w.serial; | |
593 | } | |
594 | } | |
595 | } | |
596 | } | |
597 | ||
598 | unsort(f, l, b) | |
599 | struct line *f; | |
600 | int *b; | |
601 | { | |
602 | register int *a; | |
603 | register int i; | |
604 | a = (int *)talloc((l+1)*sizeof(int)); | |
605 | for(i=1;i<=l;i++) | |
606 | a[f[i].serial] = f[i].value; | |
607 | for(i=1;i<=l;i++) | |
608 | b[i] = a[i]; | |
609 | free((char *)a); | |
610 | } | |
611 | ||
612 | skipline(f) | |
613 | { | |
5427f51a | 614 | register i, c; |
46235152 KM |
615 | |
616 | for(i=1;(c=getc(input[f]))!='\n';i++) | |
617 | if (c < 0) | |
618 | return(i); | |
43454f97 BJ |
619 | return(i); |
620 | } | |
621 | ||
622 | output() | |
623 | { | |
624 | int m; | |
625 | register int i0, i1, j1; | |
626 | int j0; | |
627 | input[0] = fopen(file1,"r"); | |
628 | input[1] = fopen(file2,"r"); | |
629 | m = len[0]; | |
630 | J[0] = 0; | |
631 | J[m+1] = len[1]+1; | |
632 | if(opt!=D_EDIT) for(i0=1;i0<=m;i0=i1+1) { | |
633 | while(i0<=m&&J[i0]==J[i0-1]+1) i0++; | |
634 | j0 = J[i0-1]+1; | |
635 | i1 = i0-1; | |
636 | while(i1<m&&J[i1+1]==0) i1++; | |
637 | j1 = J[i1+1]-1; | |
638 | J[i1] = j1; | |
639 | change(i0,i1,j0,j1); | |
640 | } else for(i0=m;i0>=1;i0=i1-1) { | |
641 | while(i0>=1&&J[i0]==J[i0+1]-1&&J[i0]!=0) i0--; | |
642 | j0 = J[i0+1]-1; | |
643 | i1 = i0+1; | |
644 | while(i1>1&&J[i1-1]==0) i1--; | |
645 | j1 = J[i1-1]+1; | |
646 | J[i1] = j1; | |
647 | change(i1,i0,j1,j0); | |
648 | } | |
649 | if(m==0) | |
650 | change(1,0,1,len[1]); | |
651 | if (opt==D_IFDEF) { | |
652 | for (;;) { | |
653 | #define c i0 | |
654 | c = getc(input[0]); | |
655 | if (c < 0) | |
656 | return; | |
657 | putchar(c); | |
658 | } | |
659 | #undef c | |
660 | } | |
c470b1f1 VJ |
661 | if (opt == D_CONTEXT) |
662 | dump_context_vec(); | |
43454f97 BJ |
663 | } |
664 | ||
c470b1f1 VJ |
665 | /* |
666 | * The following struct is used to record change information when | |
667 | * doing a "context" diff. (see routine "change" to understand the | |
668 | * highly mneumonic field names) | |
669 | */ | |
670 | struct context_vec { | |
671 | int a; /* start line in old file */ | |
672 | int b; /* end line in old file */ | |
673 | int c; /* start line in new file */ | |
674 | int d; /* end line in new file */ | |
675 | }; | |
676 | ||
677 | struct context_vec *context_vec_start, | |
678 | *context_vec_end, | |
679 | *context_vec_ptr; | |
680 | ||
681 | #define MAX_CONTEXT 128 | |
682 | ||
43454f97 BJ |
683 | /* indicate that there is a difference between lines a and b of the from file |
684 | to get to lines c to d of the to file. | |
685 | If a is greater then b then there are no lines in the from file involved | |
686 | and this means that there were lines appended (beginning at b). | |
687 | If c is greater than d then there are lines missing from the to file. | |
688 | */ | |
689 | change(a,b,c,d) | |
690 | { | |
5427f51a | 691 | int ch; |
43454f97 BJ |
692 | int lowa,upb,lowc,upd; |
693 | struct stat stbuf; | |
694 | ||
695 | if (opt != D_IFDEF && a>b && c>d) | |
696 | return; | |
697 | if (anychange == 0) { | |
698 | anychange = 1; | |
699 | if(opt == D_CONTEXT) { | |
700 | printf("*** %s ", file1); | |
701 | stat(file1, &stbuf); | |
702 | printf("%s--- %s ", | |
703 | ctime(&stbuf.st_mtime), file2); | |
704 | stat(file2, &stbuf); | |
705 | printf("%s", ctime(&stbuf.st_mtime)); | |
c470b1f1 VJ |
706 | |
707 | context_vec_start = (struct context_vec *) | |
708 | malloc(MAX_CONTEXT * | |
709 | sizeof(struct context_vec)); | |
710 | context_vec_end = context_vec_start + MAX_CONTEXT; | |
711 | context_vec_ptr = context_vec_start - 1; | |
43454f97 BJ |
712 | } |
713 | } | |
714 | if (a <= b && c <= d) | |
715 | ch = 'c'; | |
716 | else | |
717 | ch = (a <= b) ? 'd' : 'a'; | |
718 | if(opt == D_CONTEXT) { | |
c470b1f1 VJ |
719 | /* |
720 | * if this new change is within 'context' lines of | |
721 | * the previous change, just add it to the change | |
722 | * record. If the record is full or if this | |
723 | * change is more than 'context' lines from the previous | |
724 | * change, dump the record, reset it & add the new change. | |
725 | */ | |
726 | if ( context_vec_ptr >= context_vec_end || | |
727 | ( context_vec_ptr >= context_vec_start && | |
728 | a > (context_vec_ptr->b + 2*context) && | |
729 | c > (context_vec_ptr->d + 2*context) ) ) | |
730 | dump_context_vec(); | |
731 | ||
732 | context_vec_ptr++; | |
733 | context_vec_ptr->a = a; | |
734 | context_vec_ptr->b = b; | |
735 | context_vec_ptr->c = c; | |
736 | context_vec_ptr->d = d; | |
43454f97 BJ |
737 | return; |
738 | } | |
739 | switch (opt) { | |
740 | ||
741 | case D_NORMAL: | |
742 | case D_EDIT: | |
743 | range(a,b,","); | |
744 | putchar(a>b?'a':c>d?'d':'c'); | |
745 | if(opt==D_NORMAL) | |
746 | range(c,d,","); | |
747 | putchar('\n'); | |
748 | break; | |
749 | case D_REVERSE: | |
750 | putchar(a>b?'a':c>d?'d':'c'); | |
751 | range(a,b," "); | |
752 | putchar('\n'); | |
753 | break; | |
a3e88724 VJ |
754 | case D_NREVERSE: |
755 | if (a>b) | |
756 | printf("a%d %d\n",b,d-c+1); | |
757 | else { | |
758 | printf("d%d %d\n",a,b-a+1); | |
759 | if (!(c>d)) | |
760 | /* add changed lines */ | |
761 | printf("a%d %d\n",b, d-c+1); | |
762 | } | |
763 | break; | |
43454f97 BJ |
764 | } |
765 | if(opt == D_NORMAL || opt == D_IFDEF) { | |
766 | fetch(ixold,a,b,input[0],"< ", 1); | |
767 | if(a<=b&&c<=d && opt == D_NORMAL) | |
768 | prints("---\n"); | |
769 | } | |
770 | fetch(ixnew,c,d,input[1],opt==D_NORMAL?"> ":"", 0); | |
771 | if ((opt ==D_EDIT || opt == D_REVERSE) && c<=d) | |
772 | prints(".\n"); | |
773 | if (inifdef) { | |
774 | fprintf(stdout, "#endif %s\n", endifname); | |
775 | inifdef = 0; | |
776 | } | |
777 | } | |
778 | ||
779 | range(a,b,separator) | |
780 | char *separator; | |
781 | { | |
782 | printf("%d", a>b?b:a); | |
783 | if(a<b) { | |
784 | printf("%s%d", separator, b); | |
785 | } | |
786 | } | |
787 | ||
788 | fetch(f,a,b,lb,s,oldfile) | |
789 | long *f; | |
790 | FILE *lb; | |
791 | char *s; | |
792 | { | |
793 | register int i, j; | |
c470b1f1 VJ |
794 | register int c; |
795 | register int col; | |
43454f97 BJ |
796 | register int nc; |
797 | int oneflag = (*ifdef1!='\0') != (*ifdef2!='\0'); | |
798 | ||
799 | /* | |
800 | * When doing #ifdef's, copy down to current line | |
801 | * if this is the first file, so that stuff makes it to output. | |
802 | */ | |
803 | if (opt == D_IFDEF && oldfile){ | |
804 | long curpos = ftell(lb); | |
805 | /* print through if append (a>b), else to (nb: 0 vs 1 orig) */ | |
806 | nc = f[a>b? b : a-1 ] - curpos; | |
807 | for (i = 0; i < nc; i++) | |
808 | putchar(getc(lb)); | |
809 | } | |
810 | if (a > b) | |
811 | return; | |
812 | if (opt == D_IFDEF) { | |
813 | if (inifdef) | |
814 | fprintf(stdout, "#else %s%s\n", oneflag && oldfile==1 ? "!" : "", ifdef2); | |
815 | else { | |
816 | if (oneflag) { | |
817 | /* There was only one ifdef given */ | |
818 | endifname = ifdef2; | |
819 | if (oldfile) | |
820 | fprintf(stdout, "#ifndef %s\n", endifname); | |
821 | else | |
822 | fprintf(stdout, "#ifdef %s\n", endifname); | |
823 | } | |
824 | else { | |
825 | endifname = oldfile ? ifdef1 : ifdef2; | |
826 | fprintf(stdout, "#ifdef %s\n", endifname); | |
827 | } | |
828 | } | |
829 | inifdef = 1+oldfile; | |
830 | } | |
c470b1f1 | 831 | |
43454f97 BJ |
832 | for(i=a;i<=b;i++) { |
833 | fseek(lb,f[i-1],0); | |
834 | nc = f[i]-f[i-1]; | |
835 | if (opt != D_IFDEF) | |
836 | prints(s); | |
c470b1f1 VJ |
837 | col = 0; |
838 | for(j=0;j<nc;j++) { | |
839 | c = getc(lb); | |
840 | if (c == '\t' && tflag) | |
841 | do | |
842 | putchar(' '); | |
843 | while (++col & 7); | |
844 | else { | |
845 | putchar(c); | |
846 | col++; | |
847 | } | |
848 | } | |
43454f97 | 849 | } |
c470b1f1 | 850 | |
43454f97 BJ |
851 | if (inifdef && !wantelses) { |
852 | fprintf(stdout, "#endif %s\n", endifname); | |
853 | inifdef = 0; | |
854 | } | |
855 | } | |
856 | ||
5427f51a | 857 | #define POW2 /* define only if HALFLONG is 2**n */ |
43454f97 BJ |
858 | #define HALFLONG 16 |
859 | #define low(x) (x&((1L<<HALFLONG)-1)) | |
860 | #define high(x) (x>>HALFLONG) | |
861 | ||
862 | /* | |
863 | * hashing has the effect of | |
864 | * arranging line in 7-bit bytes and then | |
865 | * summing 1-s complement in 16-bit hunks | |
866 | */ | |
867 | readhash(f) | |
745638c1 | 868 | register FILE *f; |
43454f97 | 869 | { |
c470b1f1 | 870 | register long sum; |
43454f97 | 871 | register unsigned shift; |
43454f97 | 872 | register t; |
c470b1f1 VJ |
873 | register space; |
874 | ||
43454f97 BJ |
875 | sum = 1; |
876 | space = 0; | |
c470b1f1 VJ |
877 | if(!bflag && !wflag) { |
878 | if(iflag) | |
879 | for(shift=0;(t=getc(f))!='\n';shift+=7) { | |
880 | if(t==-1) | |
881 | return(0); | |
5427f51a JL |
882 | sum += (long)chrtran[t] << (shift |
883 | #ifdef POW2 | |
884 | &= HALFLONG - 1); | |
885 | #else | |
886 | %= HALFLONG); | |
887 | #endif | |
c470b1f1 VJ |
888 | } |
889 | else | |
890 | for(shift=0;(t=getc(f))!='\n';shift+=7) { | |
891 | if(t==-1) | |
892 | return(0); | |
5427f51a JL |
893 | sum += (long)t << (shift |
894 | #ifdef POW2 | |
895 | &= HALFLONG - 1); | |
896 | #else | |
897 | %= HALFLONG); | |
898 | #endif | |
c470b1f1 VJ |
899 | } |
900 | } else { | |
901 | for(shift=0;;) { | |
902 | switch(t=getc(f)) { | |
903 | case -1: | |
904 | return(0); | |
905 | case '\t': | |
906 | case ' ': | |
907 | space++; | |
908 | continue; | |
909 | default: | |
910 | if(space && !wflag) { | |
911 | shift += 7; | |
912 | space = 0; | |
913 | } | |
5427f51a JL |
914 | sum += (long)chrtran[t] << (shift |
915 | #ifdef POW2 | |
916 | &= HALFLONG - 1); | |
917 | #else | |
918 | %= HALFLONG); | |
919 | #endif | |
43454f97 | 920 | shift += 7; |
c470b1f1 VJ |
921 | continue; |
922 | case '\n': | |
923 | break; | |
43454f97 | 924 | } |
43454f97 BJ |
925 | break; |
926 | } | |
43454f97 BJ |
927 | } |
928 | sum = low(sum) + high(sum); | |
929 | return((short)low(sum) + (short)high(sum)); | |
930 | } | |
46235152 KM |
931 | |
932 | #include <a.out.h> | |
933 | ||
934 | asciifile(f) | |
935 | FILE *f; | |
936 | { | |
937 | char buf[BUFSIZ]; | |
938 | register int cnt; | |
939 | register char *cp; | |
940 | ||
941 | fseek(f, (long)0, 0); | |
6f7a12ee | 942 | cnt = fread(buf, 1, BUFSIZ, f); |
46235152 KM |
943 | if (cnt >= sizeof (struct exec)) { |
944 | struct exec hdr; | |
945 | hdr = *(struct exec *)buf; | |
946 | if (!N_BADMAG(hdr)) | |
947 | return (0); | |
948 | } | |
949 | cp = buf; | |
950 | while (--cnt >= 0) | |
951 | if (*cp++ & 0200) | |
952 | return (0); | |
953 | return (1); | |
954 | } | |
c470b1f1 VJ |
955 | |
956 | ||
957 | /* dump accumulated "context" diff changes */ | |
958 | dump_context_vec() | |
959 | { | |
960 | register int a, b, c, d; | |
961 | register char ch; | |
962 | register struct context_vec *cvp = context_vec_start; | |
963 | register int lowa, upb, lowc, upd; | |
bf94f6de | 964 | register int do_output; |
c470b1f1 VJ |
965 | |
966 | if ( cvp > context_vec_ptr ) | |
967 | return; | |
968 | ||
969 | lowa = max(1, cvp->a - context); | |
970 | upb = min(len[0], context_vec_ptr->b + context); | |
971 | lowc = max(1, cvp->c - context); | |
972 | upd = min(len[1], context_vec_ptr->d + context); | |
973 | ||
974 | printf("***************\n*** "); | |
975 | range(lowa,upb,","); | |
bf94f6de | 976 | printf(" ****\n"); |
c470b1f1 VJ |
977 | |
978 | /* | |
bf94f6de KM |
979 | * output changes to the "old" file. The first loop suppresses |
980 | * output if there were no changes to the "old" file (we'll see | |
981 | * the "old" lines as context in the "new" list). | |
c470b1f1 | 982 | */ |
bf94f6de KM |
983 | do_output = 0; |
984 | for ( ; cvp <= context_vec_ptr; cvp++) | |
c470b1f1 VJ |
985 | if (cvp->a <= cvp->b) { |
986 | cvp = context_vec_start; | |
bf94f6de | 987 | do_output++; |
c470b1f1 VJ |
988 | break; |
989 | } | |
bf94f6de KM |
990 | |
991 | if ( do_output ) { | |
992 | while (cvp <= context_vec_ptr) { | |
993 | a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d; | |
994 | ||
995 | if (a <= b && c <= d) | |
996 | ch = 'c'; | |
997 | else | |
998 | ch = (a <= b) ? 'd' : 'a'; | |
999 | ||
1000 | if (ch == 'a') | |
1001 | fetch(ixold,lowa,b,input[0]," "); | |
1002 | else { | |
1003 | fetch(ixold,lowa,a-1,input[0]," "); | |
1004 | fetch(ixold,a,b,input[0],ch == 'c' ? "! " : "- "); | |
1005 | } | |
1006 | lowa = b + 1; | |
1007 | cvp++; | |
c470b1f1 | 1008 | } |
bf94f6de | 1009 | fetch(ixold, b+1, upb, input[0], " "); |
c470b1f1 | 1010 | } |
c470b1f1 VJ |
1011 | |
1012 | /* output changes to the "new" file */ | |
bf94f6de KM |
1013 | printf("--- "); |
1014 | range(lowc,upd,","); | |
1015 | printf(" ----\n"); | |
c470b1f1 | 1016 | |
bf94f6de KM |
1017 | do_output = 0; |
1018 | for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++) | |
c470b1f1 VJ |
1019 | if (cvp->c <= cvp->d) { |
1020 | cvp = context_vec_start; | |
bf94f6de | 1021 | do_output++; |
c470b1f1 VJ |
1022 | break; |
1023 | } | |
bf94f6de KM |
1024 | |
1025 | if (do_output) { | |
1026 | while (cvp <= context_vec_ptr) { | |
1027 | a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d; | |
1028 | ||
1029 | if (a <= b && c <= d) | |
1030 | ch = 'c'; | |
1031 | else | |
1032 | ch = (a <= b) ? 'd' : 'a'; | |
1033 | ||
1034 | if (ch == 'd') | |
1035 | fetch(ixnew,lowc,d,input[1]," "); | |
1036 | else { | |
1037 | fetch(ixnew,lowc,c-1,input[1]," "); | |
1038 | fetch(ixnew,c,d,input[1],ch == 'c' ? "! " : "+ "); | |
1039 | } | |
1040 | lowc = d + 1; | |
1041 | cvp++; | |
c470b1f1 | 1042 | } |
bf94f6de | 1043 | fetch(ixnew, d+1, upd, input[1], " "); |
c470b1f1 | 1044 | } |
c470b1f1 VJ |
1045 | |
1046 | context_vec_ptr = context_vec_start - 1; | |
1047 | } |