| 1 | static char sccsid[] = "@(#)diffreg.c 4.9 %G%"; |
| 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] */ |
| 97 | |
| 98 | diffreg() |
| 99 | { |
| 100 | register int i, j; |
| 101 | FILE *f1, *f2; |
| 102 | char buf1[BUFSIZ], buf2[BUFSIZ]; |
| 103 | |
| 104 | if (hflag) { |
| 105 | diffargv[0] = "diffh"; |
| 106 | execv(diffh, diffargv); |
| 107 | fprintf(stderr, "diff: "); |
| 108 | perror(diffh); |
| 109 | done(); |
| 110 | } |
| 111 | if ((stb1.st_mode & S_IFMT) == S_IFDIR) |
| 112 | file1 = splice(file1, file2); |
| 113 | else if ((stb2.st_mode & S_IFMT) == S_IFDIR) |
| 114 | file2 = splice(file2, file1); |
| 115 | else if (!strcmp(file1, "-")) { |
| 116 | if (!strcmp(file2, "-")) { |
| 117 | fprintf(stderr, "diff: can't specify - -\n"); |
| 118 | done(); |
| 119 | } |
| 120 | file1 = copytemp(); |
| 121 | } else if (!strcmp(file2, "-")) |
| 122 | file2 = copytemp(); |
| 123 | if ((f1 = fopen(file1, "r")) == NULL) { |
| 124 | fprintf(stderr, "diff: "); |
| 125 | perror(file1); |
| 126 | done(); |
| 127 | } |
| 128 | if ((f2 = fopen(file2, "r")) == NULL) { |
| 129 | fprintf(stderr, "diff: "); |
| 130 | perror(file2); |
| 131 | fclose(f1); |
| 132 | done(); |
| 133 | } |
| 134 | if (stb1.st_size != stb2.st_size) |
| 135 | goto notsame; |
| 136 | for (;;) { |
| 137 | i = fread(buf1, 1, BUFSIZ, f1); |
| 138 | j = fread(buf2, 1, BUFSIZ, f2); |
| 139 | if (i < 0 || j < 0 || i != j) |
| 140 | goto notsame; |
| 141 | if (i == 0 && j == 0) { |
| 142 | fclose(f1); |
| 143 | fclose(f2); |
| 144 | status = 0; /* files don't differ */ |
| 145 | goto same; |
| 146 | } |
| 147 | for (j = 0; j < i; j++) |
| 148 | if (buf1[j] != buf2[j]) |
| 149 | goto notsame; |
| 150 | } |
| 151 | notsame: |
| 152 | /* |
| 153 | * Files certainly differ at this point; set status accordingly |
| 154 | */ |
| 155 | status = 1; |
| 156 | if (!asciifile(f1) || !asciifile(f2)) { |
| 157 | printf("Binary files %s and %s differ\n", file1, file2); |
| 158 | fclose(f1); |
| 159 | fclose(f2); |
| 160 | done(); |
| 161 | } |
| 162 | prepare(0, f1); |
| 163 | prepare(1, f2); |
| 164 | fclose(f1); |
| 165 | fclose(f2); |
| 166 | prune(); |
| 167 | sort(sfile[0],slen[0]); |
| 168 | sort(sfile[1],slen[1]); |
| 169 | |
| 170 | member = (int *)file[1]; |
| 171 | equiv(sfile[0], slen[0], sfile[1], slen[1], member); |
| 172 | member = (int *)ralloc((char *)member,(slen[1]+2)*sizeof(int)); |
| 173 | |
| 174 | class = (int *)file[0]; |
| 175 | unsort(sfile[0], slen[0], class); |
| 176 | class = (int *)ralloc((char *)class,(slen[0]+2)*sizeof(int)); |
| 177 | |
| 178 | klist = (int *)talloc((slen[0]+2)*sizeof(int)); |
| 179 | clist = (struct cand *)talloc(sizeof(cand)); |
| 180 | i = stone(class, slen[0], member, klist); |
| 181 | free((char *)member); |
| 182 | free((char *)class); |
| 183 | |
| 184 | J = (int *)talloc((len[0]+2)*sizeof(int)); |
| 185 | unravel(klist[i]); |
| 186 | free((char *)clist); |
| 187 | free((char *)klist); |
| 188 | |
| 189 | ixold = (long *)talloc((len[0]+2)*sizeof(long)); |
| 190 | ixnew = (long *)talloc((len[1]+2)*sizeof(long)); |
| 191 | check(); |
| 192 | output(); |
| 193 | status = anychange; |
| 194 | same: |
| 195 | if (opt == D_CONTEXT && anychange == 0) |
| 196 | printf("No differences encountered\n"); |
| 197 | done(); |
| 198 | } |
| 199 | |
| 200 | char * |
| 201 | copytemp() |
| 202 | { |
| 203 | char buf[BUFSIZ]; |
| 204 | register int i, f; |
| 205 | |
| 206 | signal(SIGHUP,done); |
| 207 | signal(SIGINT,done); |
| 208 | signal(SIGPIPE,done); |
| 209 | signal(SIGTERM,done); |
| 210 | tempfile = mktemp("/tmp/dXXXXX"); |
| 211 | f = creat(tempfile,0600); |
| 212 | if (f < 0) { |
| 213 | fprintf(stderr, "diff: "); |
| 214 | perror(tempfile); |
| 215 | done(); |
| 216 | } |
| 217 | while ((i = read(0,buf,BUFSIZ)) > 0) |
| 218 | if (write(f,buf,i) != i) { |
| 219 | fprintf(stderr, "diff: "); |
| 220 | perror(tempfile); |
| 221 | done(); |
| 222 | } |
| 223 | close(f); |
| 224 | return (tempfile); |
| 225 | } |
| 226 | |
| 227 | char * |
| 228 | splice(dir, file) |
| 229 | char *dir, *file; |
| 230 | { |
| 231 | char *tail; |
| 232 | char buf[BUFSIZ]; |
| 233 | |
| 234 | if (!strcmp(file, "-")) { |
| 235 | fprintf(stderr, "diff: can't specify - with other arg directory\n"); |
| 236 | done(); |
| 237 | } |
| 238 | tail = rindex(file, '/'); |
| 239 | if (tail == 0) |
| 240 | tail = file; |
| 241 | else |
| 242 | tail++; |
| 243 | sprintf(buf, "%s/%s", dir, tail); |
| 244 | return (savestr(buf)); |
| 245 | } |
| 246 | |
| 247 | prepare(i, fd) |
| 248 | int i; |
| 249 | FILE *fd; |
| 250 | { |
| 251 | register struct line *p; |
| 252 | register j,h; |
| 253 | |
| 254 | fseek(fd, (long)0, 0); |
| 255 | p = (struct line *)talloc(3*sizeof(line)); |
| 256 | for(j=0; h=readhash(fd);) { |
| 257 | p = (struct line *)ralloc((char *)p,(++j+3)*sizeof(line)); |
| 258 | p[j].value = h; |
| 259 | } |
| 260 | len[i] = j; |
| 261 | file[i] = p; |
| 262 | } |
| 263 | |
| 264 | prune() |
| 265 | { |
| 266 | register i,j; |
| 267 | for(pref=0;pref<len[0]&&pref<len[1]&& |
| 268 | file[0][pref+1].value==file[1][pref+1].value; |
| 269 | pref++ ) ; |
| 270 | for(suff=0;suff<len[0]-pref&&suff<len[1]-pref&& |
| 271 | file[0][len[0]-suff].value==file[1][len[1]-suff].value; |
| 272 | suff++) ; |
| 273 | for(j=0;j<2;j++) { |
| 274 | sfile[j] = file[j]+pref; |
| 275 | slen[j] = len[j]-pref-suff; |
| 276 | for(i=0;i<=slen[j];i++) |
| 277 | sfile[j][i].serial = i; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | equiv(a,n,b,m,c) |
| 282 | struct line *a, *b; |
| 283 | int *c; |
| 284 | { |
| 285 | register int i, j; |
| 286 | i = j = 1; |
| 287 | while(i<=n && j<=m) { |
| 288 | if(a[i].value <b[j].value) |
| 289 | a[i++].value = 0; |
| 290 | else if(a[i].value == b[j].value) |
| 291 | a[i++].value = j; |
| 292 | else |
| 293 | j++; |
| 294 | } |
| 295 | while(i <= n) |
| 296 | a[i++].value = 0; |
| 297 | b[m+1].value = 0; |
| 298 | j = 0; |
| 299 | while(++j <= m) { |
| 300 | c[j] = -b[j].serial; |
| 301 | while(b[j+1].value == b[j].value) { |
| 302 | j++; |
| 303 | c[j] = b[j].serial; |
| 304 | } |
| 305 | } |
| 306 | c[j] = -1; |
| 307 | } |
| 308 | |
| 309 | stone(a,n,b,c) |
| 310 | int *a; |
| 311 | int *b; |
| 312 | register int *c; |
| 313 | { |
| 314 | register int i, k,y; |
| 315 | int j, l; |
| 316 | int oldc, tc; |
| 317 | int oldl; |
| 318 | k = 0; |
| 319 | c[0] = newcand(0,0,0); |
| 320 | for(i=1; i<=n; i++) { |
| 321 | j = a[i]; |
| 322 | if(j==0) |
| 323 | continue; |
| 324 | y = -b[j]; |
| 325 | oldl = 0; |
| 326 | oldc = c[0]; |
| 327 | do { |
| 328 | if(y <= clist[oldc].y) |
| 329 | continue; |
| 330 | l = search(c, k, y); |
| 331 | if(l!=oldl+1) |
| 332 | oldc = c[l-1]; |
| 333 | if(l<=k) { |
| 334 | if(clist[c[l]].y <= y) |
| 335 | continue; |
| 336 | tc = c[l]; |
| 337 | c[l] = newcand(i,y,oldc); |
| 338 | oldc = tc; |
| 339 | oldl = l; |
| 340 | } else { |
| 341 | c[l] = newcand(i,y,oldc); |
| 342 | k++; |
| 343 | break; |
| 344 | } |
| 345 | } while((y=b[++j]) > 0); |
| 346 | } |
| 347 | return(k); |
| 348 | } |
| 349 | |
| 350 | newcand(x,y,pred) |
| 351 | { |
| 352 | register struct cand *q; |
| 353 | clist = (struct cand *)ralloc((char *)clist,++clen*sizeof(cand)); |
| 354 | q = clist + clen -1; |
| 355 | q->x = x; |
| 356 | q->y = y; |
| 357 | q->pred = pred; |
| 358 | return(clen-1); |
| 359 | } |
| 360 | |
| 361 | search(c, k, y) |
| 362 | int *c; |
| 363 | { |
| 364 | register int i, j, l; |
| 365 | int t; |
| 366 | if(clist[c[k]].y<y) /*quick look for typical case*/ |
| 367 | return(k+1); |
| 368 | i = 0; |
| 369 | j = k+1; |
| 370 | while (1) { |
| 371 | l = i + j; |
| 372 | if ((l >>= 1) <= i) |
| 373 | break; |
| 374 | t = clist[c[l]].y; |
| 375 | if(t > y) |
| 376 | j = l; |
| 377 | else if(t < y) |
| 378 | i = l; |
| 379 | else |
| 380 | return(l); |
| 381 | } |
| 382 | return(l+1); |
| 383 | } |
| 384 | |
| 385 | unravel(p) |
| 386 | { |
| 387 | register int i; |
| 388 | register struct cand *q; |
| 389 | for(i=0; i<=len[0]; i++) |
| 390 | J[i] = i<=pref ? i: |
| 391 | i>len[0]-suff ? i+len[1]-len[0]: |
| 392 | 0; |
| 393 | for(q=clist+p;q->y!=0;q=clist+q->pred) |
| 394 | J[q->x+pref] = q->y+pref; |
| 395 | } |
| 396 | |
| 397 | /* check does double duty: |
| 398 | 1. ferret out any fortuitous correspondences due |
| 399 | to confounding by hashing (which result in "jackpot") |
| 400 | 2. collect random access indexes to the two files */ |
| 401 | |
| 402 | check() |
| 403 | { |
| 404 | register int i, j; |
| 405 | int jackpot; |
| 406 | long ctold, ctnew; |
| 407 | char c,d; |
| 408 | |
| 409 | if ((input[0] = fopen(file1,"r")) == NULL) { |
| 410 | perror(file1); |
| 411 | done(); |
| 412 | } |
| 413 | if ((input[1] = fopen(file2,"r")) == NULL) { |
| 414 | perror(file2); |
| 415 | done(); |
| 416 | } |
| 417 | j = 1; |
| 418 | ixold[0] = ixnew[0] = 0; |
| 419 | jackpot = 0; |
| 420 | ctold = ctnew = 0; |
| 421 | for(i=1;i<=len[0];i++) { |
| 422 | if(J[i]==0) { |
| 423 | ixold[i] = ctold += skipline(0); |
| 424 | continue; |
| 425 | } |
| 426 | while(j<J[i]) { |
| 427 | ixnew[j] = ctnew += skipline(1); |
| 428 | j++; |
| 429 | } |
| 430 | for(;;) { |
| 431 | c = getc(input[0]); |
| 432 | d = getc(input[1]); |
| 433 | ctold++; |
| 434 | ctnew++; |
| 435 | if(bflag && isspace(c) && isspace(d)) { |
| 436 | do { |
| 437 | if(c=='\n') break; |
| 438 | ctold++; |
| 439 | } while(isspace(c=getc(input[0]))); |
| 440 | do { |
| 441 | if(d=='\n') break; |
| 442 | ctnew++; |
| 443 | } while(isspace(d=getc(input[1]))); |
| 444 | } |
| 445 | if(c!=d) { |
| 446 | jackpot++; |
| 447 | J[i] = 0; |
| 448 | if(c!='\n') |
| 449 | ctold += skipline(0); |
| 450 | if(d!='\n') |
| 451 | ctnew += skipline(1); |
| 452 | break; |
| 453 | } |
| 454 | if(c=='\n') |
| 455 | break; |
| 456 | } |
| 457 | ixold[i] = ctold; |
| 458 | ixnew[j] = ctnew; |
| 459 | j++; |
| 460 | } |
| 461 | for(;j<=len[1];j++) { |
| 462 | ixnew[j] = ctnew += skipline(1); |
| 463 | } |
| 464 | fclose(input[0]); |
| 465 | fclose(input[1]); |
| 466 | /* |
| 467 | if(jackpot) |
| 468 | fprintf(stderr, "jackpot\n"); |
| 469 | */ |
| 470 | } |
| 471 | |
| 472 | sort(a,n) /*shellsort CACM #201*/ |
| 473 | struct line *a; |
| 474 | { |
| 475 | struct line w; |
| 476 | register int j,m; |
| 477 | struct line *ai; |
| 478 | register struct line *aim; |
| 479 | int k; |
| 480 | for(j=1;j<=n;j*= 2) |
| 481 | m = 2*j - 1; |
| 482 | for(m/=2;m!=0;m/=2) { |
| 483 | k = n-m; |
| 484 | for(j=1;j<=k;j++) { |
| 485 | for(ai = &a[j]; ai > a; ai -= m) { |
| 486 | aim = &ai[m]; |
| 487 | if(aim < ai) |
| 488 | break; /*wraparound*/ |
| 489 | if(aim->value > ai[0].value || |
| 490 | aim->value == ai[0].value && |
| 491 | aim->serial > ai[0].serial) |
| 492 | break; |
| 493 | w.value = ai[0].value; |
| 494 | ai[0].value = aim->value; |
| 495 | aim->value = w.value; |
| 496 | w.serial = ai[0].serial; |
| 497 | ai[0].serial = aim->serial; |
| 498 | aim->serial = w.serial; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | unsort(f, l, b) |
| 505 | struct line *f; |
| 506 | int *b; |
| 507 | { |
| 508 | register int *a; |
| 509 | register int i; |
| 510 | a = (int *)talloc((l+1)*sizeof(int)); |
| 511 | for(i=1;i<=l;i++) |
| 512 | a[f[i].serial] = f[i].value; |
| 513 | for(i=1;i<=l;i++) |
| 514 | b[i] = a[i]; |
| 515 | free((char *)a); |
| 516 | } |
| 517 | |
| 518 | skipline(f) |
| 519 | { |
| 520 | register i; |
| 521 | char c; |
| 522 | |
| 523 | for(i=1;(c=getc(input[f]))!='\n';i++) |
| 524 | if (c < 0) |
| 525 | return(i); |
| 526 | return(i); |
| 527 | } |
| 528 | |
| 529 | output() |
| 530 | { |
| 531 | int m; |
| 532 | register int i0, i1, j1; |
| 533 | int j0; |
| 534 | input[0] = fopen(file1,"r"); |
| 535 | input[1] = fopen(file2,"r"); |
| 536 | m = len[0]; |
| 537 | J[0] = 0; |
| 538 | J[m+1] = len[1]+1; |
| 539 | if(opt!=D_EDIT) for(i0=1;i0<=m;i0=i1+1) { |
| 540 | while(i0<=m&&J[i0]==J[i0-1]+1) i0++; |
| 541 | j0 = J[i0-1]+1; |
| 542 | i1 = i0-1; |
| 543 | while(i1<m&&J[i1+1]==0) i1++; |
| 544 | j1 = J[i1+1]-1; |
| 545 | J[i1] = j1; |
| 546 | change(i0,i1,j0,j1); |
| 547 | } else for(i0=m;i0>=1;i0=i1-1) { |
| 548 | while(i0>=1&&J[i0]==J[i0+1]-1&&J[i0]!=0) i0--; |
| 549 | j0 = J[i0+1]-1; |
| 550 | i1 = i0+1; |
| 551 | while(i1>1&&J[i1-1]==0) i1--; |
| 552 | j1 = J[i1-1]+1; |
| 553 | J[i1] = j1; |
| 554 | change(i1,i0,j1,j0); |
| 555 | } |
| 556 | if(m==0) |
| 557 | change(1,0,1,len[1]); |
| 558 | if (opt==D_IFDEF) { |
| 559 | for (;;) { |
| 560 | #define c i0 |
| 561 | c = getc(input[0]); |
| 562 | if (c < 0) |
| 563 | return; |
| 564 | putchar(c); |
| 565 | } |
| 566 | #undef c |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* indicate that there is a difference between lines a and b of the from file |
| 571 | to get to lines c to d of the to file. |
| 572 | If a is greater then b then there are no lines in the from file involved |
| 573 | and this means that there were lines appended (beginning at b). |
| 574 | If c is greater than d then there are lines missing from the to file. |
| 575 | */ |
| 576 | change(a,b,c,d) |
| 577 | { |
| 578 | char ch; |
| 579 | int lowa,upb,lowc,upd; |
| 580 | struct stat stbuf; |
| 581 | |
| 582 | if (opt != D_IFDEF && a>b && c>d) |
| 583 | return; |
| 584 | if (anychange == 0) { |
| 585 | anychange = 1; |
| 586 | if(opt == D_CONTEXT) { |
| 587 | printf("*** %s ", file1); |
| 588 | stat(file1, &stbuf); |
| 589 | printf("%s--- %s ", |
| 590 | ctime(&stbuf.st_mtime), file2); |
| 591 | stat(file2, &stbuf); |
| 592 | printf("%s", ctime(&stbuf.st_mtime)); |
| 593 | } |
| 594 | } |
| 595 | if (a <= b && c <= d) |
| 596 | ch = 'c'; |
| 597 | else |
| 598 | ch = (a <= b) ? 'd' : 'a'; |
| 599 | if(opt == D_CONTEXT) { |
| 600 | lowa = max(1, a-context); |
| 601 | upb = min(len[0], b+context); |
| 602 | lowc = max(1, c-context); |
| 603 | upd = min(len[1], d+context); |
| 604 | |
| 605 | /* print out from file */ |
| 606 | printf("***************\n*** "); |
| 607 | range(lowa,upb,","); |
| 608 | printf("\n"); |
| 609 | if (ch == 'a') |
| 610 | fetch(ixold,lowa,upb,input[0]," "); |
| 611 | else { |
| 612 | fetch(ixold,lowa,a-1,input[0]," "); |
| 613 | fetch(ixold,a,b,input[0],ch == 'c' ? "! " : "- "); |
| 614 | fetch(ixold,b+1,upb,input[0]," "); |
| 615 | } |
| 616 | putchar('\n'); |
| 617 | printf("--- "); |
| 618 | range(lowc,upd,","); |
| 619 | printf(" -----\n"); |
| 620 | if (ch == 'd') |
| 621 | fetch(ixnew,lowc,upd,input[1]," "); |
| 622 | else { |
| 623 | fetch(ixnew,lowc,c-1,input[1]," "); |
| 624 | fetch(ixnew,c,d,input[1],ch == 'c' ? "! " : "+ "); |
| 625 | fetch(ixnew,d+1,upd,input[1]," "); |
| 626 | } |
| 627 | return; |
| 628 | } |
| 629 | switch (opt) { |
| 630 | |
| 631 | case D_NORMAL: |
| 632 | case D_EDIT: |
| 633 | range(a,b,","); |
| 634 | putchar(a>b?'a':c>d?'d':'c'); |
| 635 | if(opt==D_NORMAL) |
| 636 | range(c,d,","); |
| 637 | putchar('\n'); |
| 638 | break; |
| 639 | case D_REVERSE: |
| 640 | putchar(a>b?'a':c>d?'d':'c'); |
| 641 | range(a,b," "); |
| 642 | putchar('\n'); |
| 643 | break; |
| 644 | } |
| 645 | if(opt == D_NORMAL || opt == D_IFDEF) { |
| 646 | fetch(ixold,a,b,input[0],"< ", 1); |
| 647 | if(a<=b&&c<=d && opt == D_NORMAL) |
| 648 | prints("---\n"); |
| 649 | } |
| 650 | fetch(ixnew,c,d,input[1],opt==D_NORMAL?"> ":"", 0); |
| 651 | if ((opt ==D_EDIT || opt == D_REVERSE) && c<=d) |
| 652 | prints(".\n"); |
| 653 | if (inifdef) { |
| 654 | fprintf(stdout, "#endif %s\n", endifname); |
| 655 | inifdef = 0; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | range(a,b,separator) |
| 660 | char *separator; |
| 661 | { |
| 662 | printf("%d", a>b?b:a); |
| 663 | if(a<b) { |
| 664 | printf("%s%d", separator, b); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | fetch(f,a,b,lb,s,oldfile) |
| 669 | long *f; |
| 670 | FILE *lb; |
| 671 | char *s; |
| 672 | { |
| 673 | register int i, j; |
| 674 | register int nc; |
| 675 | int oneflag = (*ifdef1!='\0') != (*ifdef2!='\0'); |
| 676 | |
| 677 | /* |
| 678 | * When doing #ifdef's, copy down to current line |
| 679 | * if this is the first file, so that stuff makes it to output. |
| 680 | */ |
| 681 | if (opt == D_IFDEF && oldfile){ |
| 682 | long curpos = ftell(lb); |
| 683 | /* print through if append (a>b), else to (nb: 0 vs 1 orig) */ |
| 684 | nc = f[a>b? b : a-1 ] - curpos; |
| 685 | for (i = 0; i < nc; i++) |
| 686 | putchar(getc(lb)); |
| 687 | } |
| 688 | if (a > b) |
| 689 | return; |
| 690 | if (opt == D_IFDEF) { |
| 691 | if (inifdef) |
| 692 | fprintf(stdout, "#else %s%s\n", oneflag && oldfile==1 ? "!" : "", ifdef2); |
| 693 | else { |
| 694 | if (oneflag) { |
| 695 | /* There was only one ifdef given */ |
| 696 | endifname = ifdef2; |
| 697 | if (oldfile) |
| 698 | fprintf(stdout, "#ifndef %s\n", endifname); |
| 699 | else |
| 700 | fprintf(stdout, "#ifdef %s\n", endifname); |
| 701 | } |
| 702 | else { |
| 703 | endifname = oldfile ? ifdef1 : ifdef2; |
| 704 | fprintf(stdout, "#ifdef %s\n", endifname); |
| 705 | } |
| 706 | } |
| 707 | inifdef = 1+oldfile; |
| 708 | } |
| 709 | for(i=a;i<=b;i++) { |
| 710 | fseek(lb,f[i-1],0); |
| 711 | nc = f[i]-f[i-1]; |
| 712 | if (opt != D_IFDEF) |
| 713 | prints(s); |
| 714 | for(j=0;j<nc;j++) |
| 715 | putchar(getc(lb)); |
| 716 | } |
| 717 | if (inifdef && !wantelses) { |
| 718 | fprintf(stdout, "#endif %s\n", endifname); |
| 719 | inifdef = 0; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | #define HALFLONG 16 |
| 724 | #define low(x) (x&((1L<<HALFLONG)-1)) |
| 725 | #define high(x) (x>>HALFLONG) |
| 726 | |
| 727 | /* |
| 728 | * hashing has the effect of |
| 729 | * arranging line in 7-bit bytes and then |
| 730 | * summing 1-s complement in 16-bit hunks |
| 731 | */ |
| 732 | readhash(f) |
| 733 | register FILE *f; |
| 734 | { |
| 735 | long sum; |
| 736 | register unsigned shift; |
| 737 | register space; |
| 738 | register t; |
| 739 | sum = 1; |
| 740 | space = 0; |
| 741 | if(!bflag) for(shift=0;(t=getc(f))!='\n';shift+=7) { |
| 742 | if(t==-1) |
| 743 | return(0); |
| 744 | sum += (long)t << (shift &= HALFLONG - 1); |
| 745 | } |
| 746 | else for(shift=0;;) { |
| 747 | switch(t=getc(f)) { |
| 748 | case -1: |
| 749 | return(0); |
| 750 | case '\t': |
| 751 | case ' ': |
| 752 | space++; |
| 753 | continue; |
| 754 | default: |
| 755 | if(space) { |
| 756 | shift += 7; |
| 757 | space = 0; |
| 758 | } |
| 759 | sum += (long)t << (shift &= HALFLONG - 1); |
| 760 | shift += 7; |
| 761 | continue; |
| 762 | case '\n': |
| 763 | break; |
| 764 | } |
| 765 | break; |
| 766 | } |
| 767 | sum = low(sum) + high(sum); |
| 768 | return((short)low(sum) + (short)high(sum)); |
| 769 | } |
| 770 | |
| 771 | #include <a.out.h> |
| 772 | |
| 773 | asciifile(f) |
| 774 | FILE *f; |
| 775 | { |
| 776 | char buf[BUFSIZ]; |
| 777 | register int cnt; |
| 778 | register char *cp; |
| 779 | |
| 780 | fseek(f, (long)0, 0); |
| 781 | cnt = fread(buf, 1, BUFSIZ, f); |
| 782 | if (cnt >= sizeof (struct exec)) { |
| 783 | struct exec hdr; |
| 784 | hdr = *(struct exec *)buf; |
| 785 | if (!N_BADMAG(hdr)) |
| 786 | return (0); |
| 787 | } |
| 788 | cp = buf; |
| 789 | while (--cnt >= 0) |
| 790 | if (*cp++ & 0200) |
| 791 | return (0); |
| 792 | return (1); |
| 793 | } |