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