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