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