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