diff.c (63375B)
1 /* see LICENSE file for copyright and license details */ 2 #if FEATURE_DIFF_EXT 3 #ifndef _GNU_SOURCE 4 #define _GNU_SOURCE 5 #endif 6 #endif 7 #include "paths.h" 8 #include "util.h" 9 #include "wexec.h" 10 11 #include <sys/stat.h> 12 #include <sys/wait.h> 13 14 #include <ctype.h> 15 #include <dirent.h> 16 #include <errno.h> 17 #include <fcntl.h> 18 #include <fnmatch.h> 19 #include <getopt.h> 20 #include <limits.h> 21 #include <math.h> 22 #include <poll.h> 23 #include <regex.h> 24 #include <signal.h> 25 #include <stdint.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <time.h> 30 #include <unistd.h> 31 32 #ifndef __dead 33 #define __dead __attribute__((__noreturn__)) 34 #endif 35 36 #define d_status d_type 37 38 /* output format options */ 39 #define D_NORMAL 0 40 #define D_EDIT -1 41 #define D_REVERSE 1 42 #define D_CONTEXT 2 43 #define D_UNIFIED 3 44 #define D_IFDEF 4 45 #define D_NREVERSE 5 46 #define D_BRIEF 6 47 #define D_GFORMAT 7 48 #define D_SIDEBYSIDE 8 49 50 #define D_UNSET -2 51 52 /* algorithms */ 53 #define D_DIFFNONE 0 54 #define D_DIFFSTONE 1 55 #define D_DIFFMYERS 2 56 #define D_DIFFPATIENCE 3 57 58 /* output flags */ 59 #define D_HEADER 0x001 60 #define D_EMPTY1 0x002 61 #define D_EMPTY2 0x004 62 63 /* command line flags */ 64 #define D_FORCEASCII 0x008 65 #define D_FOLDBLANKS 0x010 66 #define D_MINIMAL 0x020 67 #define D_IGNORECASE 0x040 68 #define D_PROTOTYPE 0x080 69 #define D_EXPANDTABS 0x100 70 #define D_IGNOREBLANKS 0x200 71 #define D_STRIPCR 0x400 72 #define D_SKIPBLANKLINES 0x800 73 #define D_MATCHLAST 0x1000 74 75 /* features supported by new algorithms */ 76 #define D_NEWALGO_FLAGS (D_FORCEASCII | D_PROTOTYPE | D_IGNOREBLANKS) 77 78 /* status values for print_status and diffreg return values */ 79 #define D_SAME 0 80 #define D_DIFFER 1 81 #define D_BINARY 2 82 #define D_MISMATCH1 3 83 #define D_MISMATCH2 4 84 #define D_SKIPPED1 5 85 #define D_SKIPPED2 6 86 #define D_ERROR 7 87 88 /* color options */ 89 #define COLORFLAG_NEVER 0 90 #define COLORFLAG_AUTO 1 91 #define COLORFLAG_ALWAYS 2 92 93 #define FUNCTION_CONTEXT_SIZE 55 94 95 #ifndef roundup 96 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 97 #endif 98 99 struct Excludes { 100 char *pattern; 101 struct Excludes *next; 102 }; 103 104 struct Cand { 105 int x; 106 int y; 107 int pred; 108 }; 109 110 struct Line { 111 int serial; 112 int value; 113 }; 114 115 struct ContextVec { 116 int a; 117 int b; 118 int c; 119 int d; 120 }; 121 122 struct Pr { 123 int ostdout; 124 pid_t cpid; 125 }; 126 127 struct Algorithm { 128 const char *name; 129 int id; 130 }; 131 132 static struct Algorithm algorithms[] = { 133 {"stone", D_DIFFSTONE}, {"myers", D_DIFFMYERS}, {"patience", D_DIFFPATIENCE}, {NULL, D_DIFFNONE} 134 }; 135 136 /* options */ 137 #define OPTIONS "0123456789A:aBbC:cdD:efF:HhI:iL:lnNPpqrS:sTtU:uwW:X:x:y" 138 enum { 139 OPT_TSIZE = CHAR_MAX + 1, 140 OPT_STRIPCR, 141 OPT_IGN_FN_CASE, 142 OPT_NO_IGN_FN_CASE, 143 OPT_NORMAL, 144 OPT_HELP, 145 OPT_HORIZON_LINES, 146 OPT_CHANGED_GROUP_FORMAT, 147 OPT_SUPPRESS_COMMON, 148 OPT_COLOR, 149 OPT_NO_DEREFERENCE, 150 OPT_VERSION, 151 }; 152 153 static struct option longopts[] = { 154 {"algorithm", required_argument, 0, 'A'}, 155 {"text", no_argument, 0, 'a'}, 156 {"ignore-space-change", no_argument, 0, 'b'}, 157 {"context", optional_argument, 0, 'C'}, 158 {"ifdef", required_argument, 0, 'D'}, 159 {"minimal", no_argument, 0, 'd'}, 160 {"ed", no_argument, 0, 'e'}, 161 {"forward-ed", no_argument, 0, 'f'}, 162 {"show-function-line", required_argument, 0, 'F'}, 163 {"speed-large-files", no_argument, NULL, 'H'}, 164 {"ignore-blank-lines", no_argument, 0, 'B'}, 165 {"ignore-matching-lines", required_argument, 0, 'I'}, 166 {"ignore-case", no_argument, 0, 'i'}, 167 {"paginate", no_argument, NULL, 'l'}, 168 {"label", required_argument, 0, 'L'}, 169 {"new-file", no_argument, 0, 'N'}, 170 {"rcs", no_argument, 0, 'n'}, 171 {"unidirectional-new-file", no_argument, 0, 'P'}, 172 {"show-c-function", no_argument, 0, 'p'}, 173 {"brief", no_argument, 0, 'q'}, 174 {"recursive", no_argument, 0, 'r'}, 175 {"report-identical-files", no_argument, 0, 's'}, 176 {"starting-file", required_argument, 0, 'S'}, 177 {"expand-tabs", no_argument, 0, 't'}, 178 {"initial-tab", no_argument, 0, 'T'}, 179 {"unified", optional_argument, 0, 'U'}, 180 {"ignore-all-space", no_argument, 0, 'w'}, 181 {"width", required_argument, 0, 'W'}, 182 {"exclude", required_argument, 0, 'x'}, 183 {"exclude-from", required_argument, 0, 'X'}, 184 {"side-by-side", no_argument, NULL, 'y'}, 185 {"ignore-file-name-case", no_argument, NULL, OPT_IGN_FN_CASE}, 186 {"help", no_argument, NULL, OPT_HELP}, 187 {"horizon-lines", required_argument, NULL, OPT_HORIZON_LINES}, 188 {"no-dereference", no_argument, NULL, OPT_NO_DEREFERENCE}, 189 {"no-ignore-file-name-case", no_argument, NULL, OPT_NO_IGN_FN_CASE}, 190 {"normal", no_argument, NULL, OPT_NORMAL}, 191 {"strip-trailing-cr", no_argument, NULL, OPT_STRIPCR}, 192 {"tabsize", required_argument, NULL, OPT_TSIZE}, 193 {"changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT}, 194 {"suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON}, 195 {"color", optional_argument, NULL, OPT_COLOR}, 196 {"version", no_argument, NULL, OPT_VERSION}, 197 {NULL, 0, 0, '\0'} 198 }; 199 200 static const char diff_version[] = "FreeBSD diff 20240307"; 201 int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag; 202 int ignore_file_case, suppress_common, color, noderef; 203 static int help = 0; 204 int diff_format, diff_context, diff_algorithm, status; 205 int diff_algorithm_set; 206 int tabsize = 8, width = 130; 207 static int colorflag = COLORFLAG_NEVER; 208 char *start, *ifdefname, *diffargs, *label[2]; 209 char *ignore_pats, *most_recent_pat; 210 char *group_format = NULL; 211 const char *add_code, *del_code; 212 struct stat stb1, stb2; 213 struct Excludes *excludes_list; 214 regex_t ignore_re, most_recent_re; 215 216 enum Readhash { RH_BINARY, RH_OK, RH_EOF }; 217 218 static struct Line *file[2]; 219 220 static int *J; 221 static int *class; 222 static int *klist; 223 static int *member; 224 static int clen; 225 static int inifdef; 226 static size_t len[2]; 227 static size_t pref, suff; 228 static size_t slen[2]; 229 static int anychange; 230 static int hw, lpad, rpad; 231 static int edoffset; 232 static long *ixnew; 233 static long *ixold; 234 static struct Cand *clist; 235 static int clistlen; 236 static struct Line *sfile[2]; 237 static int (*chrtran)(int); 238 static struct ContextVec *context_vec_start; 239 static struct ContextVec *context_vec_end; 240 static struct ContextVec *context_vec_ptr; 241 static char lastbuf[FUNCTION_CONTEXT_SIZE]; 242 static int lastline; 243 static int lastmatchline; 244 245 static int sigpipe[2] = {-1, -1}; 246 static struct pollfd poll_fd; 247 248 static void checked_regcomp(char const *, regex_t *); 249 static void usage(void); 250 static void conflicting_format(void); 251 static void push_excludes(char *); 252 static void push_ignore_pats(char *); 253 static void read_excludes_file(char *); 254 static void set_argstr(char **, char **); 255 static char *diff_splice(char *, char *); 256 static int do_color(void); 257 static int cup2low(int); 258 static int clow2low(int); 259 static void xasprintf(char **, const char *, ...); 260 261 int diffreg(char *, char *, int, int); 262 void diffdir(char *, char *, int); 263 void print_status(int, char *, char *, const char *); 264 265 static int selectfile(const struct dirent *); 266 static void diffit(struct dirent *, char *, size_t, struct dirent *, char *, size_t, int); 267 static void print_only(const char *, size_t, const char *); 268 269 static FILE *opentemp(const char *); 270 static void output(char *, FILE *, char *, FILE *, int); 271 static void check(FILE *, FILE *, int); 272 static void range(int, int, const char *); 273 static void uni_range(int, int); 274 static void dump_context_vec(FILE *, FILE *, int); 275 static void dump_unified_vec(FILE *, FILE *, int); 276 static int prepare(int, FILE *, size_t, int); 277 static void prune(void); 278 static void equiv(struct Line *, int, struct Line *, int, int *); 279 static void unravel(int); 280 static void unsort(struct Line *, int, int *); 281 static void change(char *, FILE *, char *, FILE *, int, int, int, int, int *); 282 static void sort(struct Line *, int); 283 static void print_header(const char *, const char *); 284 static void print_space(int, int, int); 285 static int ignoreline_pattern(char *); 286 static int ignoreline(char *, int); 287 static int asciifile(FILE *); 288 static int fetch(long *, int, int, FILE *, int, int, int); 289 static int newcand(int, int, int); 290 static int search(int *, int, int); 291 static int skipline(FILE *); 292 static int stone(int *, int, int *, int *, int); 293 static enum Readhash readhash(FILE *, int, unsigned *); 294 static int files_differ(FILE *, FILE *, int); 295 static char *match_function(const long *, int, FILE *); 296 static char *preadline(int, size_t, off_t); 297 298 static void handle_sig(int); 299 struct Pr *start_pr(char *, char *); 300 void stop_pr(struct Pr *); 301 302 static void 303 handle_sig(int signo) 304 { 305 write(sigpipe[1], &signo, sizeof(signo)); 306 } 307 308 struct Pr * 309 start_pr(char *file1, char *file2) 310 { 311 int pfd[2]; 312 pid_t pid; 313 char *header; 314 struct Pr *pr; 315 316 pr = ecalloc(1, sizeof(*pr)); 317 xasprintf(&header, "%s %s %s", diffargs, file1, file2); 318 signal(SIGPIPE, SIG_IGN); 319 fflush(stdout); 320 if (pipe(pfd) == -1) 321 enprintf(2, "pipe"); 322 if (sigpipe[0] < 0) { 323 if (pipe(sigpipe) == -1) 324 enprintf(2, "pipe"); 325 if (fcntl(sigpipe[0], F_SETFD, FD_CLOEXEC) == -1) 326 enprintf(2, "fcntl"); 327 if (fcntl(sigpipe[1], F_SETFD, FD_CLOEXEC) == -1) 328 enprintf(2, "fcntl"); 329 if (signal(SIGCHLD, handle_sig) == SIG_ERR) 330 enprintf(2, "signal"); 331 poll_fd.fd = sigpipe[0]; 332 poll_fd.events = POLLIN; 333 } 334 poll_fd.revents = 0; 335 switch ((pid = fork())) { 336 case -1: 337 status |= 2; 338 free(header); 339 enprintf(2, "no more processes"); 340 /* fallthrough */ 341 case 0: 342 if (pfd[0] != STDIN_FILENO) { 343 dup2(pfd[0], STDIN_FILENO); 344 close(pfd[0]); 345 } 346 close(pfd[1]); 347 { 348 char *pr_argv[4]; 349 pr_argv[0] = "pr"; 350 pr_argv[1] = "-h"; 351 pr_argv[2] = header; 352 pr_argv[3] = NULL; 353 wexecvp_self("pr", pr_argv); 354 } 355 _exit(127); 356 default: 357 if (pfd[1] != STDOUT_FILENO) { 358 pr->ostdout = dup(STDOUT_FILENO); 359 dup2(pfd[1], STDOUT_FILENO); 360 close(pfd[1]); 361 } 362 close(pfd[0]); 363 free(header); 364 pr->cpid = pid; 365 } 366 return pr; 367 } 368 369 void 370 stop_pr(struct Pr *pr) 371 { 372 int wstatus; 373 int done = 0; 374 375 if (!pr) 376 return; 377 378 fflush(stdout); 379 if (pr->ostdout != STDOUT_FILENO) { 380 close(STDOUT_FILENO); 381 dup2(pr->ostdout, STDOUT_FILENO); 382 close(pr->ostdout); 383 } 384 while (!done) { 385 pid_t wpid; 386 int npe = poll(&poll_fd, 1, -1); 387 if (npe == -1) { 388 if (errno == EINTR) 389 continue; 390 enprintf(2, "poll"); 391 } 392 if (poll_fd.revents != POLLIN) 393 continue; 394 if (read(poll_fd.fd, &npe, sizeof(npe)) < 0) 395 enprintf(2, "read"); 396 while ((wpid = waitpid(-1, &wstatus, WNOHANG)) > 0) { 397 if (wpid != pr->cpid) 398 continue; 399 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) 400 enprintf(2, "pr exited abnormally"); 401 else if (WIFSIGNALED(wstatus)) 402 enprintf(2, "pr killed by signal %d", WTERMSIG(wstatus)); 403 done = 1; 404 break; 405 } 406 } 407 free(pr); 408 } 409 410 // ?man diff: differential file and directory comparator 411 // ?man arguments: file1 file2 412 // ?man synopsis: [-aBbdipTtw] [-c|-e|-f|-n|-q|-u|-y] [-A algo] [--brief] [--color=when] [--changed-group-format GFMT] [--ed] [--expand-tabs] [--forward-ed] [--ignore-all-space] [--ignore-case] [--ignore-space-change] [--initial-tab] [--minimal] [--no-dereference] [--no-ignore-file-name-case] [--normal] [--rcs] [--show-c-function] [--starting-file] [--speed-large-files] [--strip-trailing-cr] [--tabsize number] [--text] [-I pattern] [-F pattern] [-L label] file1 file2 413 int 414 main(int argc, char **argv) 415 { 416 const char *errstr = NULL; 417 char *ep, **oargv; 418 long l; 419 int ch, dflags, lastch, gotstdin, prevoptind, newarg; 420 421 oargv = argv; 422 gotstdin = 0; 423 dflags = 0; 424 lastch = '\0'; 425 prevoptind = 1; 426 newarg = 1; 427 diff_context = 3; 428 diff_format = D_UNSET; 429 diff_algorithm = D_DIFFMYERS; 430 diff_algorithm_set = 0; 431 #define FORMAT_MISMATCHED(type) (diff_format != D_UNSET && diff_format != (type)) 432 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 433 // ?man -0: context length 434 // ?man -1: context length 435 // ?man -2: context length 436 // ?man -3: context length 437 // ?man -4: context length 438 // ?man -5: context length 439 // ?man -6: context length 440 // ?man -7: context length 441 // ?man -8: context length 442 // ?man -9: context length 443 switch (ch) { 444 case '0': 445 case '1': 446 case '2': 447 case '3': 448 case '4': 449 case '5': 450 case '6': 451 case '7': 452 case '8': 453 case '9': 454 if (newarg) 455 usage(); 456 else if (lastch == 'c' || lastch == 'u') 457 diff_context = 0; 458 else if (!isdigit(lastch) || diff_context > INT_MAX / 10) 459 usage(); 460 diff_context = (diff_context * 10) + (ch - '0'); 461 break; 462 // ?man -A:algo: algorithm (stone, myers, patience) 463 case 'A': 464 diff_algorithm = D_DIFFNONE; 465 for (struct Algorithm *a = algorithms; a->name; a++) { 466 if (strcasecmp(optarg, a->name) == 0) { 467 diff_algorithm = a->id; 468 diff_algorithm_set = 1; 469 break; 470 } 471 } 472 if (diff_algorithm == D_DIFFNONE) { 473 printf("unknown algorithm: %s\n", optarg); 474 usage(); 475 } 476 break; 477 // ?man -a: force text mode 478 case 'a': 479 dflags |= D_FORCEASCII; 480 break; 481 // ?man -b: ignore space changes 482 case 'b': 483 dflags |= D_FOLDBLANKS; 484 break; 485 // ?man -C:lines: context format 486 case 'C': 487 // ?man -c: context format 488 case 'c': 489 if (FORMAT_MISMATCHED(D_CONTEXT)) 490 conflicting_format(); 491 cflag = 1; 492 diff_format = D_CONTEXT; 493 if (optarg != NULL) { 494 l = strtol(optarg, &ep, 10); 495 if (*ep != '\0' || l < 0 || l >= INT_MAX) 496 usage(); 497 diff_context = (int)l; 498 } 499 break; 500 // ?man -d: minimal diff search 501 case 'd': 502 dflags |= D_MINIMAL; 503 break; 504 // ?man -D:name: ifdef format 505 case 'D': 506 if (FORMAT_MISMATCHED(D_IFDEF)) 507 conflicting_format(); 508 diff_format = D_IFDEF; 509 ifdefname = optarg; 510 break; 511 // ?man -e: ed script format 512 case 'e': 513 if (FORMAT_MISMATCHED(D_EDIT)) 514 conflicting_format(); 515 diff_format = D_EDIT; 516 break; 517 // ?man -f: forward ed script format 518 case 'f': 519 if (FORMAT_MISMATCHED(D_REVERSE)) 520 conflicting_format(); 521 diff_format = D_REVERSE; 522 break; 523 // ?man -H: speed up large files (stub) 524 case 'H': 525 break; 526 // ?man -h: backward compatibility (stub) 527 case 'h': 528 break; 529 // ?man -B: ignore blank lines 530 case 'B': 531 dflags |= D_SKIPBLANKLINES; 532 break; 533 // ?man -F:pat: show function matching pattern 534 case 'F': 535 if (dflags & D_PROTOTYPE) 536 conflicting_format(); 537 dflags |= D_MATCHLAST; 538 most_recent_pat = estrdup(optarg); 539 break; 540 // ?man -I:pat: ignore pattern matching lines 541 case 'I': 542 push_ignore_pats(optarg); 543 break; 544 // ?man -i: ignore case 545 case 'i': 546 dflags |= D_IGNORECASE; 547 break; 548 // ?man -L:label: custom label 549 case 'L': 550 if (label[0] == NULL) 551 label[0] = optarg; 552 else if (label[1] == NULL) 553 label[1] = optarg; 554 else 555 usage(); 556 break; 557 // ?man -l: paginate output using pr 558 case 'l': 559 lflag = 1; 560 break; 561 // ?man -N: treat missing files as empty 562 case 'N': 563 Nflag = 1; 564 break; 565 // ?man -n: rcs format 566 case 'n': 567 if (FORMAT_MISMATCHED(D_NREVERSE)) 568 conflicting_format(); 569 diff_format = D_NREVERSE; 570 break; 571 // ?man -p: show C prototype context 572 case 'p': 573 if (dflags & D_MATCHLAST) 574 conflicting_format(); 575 dflags |= D_PROTOTYPE; 576 break; 577 // ?man -P: treat missing destination files as empty 578 case 'P': 579 Pflag = 1; 580 break; 581 // ?man -r: recursive directory comparison 582 case 'r': 583 rflag = 1; 584 break; 585 // ?man -q: brief output (differ / same only) 586 case 'q': 587 if (FORMAT_MISMATCHED(D_BRIEF)) 588 conflicting_format(); 589 diff_format = D_BRIEF; 590 break; 591 // ?man -S:name: starting file in directory comparison 592 case 'S': 593 start = optarg; 594 break; 595 // ?man -s: report identical files 596 case 's': 597 sflag = 1; 598 break; 599 // ?man -T: initial tab alignment 600 case 'T': 601 Tflag = 1; 602 break; 603 // ?man -t: expand tabs in output 604 case 't': 605 dflags |= D_EXPANDTABS; 606 break; 607 // ?man -U:lines: unified context format 608 case 'U': 609 // ?man -u: unified context format 610 case 'u': 611 if (FORMAT_MISMATCHED(D_UNIFIED)) 612 conflicting_format(); 613 diff_format = D_UNIFIED; 614 if (optarg != NULL) { 615 l = strtol(optarg, &ep, 10); 616 if (*ep != '\0' || l < 0 || l >= INT_MAX) 617 usage(); 618 diff_context = (int)l; 619 } 620 break; 621 // ?man -w: ignore all whitespace 622 case 'w': 623 dflags |= D_IGNOREBLANKS; 624 break; 625 // ?man -W:cols: column width for side-by-side 626 case 'W': 627 width = (int)strtonum(optarg, 1, INT_MAX, &errstr); 628 if (errstr) { 629 weprintf("invalid width argument"); 630 usage(); 631 } 632 break; 633 // ?man -X:file: exclude patterns file 634 case 'X': 635 read_excludes_file(optarg); 636 break; 637 // ?man -x:pat: exclude pattern 638 case 'x': 639 push_excludes(optarg); 640 break; 641 // ?man -y: side by side format 642 case 'y': 643 if (FORMAT_MISMATCHED(D_SIDEBYSIDE)) 644 conflicting_format(); 645 diff_format = D_SIDEBYSIDE; 646 break; 647 case OPT_CHANGED_GROUP_FORMAT: 648 if (FORMAT_MISMATCHED(D_GFORMAT)) 649 conflicting_format(); 650 diff_format = D_GFORMAT; 651 group_format = optarg; 652 break; 653 case OPT_HELP: 654 help = 1; 655 usage(); 656 break; 657 case OPT_HORIZON_LINES: 658 break; 659 case OPT_IGN_FN_CASE: 660 ignore_file_case = 1; 661 break; 662 case OPT_NO_IGN_FN_CASE: 663 ignore_file_case = 0; 664 break; 665 case OPT_NORMAL: 666 if (FORMAT_MISMATCHED(D_NORMAL)) 667 conflicting_format(); 668 diff_format = D_NORMAL; 669 break; 670 case OPT_TSIZE: 671 tabsize = (int)strtonum(optarg, 1, INT_MAX, &errstr); 672 if (errstr) { 673 weprintf("invalid tabsize argument"); 674 usage(); 675 } 676 break; 677 case OPT_STRIPCR: 678 dflags |= D_STRIPCR; 679 break; 680 case OPT_SUPPRESS_COMMON: 681 suppress_common = 1; 682 break; 683 case OPT_COLOR: 684 if (optarg == NULL || strncmp(optarg, "auto", 4) == 0) 685 colorflag = COLORFLAG_AUTO; 686 else if (strncmp(optarg, "always", 6) == 0) 687 colorflag = COLORFLAG_ALWAYS; 688 else if (strncmp(optarg, "never", 5) == 0) 689 colorflag = COLORFLAG_NEVER; 690 else 691 enprintf(2, "unsupported color option %s", optarg); 692 break; 693 case OPT_NO_DEREFERENCE: 694 noderef = 1; 695 break; 696 case OPT_VERSION: 697 printf("%s\n", diff_version); 698 exit(0); 699 default: 700 usage(); 701 break; 702 } 703 lastch = ch; 704 newarg = optind != prevoptind; 705 prevoptind = optind; 706 } 707 if (diff_format == D_UNSET && (dflags & D_PROTOTYPE) != 0) 708 diff_format = D_CONTEXT; 709 if (diff_format == D_UNSET) 710 diff_format = D_NORMAL; 711 argc -= optind; 712 argv += optind; 713 714 if (do_color()) { 715 char *p; 716 const char *env; 717 718 color = 1; 719 add_code = "32"; 720 del_code = "31"; 721 env = getenv("DIFFCOLORS"); 722 if (env != NULL && *env != '\0' && (p = estrdup(env))) { 723 add_code = p; 724 strsep(&p, ":"); 725 if (p != NULL) 726 del_code = p; 727 } 728 } 729 730 if (argc != 2) 731 usage(); 732 checked_regcomp(ignore_pats, &ignore_re); 733 checked_regcomp(most_recent_pat, &most_recent_re); 734 if (strcmp(argv[0], "-") == 0) { 735 fstat(STDIN_FILENO, &stb1); 736 gotstdin = 1; 737 } else if (stat(argv[0], &stb1) != 0) { 738 if (!Nflag || errno != ENOENT) 739 enprintf(2, "%s", argv[0]); 740 dflags |= D_EMPTY1; 741 memset(&stb1, 0, sizeof(struct stat)); 742 } 743 744 if (strcmp(argv[1], "-") == 0) { 745 fstat(STDIN_FILENO, &stb2); 746 gotstdin = 1; 747 } else if (stat(argv[1], &stb2) != 0) { 748 if (!Nflag || errno != ENOENT) 749 enprintf(2, "%s", argv[1]); 750 dflags |= D_EMPTY2; 751 memset(&stb2, 0, sizeof(stb2)); 752 stb2.st_mode = stb1.st_mode; 753 } 754 755 if (dflags & D_EMPTY1 && dflags & D_EMPTY2) { 756 weprintf("%s", argv[0]); 757 weprintf("%s", argv[1]); 758 exit(2); 759 } 760 761 if (stb1.st_mode == 0) 762 stb1.st_mode = stb2.st_mode; 763 764 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 765 enprintf(2, "cant compare - to directory"); 766 set_argstr(oargv, argv); 767 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 768 if (diff_format == D_IFDEF) 769 enprintf(2, "-D option not supported with directories"); 770 diffdir(argv[0], argv[1], dflags); 771 } else { 772 if (S_ISDIR(stb1.st_mode)) { 773 argv[0] = diff_splice(argv[0], argv[1]); 774 if (stat(argv[0], &stb1) == -1) 775 enprintf(2, "%s", argv[0]); 776 } 777 if (S_ISDIR(stb2.st_mode)) { 778 argv[1] = diff_splice(argv[1], argv[0]); 779 if (stat(argv[1], &stb2) == -1) 780 enprintf(2, "%s", argv[1]); 781 } 782 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0], argv[1], ""); 783 } 784 if (fflush(stdout) != 0) 785 enprintf(2, "stdout"); 786 exit(status); 787 } 788 789 static void 790 checked_regcomp(char const *pattern, regex_t *comp) 791 { 792 char buf[BUFSIZ]; 793 int error; 794 795 if (pattern == NULL) 796 return; 797 798 error = regcomp(comp, pattern, REG_NEWLINE | REG_EXTENDED); 799 if (error != 0) { 800 regerror(error, comp, buf, sizeof(buf)); 801 if (*pattern != '\0') 802 enprintf(2, "%s: %s", pattern, buf); 803 else 804 enprintf(2, "%s", buf); 805 } 806 } 807 808 static void 809 set_argstr(char **av, char **ave) 810 { 811 size_t argsize; 812 char **ap; 813 814 argsize = strlen("diff") + 1; 815 for (ap = av + 1; ap < ave; ap++) { 816 if (strcmp(*ap, "--") != 0) 817 argsize += 1 + strlen(*ap); 818 } 819 diffargs = emalloc(argsize); 820 strlcpy(diffargs, "diff", argsize); 821 for (ap = av + 1; ap < ave; ap++) { 822 if (strcmp(*ap, "--") != 0) { 823 strlcat(diffargs, " ", argsize); 824 strlcat(diffargs, *ap, argsize); 825 } 826 } 827 } 828 829 static void 830 read_excludes_file(char *file) 831 { 832 FILE *fp; 833 char *pattern = NULL; 834 size_t blen = 0; 835 ssize_t len; 836 837 if (strcmp(file, "-") == 0) 838 fp = stdin; 839 else if ((fp = fopen(file, "r")) == NULL) 840 enprintf(2, "%s", file); 841 while ((len = getline(&pattern, &blen, fp)) >= 0) { 842 if ((len > 0) && (pattern[len - 1] == '\n')) 843 pattern[len - 1] = '\0'; 844 push_excludes(pattern); 845 pattern = NULL; 846 blen = 0; 847 } 848 free(pattern); 849 if (strcmp(file, "-") != 0) 850 fclose(fp); 851 } 852 853 static void 854 push_excludes(char *pattern) 855 { 856 struct Excludes *entry; 857 858 entry = emalloc(sizeof(*entry)); 859 entry->pattern = pattern; 860 entry->next = excludes_list; 861 excludes_list = entry; 862 } 863 864 static void 865 push_ignore_pats(char *pattern) 866 { 867 size_t len; 868 869 if (ignore_pats == NULL) 870 ignore_pats = estrdup(pattern); 871 else { 872 len = strlen(ignore_pats) + strlen(pattern) + 2; 873 ignore_pats = ereallocarray(ignore_pats, 1, len); 874 strlcat(ignore_pats, "|", len); 875 strlcat(ignore_pats, pattern, len); 876 } 877 } 878 879 void 880 print_status(int val, char *path1, char *path2, const char *entry) 881 { 882 if (label[0] != NULL) 883 path1 = label[0]; 884 if (label[1] != NULL) 885 path2 = label[1]; 886 887 switch (val) { 888 case D_BINARY: 889 printf("Binary files %s%s and %s%s differ\n", path1, entry, path2, entry); 890 break; 891 case D_DIFFER: 892 if (diff_format == D_BRIEF) 893 printf("Files %s%s and %s%s differ\n", path1, entry, path2, entry); 894 break; 895 case D_SAME: 896 if (sflag) 897 printf("Files %s%s and %s%s are identical\n", path1, entry, path2, entry); 898 break; 899 case D_MISMATCH1: 900 printf("File %s%s is directory, %s%s is file\n", path1, entry, path2, entry); 901 break; 902 case D_MISMATCH2: 903 printf("File %s%s is file, %s%s is directory\n", path1, entry, path2, entry); 904 break; 905 case D_SKIPPED1: 906 printf("File %s%s is not regular file or directory\n", path1, entry); 907 break; 908 case D_SKIPPED2: 909 printf("File %s%s is not regular file or directory\n", path2, entry); 910 break; 911 } 912 } 913 914 static void 915 usage(void) 916 { 917 fprintf( 918 stderr, 919 "usage: diff [-aBbdipTtw] [-c|-e|-f|-n|-q|-u|-y] [-A algo] " 920 "[-I pattern] [-F pattern] [-L label] file1 file2\n" 921 ); 922 exit(2); 923 } 924 925 static void 926 conflicting_format(void) 927 { 928 enprintf(2, "conflicting output format options"); 929 } 930 931 static char * 932 diff_splice(char *dir, char *file) 933 { 934 char *p, *res; 935 936 p = strrchr(file, '/'); 937 p = p ? p + 1 : file; 938 xasprintf(&res, "%s/%s", dir, p); 939 return res; 940 } 941 942 static void 943 xasprintf(char **strp, const char *fmt, ...) 944 { 945 va_list ap; 946 int r; 947 948 va_start(ap, fmt); 949 r = vasprintf(strp, fmt, ap); 950 va_end(ap); 951 if (r == -1) 952 enprintf(2, "asprintf"); 953 } 954 955 static int 956 cup2low(int c) 957 { 958 return tolower(c); 959 } 960 961 static int 962 clow2low(int c) 963 { 964 return c; 965 } 966 967 static int 968 do_color(void) 969 { 970 if (colorflag == COLORFLAG_ALWAYS) 971 return 1; 972 if (colorflag == COLORFLAG_NEVER) 973 return 0; 974 if (isatty(STDOUT_FILENO)) { 975 char *term = getenv("COLORTERM"); 976 if (term && *term != '\0') 977 return 1; 978 } 979 return 0; 980 } 981 982 void 983 diffdir(char *p1, char *p2, int flags) 984 { 985 struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL; 986 struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL; 987 size_t dirlen1, dirlen2; 988 char path1[PATH_MAX], path2[PATH_MAX]; 989 int pos; 990 991 edp1 = edp2 = NULL; 992 dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1)); 993 if (dirlen1 >= sizeof(path1) - 1) { 994 errno = ENAMETOOLONG; 995 weprintf("%s", p1); 996 status |= 2; 997 return; 998 } 999 if (path1[dirlen1 - 1] != '/') { 1000 path1[dirlen1++] = '/'; 1001 path1[dirlen1] = '\0'; 1002 } 1003 dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2)); 1004 if (dirlen2 >= sizeof(path2) - 1) { 1005 errno = ENAMETOOLONG; 1006 weprintf("%s", p2); 1007 status |= 2; 1008 return; 1009 } 1010 if (path2[dirlen2 - 1] != '/') { 1011 path2[dirlen2++] = '/'; 1012 path2[dirlen2] = '\0'; 1013 } 1014 1015 pos = scandir(path1, &dirp1, selectfile, alphasort); 1016 if (pos == -1) { 1017 if (errno == ENOENT && (Nflag || Pflag)) 1018 pos = 0; 1019 else { 1020 weprintf("%s", path1); 1021 goto closem; 1022 } 1023 } 1024 dp1 = dirp1; 1025 edp1 = dirp1 + pos; 1026 1027 pos = scandir(path2, &dirp2, selectfile, alphasort); 1028 if (pos == -1) { 1029 if (errno == ENOENT && Nflag) 1030 pos = 0; 1031 else { 1032 weprintf("%s", path2); 1033 goto closem; 1034 } 1035 } 1036 dp2 = dirp2; 1037 edp2 = dirp2 + pos; 1038 1039 if (start != NULL) { 1040 while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0) 1041 dp1++; 1042 while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0) 1043 dp2++; 1044 } 1045 1046 while (dp1 != edp1 || dp2 != edp2) { 1047 dent1 = dp1 != edp1 ? *dp1 : NULL; 1048 dent2 = dp2 != edp2 ? *dp2 : NULL; 1049 1050 pos = dent1 == NULL ? 1 1051 : dent2 == NULL ? -1 1052 : ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) 1053 : strcmp(dent1->d_name, dent2->d_name); 1054 if (pos == 0) { 1055 diffit(dent1, path1, dirlen1, dent2, path2, dirlen2, flags); 1056 dp1++; 1057 dp2++; 1058 } else if (pos < 0) { 1059 if (Nflag) 1060 diffit(dent1, path1, dirlen1, dent2, path2, dirlen2, flags); 1061 else { 1062 print_only(path1, dirlen1, dent1->d_name); 1063 status |= 1; 1064 } 1065 dp1++; 1066 } else { 1067 if (Nflag || Pflag) 1068 diffit(dent2, path1, dirlen1, dent1, path2, dirlen2, flags); 1069 else { 1070 print_only(path2, dirlen2, dent2->d_name); 1071 status |= 1; 1072 } 1073 dp2++; 1074 } 1075 } 1076 1077 closem: 1078 if (dirp1 != NULL) { 1079 for (dp1 = dirp1; dp1 < edp1; dp1++) 1080 free(*dp1); 1081 free(dirp1); 1082 } 1083 if (dirp2 != NULL) { 1084 for (dp2 = dirp2; dp2 < edp2; dp2++) 1085 free(*dp2); 1086 free(dirp2); 1087 } 1088 } 1089 1090 static void 1091 diffit( 1092 struct dirent *dp, 1093 char *path1, 1094 size_t plen1, 1095 struct dirent *dp2, 1096 char *path2, 1097 size_t plen2, 1098 int flags 1099 ) 1100 { 1101 flags |= D_HEADER; 1102 strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1); 1103 1104 if (ignore_file_case && strcasecmp(dp->d_name, dp2->d_name) == 0) 1105 strlcpy(path2 + plen2, dp2->d_name, PATH_MAX - plen2); 1106 else 1107 strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2); 1108 1109 if (noderef) { 1110 if (lstat(path1, &stb1) != 0) { 1111 if (!(Nflag || Pflag) || errno != ENOENT) { 1112 weprintf("%s", path1); 1113 return; 1114 } 1115 flags |= D_EMPTY1; 1116 memset(&stb1, 0, sizeof(stb1)); 1117 } 1118 1119 if (lstat(path2, &stb2) != 0) { 1120 if (!Nflag || errno != ENOENT) { 1121 weprintf("%s", path2); 1122 return; 1123 } 1124 flags |= D_EMPTY2; 1125 memset(&stb2, 0, sizeof(stb2)); 1126 stb2.st_mode = stb1.st_mode; 1127 } 1128 if (stb1.st_mode == 0) 1129 stb1.st_mode = stb2.st_mode; 1130 if (S_ISLNK(stb1.st_mode) || S_ISLNK(stb2.st_mode)) { 1131 if (S_ISLNK(stb1.st_mode) && S_ISLNK(stb2.st_mode)) { 1132 char buf1[PATH_MAX]; 1133 char buf2[PATH_MAX]; 1134 ssize_t len1; 1135 ssize_t len2; 1136 1137 len1 = readlink(path1, buf1, sizeof(buf1)); 1138 len2 = readlink(path2, buf2, sizeof(buf2)); 1139 if (len1 < 0 || len2 < 0) { 1140 perror("reading links"); 1141 return; 1142 } 1143 buf1[len1] = '\0'; 1144 buf2[len2] = '\0'; 1145 if (len1 != len2 || strncmp(buf1, buf2, len1) != 0) { 1146 printf( 1147 "Symbolic links %s and %s " 1148 "differ\n", 1149 path1, 1150 path2 1151 ); 1152 status |= 1; 1153 } 1154 return; 1155 } 1156 printf( 1157 "File %s is a %s while file %s is a %s\n", 1158 path1, 1159 S_ISLNK(stb1.st_mode) ? "symbolic link" 1160 : S_ISDIR(stb1.st_mode) ? "directory" 1161 : "file", 1162 path2, 1163 S_ISLNK(stb2.st_mode) ? "symbolic link" 1164 : S_ISDIR(stb2.st_mode) ? "directory" 1165 : "file" 1166 ); 1167 status |= 1; 1168 return; 1169 } 1170 } else { 1171 if (stat(path1, &stb1) != 0) { 1172 if (!(Nflag || Pflag) || errno != ENOENT) { 1173 weprintf("%s", path1); 1174 return; 1175 } 1176 flags |= D_EMPTY1; 1177 memset(&stb1, 0, sizeof(stb1)); 1178 } 1179 1180 if (stat(path2, &stb2) != 0) { 1181 if (!Nflag || errno != ENOENT) { 1182 weprintf("%s", path2); 1183 return; 1184 } 1185 flags |= D_EMPTY2; 1186 memset(&stb2, 0, sizeof(stb2)); 1187 stb2.st_mode = stb1.st_mode; 1188 } 1189 if (stb1.st_mode == 0) 1190 stb1.st_mode = stb2.st_mode; 1191 } 1192 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 1193 if (rflag) 1194 diffdir(path1, path2, flags); 1195 else 1196 printf("Common subdirectories: %s and %s\n", path1, path2); 1197 return; 1198 } 1199 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode)) 1200 dp->d_status = D_SKIPPED1; 1201 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode)) 1202 dp->d_status = D_SKIPPED2; 1203 else 1204 dp->d_status = diffreg(path1, path2, flags, 0); 1205 print_status(dp->d_status, path1, path2, ""); 1206 } 1207 1208 static int 1209 selectfile(const struct dirent *dp) 1210 { 1211 struct Excludes *excl; 1212 1213 if (dp->d_fileno == 0) 1214 return 0; 1215 1216 if (dp->d_name[0] == '.' 1217 && (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) 1218 return 0; 1219 1220 for (excl = excludes_list; excl != NULL; excl = excl->next) 1221 if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0) 1222 return 0; 1223 1224 return 1; 1225 } 1226 1227 void 1228 print_only(const char *path, size_t dirlen, const char *entry) 1229 { 1230 if (dirlen > 1) 1231 dirlen--; 1232 printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 1233 } 1234 1235 int 1236 diffreg(char *file1, char *file2, int flags, int capsicum) 1237 { 1238 FILE *f1, *f2; 1239 int i, rval; 1240 struct Pr *pr = NULL; 1241 1242 f1 = f2 = NULL; 1243 rval = D_SAME; 1244 anychange = 0; 1245 lastline = 0; 1246 lastmatchline = 0; 1247 1248 if (diff_format == D_SIDEBYSIDE) { 1249 if (flags & D_EXPANDTABS) { 1250 if (width > 3) 1251 hw = (width - 3) / 2; 1252 else 1253 hw = 0; 1254 } else if (width <= 3 || width <= tabsize) { 1255 hw = 0; 1256 } else { 1257 hw = (width - 3) / 2; 1258 while (hw > 0 && roundup(hw + 3, tabsize) + hw > width) 1259 hw--; 1260 if (width - (roundup(hw + 3, tabsize) + hw) < tabsize) 1261 width = roundup(hw + 3, tabsize) + hw; 1262 } 1263 lpad = (width - hw * 2 - 1) / 2; 1264 rpad = (width - hw * 2 - 1) - lpad; 1265 } 1266 1267 if (flags & D_IGNORECASE) 1268 chrtran = cup2low; 1269 else 1270 chrtran = clow2low; 1271 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode)) 1272 return S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2; 1273 if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0) 1274 goto closem; 1275 1276 if (flags & D_EMPTY1) 1277 f1 = fopen(ARUU_PATH_DEVNULL, "r"); 1278 else { 1279 if (!S_ISREG(stb1.st_mode)) { 1280 if ((f1 = opentemp(file1)) == NULL || fstat(fileno(f1), &stb1) == -1) { 1281 weprintf("%s", file1); 1282 rval = D_ERROR; 1283 status |= 2; 1284 goto closem; 1285 } 1286 } else if (strcmp(file1, "-") == 0) 1287 f1 = stdin; 1288 else 1289 f1 = fopen(file1, "r"); 1290 } 1291 if (f1 == NULL) { 1292 weprintf("%s", file1); 1293 rval = D_ERROR; 1294 status |= 2; 1295 goto closem; 1296 } 1297 1298 if (flags & D_EMPTY2) 1299 f2 = fopen(ARUU_PATH_DEVNULL, "r"); 1300 else { 1301 if (!S_ISREG(stb2.st_mode)) { 1302 if ((f2 = opentemp(file2)) == NULL || fstat(fileno(f2), &stb2) == -1) { 1303 weprintf("%s", file2); 1304 rval = D_ERROR; 1305 status |= 2; 1306 goto closem; 1307 } 1308 } else if (strcmp(file2, "-") == 0) 1309 f2 = stdin; 1310 else 1311 f2 = fopen(file2, "r"); 1312 } 1313 if (f2 == NULL) { 1314 weprintf("%s", file2); 1315 rval = D_ERROR; 1316 status |= 2; 1317 goto closem; 1318 } 1319 1320 if (lflag) 1321 pr = start_pr(file1, file2); 1322 1323 (void)capsicum; 1324 1325 switch (files_differ(f1, f2, flags)) { 1326 case 0: 1327 goto closem; 1328 case 1: 1329 break; 1330 default: 1331 rval = D_ERROR; 1332 status |= 2; 1333 goto closem; 1334 } 1335 1336 if (diff_format == D_BRIEF && ignore_pats == NULL 1337 && (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_SKIPBLANKLINES | D_STRIPCR)) 1338 == 0) { 1339 rval = D_DIFFER; 1340 status |= 1; 1341 goto closem; 1342 } 1343 if ((flags & D_FORCEASCII) != 0) { 1344 prepare(0, f1, stb1.st_size, flags); 1345 prepare(1, f2, stb2.st_size, flags); 1346 } else if ( 1347 !asciifile(f1) || !asciifile(f2) || !prepare(0, f1, stb1.st_size, flags) 1348 || !prepare(1, f2, stb2.st_size, flags) 1349 ) { 1350 rval = D_BINARY; 1351 status |= 1; 1352 goto closem; 1353 } 1354 if (len[0] > INT_MAX - 2) 1355 errno = EFBIG, enprintf(1, "%s", file1); 1356 if (len[1] > INT_MAX - 2) 1357 errno = EFBIG, enprintf(1, "%s", file2); 1358 1359 prune(); 1360 sort(sfile[0], slen[0]); 1361 sort(sfile[1], slen[1]); 1362 1363 member = (int *)file[1]; 1364 equiv(sfile[0], slen[0], sfile[1], slen[1], member); 1365 member = ereallocarray(member, slen[1] + 2, sizeof(*member)); 1366 1367 class = (int *)file[0]; 1368 unsort(sfile[0], slen[0], class); 1369 class = ereallocarray(class, slen[0] + 2, sizeof(*class)); 1370 1371 klist = ecalloc(slen[0] + 2, sizeof(*klist)); 1372 clen = 0; 1373 clistlen = 100; 1374 clist = ecalloc(clistlen, sizeof(*clist)); 1375 i = stone(class, slen[0], member, klist, flags); 1376 free(member); 1377 free(class); 1378 1379 J = ereallocarray(J, len[0] + 2, sizeof(*J)); 1380 unravel(klist[i]); 1381 free(clist); 1382 free(klist); 1383 1384 ixold = ereallocarray(ixold, len[0] + 2, sizeof(*ixold)); 1385 ixnew = ereallocarray(ixnew, len[1] + 2, sizeof(*ixnew)); 1386 check(f1, f2, flags); 1387 output(file1, f1, file2, f2, flags); 1388 1389 closem: 1390 if (pr != NULL) 1391 stop_pr(pr); 1392 if (anychange) { 1393 status |= 1; 1394 if (rval == D_SAME) 1395 rval = D_DIFFER; 1396 } 1397 if (f1 != NULL && f1 != stdin) 1398 fclose(f1); 1399 if (f2 != NULL && f2 != stdin) 1400 fclose(f2); 1401 1402 return rval; 1403 } 1404 1405 static int 1406 files_differ(FILE *f1, FILE *f2, int flags) 1407 { 1408 char buf1[BUFSIZ], buf2[BUFSIZ]; 1409 size_t i, j; 1410 1411 if ((flags & (D_EMPTY1 | D_EMPTY2)) || stb1.st_size != stb2.st_size 1412 || (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) 1413 return 1; 1414 1415 if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino) 1416 return 0; 1417 1418 for (;;) { 1419 i = fread(buf1, 1, sizeof(buf1), f1); 1420 j = fread(buf2, 1, sizeof(buf2), f2); 1421 if ((!i && ferror(f1)) || (!j && ferror(f2))) 1422 return -1; 1423 if (i != j) 1424 return 1; 1425 if (i == 0) 1426 return 0; 1427 if (memcmp(buf1, buf2, i) != 0) 1428 return 1; 1429 } 1430 } 1431 1432 static FILE * 1433 opentemp(const char *f) 1434 { 1435 char buf[BUFSIZ], tempfile[PATH_MAX]; 1436 ssize_t nread; 1437 int ifd, ofd; 1438 1439 if (strcmp(f, "-") == 0) 1440 ifd = STDIN_FILENO; 1441 else if ((ifd = open(f, O_RDONLY, 0644)) == -1) 1442 return NULL; 1443 1444 strlcpy(tempfile, ARUU_PATH_TMP "/diff.XXXXXXXX", sizeof(tempfile)); 1445 if ((ofd = mkstemp(tempfile)) == -1) { 1446 close(ifd); 1447 return NULL; 1448 } 1449 unlink(tempfile); 1450 while ((nread = read(ifd, buf, BUFSIZ)) > 0) { 1451 if (write(ofd, buf, nread) != nread) { 1452 close(ifd); 1453 close(ofd); 1454 return NULL; 1455 } 1456 } 1457 close(ifd); 1458 lseek(ofd, (off_t)0, SEEK_SET); 1459 return fdopen(ofd, "r"); 1460 } 1461 1462 static int 1463 prepare(int i, FILE *fd, size_t filesize, int flags) 1464 { 1465 struct Line *p; 1466 unsigned h; 1467 size_t sz, j = 0; 1468 enum Readhash r; 1469 1470 rewind(fd); 1471 sz = filesize / 25; 1472 if (sz < 100) 1473 sz = 100; 1474 1475 p = ecalloc(sz + 3, sizeof(*p)); 1476 while ((r = readhash(fd, flags, &h)) != RH_EOF) { 1477 if (r == RH_BINARY) 1478 return 0; 1479 if (j == SIZE_MAX) 1480 break; 1481 if (j == sz) { 1482 sz = sz * 3 / 2; 1483 p = ereallocarray(p, sz + 3, sizeof(*p)); 1484 } 1485 p[++j].value = h; 1486 } 1487 len[i] = j; 1488 file[i] = p; 1489 return 1; 1490 } 1491 1492 static void 1493 prune(void) 1494 { 1495 size_t i, j; 1496 1497 for (pref = 0; 1498 pref < len[0] && pref < len[1] && file[0][pref + 1].value == file[1][pref + 1].value; 1499 pref++) 1500 ; 1501 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref 1502 && file[0][len[0] - suff].value == file[1][len[1] - suff].value; 1503 suff++) 1504 ; 1505 for (j = 0; j < 2; j++) { 1506 sfile[j] = file[j] + pref; 1507 slen[j] = len[j] - pref - suff; 1508 for (i = 0; i <= slen[j]; i++) 1509 sfile[j][i].serial = i; 1510 } 1511 } 1512 1513 static void 1514 equiv(struct Line *a, int n, struct Line *b, int m, int *c) 1515 { 1516 int i, j; 1517 1518 i = j = 1; 1519 while (i <= n && j <= m) { 1520 if (a[i].value < b[j].value) 1521 a[i++].value = 0; 1522 else if (a[i].value == b[j].value) 1523 a[i++].value = j; 1524 else 1525 j++; 1526 } 1527 while (i <= n) 1528 a[i++].value = 0; 1529 b[m + 1].value = 0; 1530 j = 0; 1531 while (++j <= m) { 1532 c[j] = -b[j].serial; 1533 while (b[j + 1].value == b[j].value) { 1534 j++; 1535 c[j] = b[j].serial; 1536 } 1537 } 1538 c[j] = -1; 1539 } 1540 1541 static int 1542 stone(int *a, int n, int *b, int *c, int flags) 1543 { 1544 int i, k, y, j, l; 1545 int oldc, tc, oldl, sq; 1546 unsigned numtries, bound; 1547 1548 if (flags & D_MINIMAL) 1549 bound = UINT_MAX; 1550 else { 1551 sq = sqrt(n); 1552 bound = MAX(256, sq); 1553 } 1554 1555 k = 0; 1556 c[0] = newcand(0, 0, 0); 1557 for (i = 1; i <= n; i++) { 1558 j = a[i]; 1559 if (j == 0) 1560 continue; 1561 y = -b[j]; 1562 oldl = 0; 1563 oldc = c[0]; 1564 numtries = 0; 1565 do { 1566 if (y <= clist[oldc].y) 1567 continue; 1568 l = search(c, k, y); 1569 if (l != oldl + 1) 1570 oldc = c[l - 1]; 1571 if (l <= k) { 1572 if (clist[c[l]].y <= y) 1573 continue; 1574 tc = c[l]; 1575 c[l] = newcand(i, y, oldc); 1576 oldc = tc; 1577 oldl = l; 1578 numtries++; 1579 } else { 1580 c[l] = newcand(i, y, oldc); 1581 k++; 1582 break; 1583 } 1584 } while ((y = b[++j]) > 0 && numtries < bound); 1585 } 1586 return k; 1587 } 1588 1589 static int 1590 newcand(int x, int y, int pred) 1591 { 1592 struct Cand *q; 1593 1594 if (clen == clistlen) { 1595 clistlen = clistlen * 11 / 10; 1596 clist = ereallocarray(clist, clistlen, sizeof(*clist)); 1597 } 1598 q = clist + clen; 1599 q->x = x; 1600 q->y = y; 1601 q->pred = pred; 1602 return clen++; 1603 } 1604 1605 static int 1606 search(int *c, int k, int y) 1607 { 1608 int i, j, l, t; 1609 1610 if (clist[c[k]].y < y) 1611 return k + 1; 1612 i = 0; 1613 j = k + 1; 1614 for (;;) { 1615 l = (i + j) / 2; 1616 if (l <= i) 1617 break; 1618 t = clist[c[l]].y; 1619 if (t > y) 1620 j = l; 1621 else if (t < y) 1622 i = l; 1623 else 1624 return l; 1625 } 1626 return l + 1; 1627 } 1628 1629 static void 1630 unravel(int p) 1631 { 1632 struct Cand *q; 1633 size_t i; 1634 1635 for (i = 0; i <= len[0]; i++) 1636 J[i] = i <= pref ? i : i > len[0] - suff ? i + len[1] - len[0] : 0; 1637 for (q = clist + p; q->y != 0; q = clist + q->pred) 1638 J[q->x + pref] = q->y + pref; 1639 } 1640 1641 static void 1642 check(FILE *f1, FILE *f2, int flags) 1643 { 1644 int i, j, c, d; 1645 long ctold, ctnew; 1646 1647 rewind(f1); 1648 rewind(f2); 1649 j = 1; 1650 ixold[0] = ixnew[0] = 0; 1651 ctold = ctnew = 0; 1652 for (i = 1; i <= (int)len[0]; i++) { 1653 if (J[i] == 0) { 1654 ixold[i] = ctold += skipline(f1); 1655 continue; 1656 } 1657 while (j < J[i]) { 1658 ixnew[j] = ctnew += skipline(f2); 1659 j++; 1660 } 1661 if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_STRIPCR)) { 1662 for (;;) { 1663 c = getc(f1); 1664 d = getc(f2); 1665 if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) { 1666 if (c == EOF && isspace(d)) { 1667 ctnew++; 1668 break; 1669 } else if (isspace(c) && d == EOF) { 1670 ctold++; 1671 break; 1672 } 1673 } 1674 ctold++; 1675 ctnew++; 1676 if (flags & D_STRIPCR && (c == '\r' || d == '\r')) { 1677 if (c == '\r') { 1678 if ((c = getc(f1)) == '\n') 1679 ctold++; 1680 else 1681 ungetc(c, f1); 1682 } 1683 if (d == '\r') { 1684 if ((d = getc(f2)) == '\n') 1685 ctnew++; 1686 else 1687 ungetc(d, f2); 1688 } 1689 break; 1690 } 1691 if ((flags & D_FOLDBLANKS) && isspace(c) && isspace(d)) { 1692 do { 1693 if (c == '\n') 1694 break; 1695 ctold++; 1696 } while (isspace(c = getc(f1))); 1697 do { 1698 if (d == '\n') 1699 break; 1700 ctnew++; 1701 } while (isspace(d = getc(f2))); 1702 } else if (flags & D_IGNOREBLANKS) { 1703 while (isspace(c) && c != '\n') { 1704 c = getc(f1); 1705 ctold++; 1706 } 1707 while (isspace(d) && d != '\n') { 1708 d = getc(f2); 1709 ctnew++; 1710 } 1711 } 1712 if (chrtran(c) != chrtran(d)) { 1713 J[i] = 0; 1714 if (c != '\n' && c != EOF) 1715 ctold += skipline(f1); 1716 if (d != '\n' && c != EOF) 1717 ctnew += skipline(f2); 1718 break; 1719 } 1720 if (c == '\n' || c == EOF) 1721 break; 1722 } 1723 } else { 1724 for (;;) { 1725 ctold++; 1726 ctnew++; 1727 if ((c = getc(f1)) != (d = getc(f2))) { 1728 J[i] = 0; 1729 if (c != '\n' && c != EOF) 1730 ctold += skipline(f1); 1731 if (d != '\n' && c != EOF) 1732 ctnew += skipline(f2); 1733 break; 1734 } 1735 if (c == '\n' || c == EOF) 1736 break; 1737 } 1738 } 1739 ixold[i] = ctold; 1740 ixnew[j] = ctnew; 1741 j++; 1742 } 1743 for (; j <= (int)len[1]; j++) { 1744 ixnew[j] = ctnew += skipline(f2); 1745 } 1746 } 1747 1748 static void 1749 sort(struct Line *a, int n) 1750 { 1751 struct Line *ai, *aim, w; 1752 int j, m = 0, k; 1753 1754 if (n == 0) 1755 return; 1756 for (j = 1; j <= n; j *= 2) 1757 m = 2 * j - 1; 1758 for (m /= 2; m != 0; m /= 2) { 1759 k = n - m; 1760 for (j = 1; j <= k; j++) { 1761 for (ai = &a[j]; ai > a; ai -= m) { 1762 aim = &ai[m]; 1763 if (aim < ai) 1764 break; 1765 if (aim->value > ai[0].value || (aim->value == ai[0].value && aim->serial > ai[0].serial)) 1766 break; 1767 w.value = ai[0].value; 1768 ai[0].value = aim->value; 1769 aim->value = w.value; 1770 w.serial = ai[0].serial; 1771 ai[0].serial = aim->serial; 1772 aim->serial = w.serial; 1773 } 1774 } 1775 } 1776 } 1777 1778 static void 1779 unsort(struct Line *f, int l, int *b) 1780 { 1781 int *a, i; 1782 1783 a = ecalloc(l + 1, sizeof(*a)); 1784 for (i = 1; i <= l; i++) 1785 a[f[i].serial] = f[i].value; 1786 for (i = 1; i <= l; i++) 1787 b[i] = a[i]; 1788 free(a); 1789 } 1790 1791 static int 1792 skipline(FILE *f) 1793 { 1794 int i, c; 1795 1796 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++) 1797 continue; 1798 return i; 1799 } 1800 1801 static void 1802 output(char *file1, FILE *f1, char *file2, FILE *f2, int flags) 1803 { 1804 int i, j, m, i0, i1, j0, j1, nc; 1805 1806 rewind(f1); 1807 rewind(f2); 1808 m = len[0]; 1809 J[0] = 0; 1810 J[m + 1] = len[1] + 1; 1811 if (diff_format != D_EDIT) { 1812 for (i0 = 1; i0 <= m; i0 = i1 + 1) { 1813 while (i0 <= m && J[i0] == J[i0 - 1] + 1) { 1814 if (diff_format == D_SIDEBYSIDE && suppress_common != 1) { 1815 nc = fetch(ixold, i0, i0, f1, '\0', 1, flags); 1816 print_space(nc, hw - nc + lpad + 1 + rpad, flags); 1817 fetch(ixnew, J[i0], J[i0], f2, '\0', 0, flags); 1818 printf("\n"); 1819 } 1820 i0++; 1821 } 1822 j0 = J[i0 - 1] + 1; 1823 i1 = i0 - 1; 1824 while (i1 < m && J[i1 + 1] == 0) 1825 i1++; 1826 j1 = J[i1 + 1] - 1; 1827 J[i1] = j1; 1828 1829 if (diff_format == D_SIDEBYSIDE) { 1830 for (i = i0, j = j0; i <= i1 && j <= j1; i++, j++) 1831 change(file1, f1, file2, f2, i, i, j, j, &flags); 1832 while (i <= i1) { 1833 change(file1, f1, file2, f2, i, i, j + 1, j, &flags); 1834 i++; 1835 } 1836 while (j <= j1) { 1837 change(file1, f1, file2, f2, i + 1, i, j, j, &flags); 1838 j++; 1839 } 1840 } else 1841 change(file1, f1, file2, f2, i0, i1, j0, j1, &flags); 1842 } 1843 } else { 1844 for (i0 = m; i0 >= 1; i0 = i1 - 1) { 1845 while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0) 1846 i0--; 1847 j0 = J[i0 + 1] - 1; 1848 i1 = i0 + 1; 1849 while (i1 > 1 && J[i1 - 1] == 0) 1850 i1--; 1851 j1 = J[i1 - 1] + 1; 1852 J[i1] = j1; 1853 change(file1, f1, file2, f2, i1, i0, j1, j0, &flags); 1854 } 1855 } 1856 if (m == 0) 1857 change(file1, f1, file2, f2, 1, 0, 1, len[1], &flags); 1858 if (diff_format == D_IFDEF || diff_format == D_GFORMAT) { 1859 for (;;) { 1860 #define c i0 1861 if ((c = getc(f1)) == EOF) 1862 return; 1863 printf("%c", c); 1864 } 1865 #undef c 1866 } 1867 if (anychange != 0) { 1868 if (diff_format == D_CONTEXT) 1869 dump_context_vec(f1, f2, flags); 1870 else if (diff_format == D_UNIFIED) 1871 dump_unified_vec(f1, f2, flags); 1872 } 1873 } 1874 1875 static void 1876 range(int a, int b, const char *separator) 1877 { 1878 printf("%d", a > b ? b : a); 1879 if (a < b) 1880 printf("%s%d", separator, b); 1881 } 1882 1883 static void 1884 uni_range(int a, int b) 1885 { 1886 if (a < b) 1887 printf("%d,%d", a, b - a + 1); 1888 else if (a == b) 1889 printf("%d", b); 1890 else 1891 printf("%d,0", b); 1892 } 1893 1894 static char * 1895 preadline(int fd, size_t rlen, off_t off) 1896 { 1897 char *line; 1898 ssize_t nr; 1899 1900 line = emalloc(rlen + 1); 1901 if ((nr = pread(fd, line, rlen, off)) == -1) 1902 enprintf(2, "preadline"); 1903 if (nr > 0 && line[nr - 1] == '\n') 1904 nr--; 1905 line[nr] = '\0'; 1906 return line; 1907 } 1908 1909 static int 1910 ignoreline_pattern(char *line) 1911 { 1912 int ret; 1913 1914 ret = regexec(&ignore_re, line, 0, NULL, 0); 1915 return ret == 0; 1916 } 1917 1918 static int 1919 ignoreline(char *line, int skip_blanks) 1920 { 1921 if (skip_blanks && *line == '\0') 1922 return 1; 1923 if (ignore_pats != NULL && ignoreline_pattern(line)) 1924 return 1; 1925 return 0; 1926 } 1927 1928 static void 1929 change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d, int *pflags) 1930 { 1931 static size_t max_context = 64; 1932 long curpos; 1933 int i, nc; 1934 const char *walk; 1935 int skip_blanks, ignore; 1936 1937 skip_blanks = (*pflags & D_SKIPBLANKLINES); 1938 restart: 1939 if ((diff_format != D_IFDEF || diff_format == D_GFORMAT) && a > b && c > d) 1940 return; 1941 if (ignore_pats != NULL || skip_blanks) { 1942 char *line; 1943 if (a <= b) { 1944 for (i = a; i <= b; i++) { 1945 line = preadline(fileno(f1), ixold[i] - ixold[i - 1], ixold[i - 1]); 1946 ignore = ignoreline(line, skip_blanks); 1947 free(line); 1948 if (!ignore) 1949 goto proceed; 1950 } 1951 } 1952 if (a > b || c <= d) { 1953 for (i = c; i <= d; i++) { 1954 line = preadline(fileno(f2), ixnew[i] - ixnew[i - 1], ixnew[i - 1]); 1955 ignore = ignoreline(line, skip_blanks); 1956 free(line); 1957 if (!ignore) 1958 goto proceed; 1959 } 1960 } 1961 return; 1962 } 1963 proceed: 1964 if (*pflags & D_HEADER && diff_format != D_BRIEF) { 1965 printf("%s %s %s\n", diffargs, file1, file2); 1966 *pflags &= ~D_HEADER; 1967 } 1968 if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) { 1969 if (context_vec_start == NULL || context_vec_ptr == context_vec_end - 1) { 1970 ptrdiff_t offset = -1; 1971 1972 if (context_vec_start != NULL) 1973 offset = context_vec_ptr - context_vec_start; 1974 max_context <<= 1; 1975 context_vec_start = ereallocarray(context_vec_start, max_context, sizeof(*context_vec_start)); 1976 context_vec_end = context_vec_start + max_context; 1977 context_vec_ptr = context_vec_start + offset; 1978 } 1979 if (anychange == 0) { 1980 print_header(file1, file2); 1981 anychange = 1; 1982 } else if ( 1983 a > context_vec_ptr->b + (2 * diff_context) + 1 1984 && c > context_vec_ptr->d + (2 * diff_context) + 1 1985 ) { 1986 if (diff_format == D_CONTEXT) 1987 dump_context_vec(f1, f2, *pflags); 1988 else 1989 dump_unified_vec(f1, f2, *pflags); 1990 } 1991 context_vec_ptr++; 1992 context_vec_ptr->a = a; 1993 context_vec_ptr->b = b; 1994 context_vec_ptr->c = c; 1995 context_vec_ptr->d = d; 1996 return; 1997 } 1998 if (anychange == 0) 1999 anychange = 1; 2000 switch (diff_format) { 2001 case D_BRIEF: 2002 return; 2003 case D_NORMAL: 2004 case D_EDIT: 2005 range(a, b, ","); 2006 printf("%c", a > b ? 'a' : c > d ? 'd' : 'c'); 2007 if (diff_format == D_NORMAL) 2008 range(c, d, ","); 2009 printf("\n"); 2010 break; 2011 case D_REVERSE: 2012 printf("%c", a > b ? 'a' : c > d ? 'd' : 'c'); 2013 range(a, b, " "); 2014 printf("\n"); 2015 break; 2016 case D_NREVERSE: 2017 if (a > b) 2018 printf("a%d %d\n", b, d - c + 1); 2019 else { 2020 printf("d%d %d\n", a, b - a + 1); 2021 if (!(c > d)) 2022 printf("a%d %d\n", b, d - c + 1); 2023 } 2024 break; 2025 } 2026 if (diff_format == D_GFORMAT) { 2027 curpos = ftell(f1); 2028 nc = ixold[a > b ? b : a - 1] - curpos; 2029 for (i = 0; i < nc; i++) 2030 printf("%c", getc(f1)); 2031 for (walk = group_format; *walk != '\0'; walk++) { 2032 if (*walk == '%') { 2033 walk++; 2034 switch (*walk) { 2035 case '<': 2036 fetch(ixold, a, b, f1, '<', 1, *pflags); 2037 break; 2038 case '>': 2039 fetch(ixnew, c, d, f2, '>', 0, *pflags); 2040 break; 2041 default: 2042 printf("%%%c", *walk); 2043 break; 2044 } 2045 continue; 2046 } 2047 printf("%c", *walk); 2048 } 2049 } 2050 if (diff_format == D_SIDEBYSIDE) { 2051 if (color && a > b) 2052 printf("\033[%sm", add_code); 2053 else if (color && c > d) 2054 printf("\033[%sm", del_code); 2055 if (a > b) { 2056 print_space(0, hw + lpad, *pflags); 2057 } else { 2058 nc = fetch(ixold, a, b, f1, '\0', 1, *pflags); 2059 print_space(nc, hw - nc + lpad, *pflags); 2060 } 2061 if (color && a > b) 2062 printf("\033[%sm", add_code); 2063 else if (color && c > d) 2064 printf("\033[%sm", del_code); 2065 printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|')); 2066 if (color && c > d) 2067 printf("\033[m"); 2068 print_space(hw + lpad + 1, rpad, *pflags); 2069 fetch(ixnew, c, d, f2, '\0', 0, *pflags); 2070 printf("\n"); 2071 } 2072 if (diff_format == D_NORMAL || diff_format == D_IFDEF) { 2073 fetch(ixold, a, b, f1, '<', 1, *pflags); 2074 if (a <= b && c <= d && diff_format == D_NORMAL) 2075 printf("---\n"); 2076 } 2077 if (diff_format != D_GFORMAT && diff_format != D_SIDEBYSIDE) 2078 fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags); 2079 if (edoffset != 0 && diff_format == D_EDIT) { 2080 printf(".\n"); 2081 printf("%ds/.// \n", a + edoffset - 1); 2082 b = a + edoffset - 1; 2083 a = b + 1; 2084 c += edoffset; 2085 goto restart; 2086 } 2087 if ((diff_format == D_EDIT || diff_format == D_REVERSE) && c <= d) 2088 printf(".\n"); 2089 if (inifdef) { 2090 printf("#endif /* %s */\n", ifdefname); 2091 inifdef = 0; 2092 } 2093 } 2094 2095 static int 2096 fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags) 2097 { 2098 int i, j, c, lastc, col, nc, newcol; 2099 2100 edoffset = 0; 2101 nc = 0; 2102 col = 0; 2103 if ((diff_format == D_IFDEF) && oldfile) { 2104 long curpos = ftell(lb); 2105 nc = f[a > b ? b : a - 1] - curpos; 2106 for (i = 0; i < nc; i++) 2107 printf("%c", getc(lb)); 2108 } 2109 if (a > b) 2110 return 0; 2111 if (diff_format == D_IFDEF) { 2112 if (inifdef) { 2113 printf("#else /* %s%s */\n", oldfile == 1 ? "!" : "", ifdefname); 2114 } else { 2115 if (oldfile) 2116 printf("#ifndef %s\n", ifdefname); 2117 else 2118 printf("#ifdef %s\n", ifdefname); 2119 } 2120 inifdef = 1 + oldfile; 2121 } 2122 for (i = a; i <= b; i++) { 2123 fseek(lb, f[i - 1], SEEK_SET); 2124 nc = f[i] - f[i - 1]; 2125 if (diff_format == D_SIDEBYSIDE && hw < nc) 2126 nc = hw; 2127 if (diff_format != D_IFDEF && diff_format != D_GFORMAT && ch != '\0') { 2128 if (color && (ch == '>' || ch == '+')) 2129 printf("\033[%sm", add_code); 2130 else if (color && (ch == '<' || ch == '-')) 2131 printf("\033[%sm", del_code); 2132 printf("%c", ch); 2133 if (Tflag 2134 && (diff_format == D_NORMAL || diff_format == D_CONTEXT || diff_format == D_UNIFIED)) 2135 printf("\t"); 2136 else if (diff_format != D_UNIFIED) 2137 printf(" "); 2138 } 2139 col = j = 0; 2140 lastc = '\0'; 2141 while (j < nc && (hw == 0 || col < hw)) { 2142 c = getc(lb); 2143 if (flags & D_STRIPCR && c == '\r') { 2144 if ((c = getc(lb)) == '\n') 2145 j++; 2146 else { 2147 ungetc(c, lb); 2148 c = '\r'; 2149 } 2150 } 2151 if (c == EOF) { 2152 if (diff_format == D_EDIT || diff_format == D_REVERSE || diff_format == D_NREVERSE) 2153 weprintf("No newline at end of file"); 2154 else 2155 printf( 2156 "\n\\ No newline at end of " 2157 "file\n" 2158 ); 2159 return col; 2160 } 2161 if (c == '\t') { 2162 newcol = roundup(col + 1, tabsize); 2163 if ((flags & D_EXPANDTABS) == 0) { 2164 if (hw > 0 && newcol >= hw) 2165 return col; 2166 printf("\t"); 2167 } else { 2168 if (hw > 0 && newcol > hw) 2169 newcol = hw; 2170 printf("%*s", newcol - col, ""); 2171 } 2172 col = newcol; 2173 } else { 2174 if (diff_format == D_EDIT && j == 1 && c == '\n' && lastc == '.') { 2175 printf(".\n"); 2176 edoffset = i - a + 1; 2177 return edoffset; 2178 } 2179 if (diff_format != D_SIDEBYSIDE || c != '\n') { 2180 if (color && c == '\n') 2181 printf("\033[m%c", c); 2182 else 2183 printf("%c", c); 2184 col++; 2185 } 2186 } 2187 j++; 2188 lastc = c; 2189 } 2190 } 2191 if (color && diff_format == D_SIDEBYSIDE) 2192 printf("\033[m"); 2193 return col; 2194 } 2195 2196 static enum Readhash 2197 readhash(FILE *f, int flags, unsigned *hash) 2198 { 2199 int i, t, space; 2200 unsigned sum; 2201 2202 sum = 1; 2203 space = 0; 2204 for (i = 0;;) { 2205 switch (t = getc(f)) { 2206 case '\0': 2207 if ((flags & D_FORCEASCII) == 0) 2208 return RH_BINARY; 2209 goto hashchar; 2210 case '\r': 2211 if (flags & D_STRIPCR) { 2212 t = getc(f); 2213 if (t == '\n') 2214 break; 2215 ungetc(t, f); 2216 } 2217 /* FALLTHROUGH */ 2218 case '\t': 2219 case '\v': 2220 case '\f': 2221 case ' ': 2222 if ((flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) != 0) { 2223 space++; 2224 continue; 2225 } 2226 /* FALLTHROUGH */ 2227 default: 2228 hashchar: 2229 if (space && (flags & D_IGNOREBLANKS) == 0) { 2230 i++; 2231 space = 0; 2232 } 2233 sum = sum * 127 + chrtran(t); 2234 i++; 2235 continue; 2236 case EOF: 2237 if (i == 0) 2238 return RH_EOF; 2239 /* FALLTHROUGH */ 2240 case '\n': 2241 break; 2242 } 2243 break; 2244 } 2245 *hash = sum; 2246 return RH_OK; 2247 } 2248 2249 static int 2250 asciifile(FILE *f) 2251 { 2252 unsigned char buf[BUFSIZ]; 2253 size_t cnt; 2254 2255 if (f == NULL) 2256 return 1; 2257 2258 rewind(f); 2259 cnt = fread(buf, 1, sizeof(buf), f); 2260 return memchr(buf, '\0', cnt) == NULL; 2261 } 2262 2263 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre) - 1) == 0) 2264 2265 static char * 2266 match_function(const long *f, int pos, FILE *fp) 2267 { 2268 char buf[FUNCTION_CONTEXT_SIZE]; 2269 size_t nc; 2270 int last = lastline; 2271 const char *state = NULL; 2272 2273 lastline = pos; 2274 for (; pos > last; pos--) { 2275 fseek(fp, f[pos - 1], SEEK_SET); 2276 nc = f[pos] - f[pos - 1]; 2277 if (nc >= sizeof(buf)) 2278 nc = sizeof(buf) - 1; 2279 nc = fread(buf, 1, nc, fp); 2280 if (nc == 0) 2281 continue; 2282 buf[nc] = '\0'; 2283 buf[strcspn(buf, "\n")] = '\0'; 2284 if (most_recent_pat != NULL) { 2285 int ret = regexec(&most_recent_re, buf, 0, NULL, 0); 2286 if (ret != 0) 2287 continue; 2288 strlcpy(lastbuf, buf, sizeof(lastbuf)); 2289 lastmatchline = pos; 2290 return lastbuf; 2291 } else if ( 2292 isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$' || buf[0] == '-' || buf[0] == '+' 2293 ) { 2294 if (begins_with(buf, "private:")) { 2295 if (!state) 2296 state = " (private)"; 2297 } else if (begins_with(buf, "protected:")) { 2298 if (!state) 2299 state = " (protected)"; 2300 } else if (begins_with(buf, "public:")) { 2301 if (!state) 2302 state = " (public)"; 2303 } else { 2304 strlcpy(lastbuf, buf, sizeof(lastbuf)); 2305 if (state) 2306 strlcat(lastbuf, state, sizeof(lastbuf)); 2307 lastmatchline = pos; 2308 return lastbuf; 2309 } 2310 } 2311 } 2312 return lastmatchline > 0 ? lastbuf : NULL; 2313 } 2314 2315 static void 2316 dump_context_vec(FILE *f1, FILE *f2, int flags) 2317 { 2318 struct ContextVec *cvp = context_vec_start; 2319 int lowa, upb, lowc, upd, do_output; 2320 int a, b, c, d; 2321 char ch, *f; 2322 2323 if (context_vec_start > context_vec_ptr) 2324 return; 2325 2326 b = d = 0; 2327 lowa = MAX(1, cvp->a - diff_context); 2328 upb = MIN((int)len[0], context_vec_ptr->b + diff_context); 2329 lowc = MAX(1, cvp->c - diff_context); 2330 upd = MIN((int)len[1], context_vec_ptr->d + diff_context); 2331 2332 printf("***************"); 2333 if (flags & (D_PROTOTYPE | D_MATCHLAST)) { 2334 f = match_function(ixold, cvp->a - 1, f1); 2335 if (f != NULL) 2336 printf(" %s", f); 2337 } 2338 printf("\n*** "); 2339 range(lowa, upb, ","); 2340 printf(" ****\n"); 2341 2342 do_output = 0; 2343 for (; cvp <= context_vec_ptr; cvp++) 2344 if (cvp->a <= cvp->b) { 2345 cvp = context_vec_start; 2346 do_output++; 2347 break; 2348 } 2349 if (do_output) { 2350 while (cvp <= context_vec_ptr) { 2351 a = cvp->a; 2352 b = cvp->b; 2353 c = cvp->c; 2354 d = cvp->d; 2355 2356 if (a <= b && c <= d) 2357 ch = 'c'; 2358 else 2359 ch = (a <= b) ? 'd' : 'a'; 2360 2361 if (ch == 'a') 2362 fetch(ixold, lowa, b, f1, ' ', 0, flags); 2363 else { 2364 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags); 2365 fetch(ixold, a, b, f1, ch == 'c' ? '!' : '-', 0, flags); 2366 } 2367 lowa = b + 1; 2368 cvp++; 2369 } 2370 fetch(ixold, b + 1, upb, f1, ' ', 0, flags); 2371 } 2372 2373 printf("--- "); 2374 range(lowc, upd, ","); 2375 printf(" ----\n"); 2376 2377 do_output = 0; 2378 for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++) 2379 if (cvp->c <= cvp->d) { 2380 cvp = context_vec_start; 2381 do_output++; 2382 break; 2383 } 2384 if (do_output) { 2385 while (cvp <= context_vec_ptr) { 2386 a = cvp->a; 2387 b = cvp->b; 2388 c = cvp->c; 2389 d = cvp->d; 2390 2391 if (a <= b && c <= d) 2392 ch = 'c'; 2393 else 2394 ch = (a <= b) ? 'd' : 'a'; 2395 2396 if (ch == 'd') 2397 fetch(ixnew, lowc, d, f2, ' ', 0, flags); 2398 else { 2399 fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags); 2400 fetch(ixnew, c, d, f2, ch == 'c' ? '!' : '+', 0, flags); 2401 } 2402 lowc = d + 1; 2403 cvp++; 2404 } 2405 fetch(ixnew, d + 1, upd, f2, ' ', 0, flags); 2406 } 2407 context_vec_ptr = context_vec_start - 1; 2408 } 2409 2410 static void 2411 dump_unified_vec(FILE *f1, FILE *f2, int flags) 2412 { 2413 struct ContextVec *cvp = context_vec_start; 2414 int lowa, upb, lowc, upd; 2415 int a, b, c, d; 2416 char ch, *f; 2417 2418 if (context_vec_start > context_vec_ptr) 2419 return; 2420 2421 b = d = 0; 2422 lowa = MAX(1, cvp->a - diff_context); 2423 upb = MIN((int)len[0], context_vec_ptr->b + diff_context); 2424 lowc = MAX(1, cvp->c - diff_context); 2425 upd = MIN((int)len[1], context_vec_ptr->d + diff_context); 2426 2427 printf("@@ -"); 2428 uni_range(lowa, upb); 2429 printf(" +"); 2430 uni_range(lowc, upd); 2431 printf(" @@"); 2432 if (flags & (D_PROTOTYPE | D_MATCHLAST)) { 2433 f = match_function(ixold, cvp->a - 1, f1); 2434 if (f != NULL) 2435 printf(" %s", f); 2436 } 2437 printf("\n"); 2438 2439 for (; cvp <= context_vec_ptr; cvp++) { 2440 a = cvp->a; 2441 b = cvp->b; 2442 c = cvp->c; 2443 d = cvp->d; 2444 2445 if (a <= b && c <= d) 2446 ch = 'c'; 2447 else 2448 ch = (a <= b) ? 'd' : 'a'; 2449 2450 switch (ch) { 2451 case 'c': 2452 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags); 2453 fetch(ixold, a, b, f1, '-', 0, flags); 2454 fetch(ixnew, c, d, f2, '+', 0, flags); 2455 break; 2456 case 'd': 2457 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags); 2458 fetch(ixold, a, b, f1, '-', 0, flags); 2459 break; 2460 case 'a': 2461 fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags); 2462 fetch(ixnew, c, d, f2, '+', 0, flags); 2463 break; 2464 } 2465 lowa = b + 1; 2466 lowc = d + 1; 2467 } 2468 fetch(ixnew, d + 1, upd, f2, ' ', 0, flags); 2469 context_vec_ptr = context_vec_start - 1; 2470 } 2471 2472 static void 2473 print_header(const char *file1, const char *file2) 2474 { 2475 const char *time_format; 2476 char buf[256]; 2477 struct tm tm1, tm2, *tm_ptr1, *tm_ptr2; 2478 int nsec1 = stb1.st_mtim.tv_nsec; 2479 int nsec2 = stb2.st_mtim.tv_nsec; 2480 2481 time_format = "%Y-%m-%d %H:%M:%S"; 2482 if (cflag) 2483 time_format = "%c"; 2484 tm_ptr1 = localtime_r(&stb1.st_mtime, &tm1); 2485 tm_ptr2 = localtime_r(&stb2.st_mtime, &tm2); 2486 if (label[0] != NULL) 2487 printf("%s %s\n", diff_format == D_CONTEXT ? "***" : "---", label[0]); 2488 else { 2489 strftime(buf, sizeof(buf), time_format, tm_ptr1); 2490 printf("%s %s\t%s", diff_format == D_CONTEXT ? "***" : "---", file1, buf); 2491 if (!cflag) { 2492 strftime(buf, sizeof(buf), "%z", tm_ptr1); 2493 printf(".%.9d %s", nsec1, buf); 2494 } 2495 printf("\n"); 2496 } 2497 if (label[1] != NULL) 2498 printf("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++", label[1]); 2499 else { 2500 strftime(buf, sizeof(buf), time_format, tm_ptr2); 2501 printf("%s %s\t%s", diff_format == D_CONTEXT ? "---" : "+++", file2, buf); 2502 if (!cflag) { 2503 strftime(buf, sizeof(buf), "%z", tm_ptr2); 2504 printf(".%.9d %s", nsec2, buf); 2505 } 2506 printf("\n"); 2507 } 2508 } 2509 2510 static void 2511 print_space(int nc, int n, int flags) 2512 { 2513 int col, newcol, tabstop; 2514 2515 col = nc; 2516 newcol = nc + n; 2517 if ((flags & D_EXPANDTABS) == 0) { 2518 while ((tabstop = roundup(col + 1, tabsize)) <= newcol) { 2519 printf("\t"); 2520 col = tabstop; 2521 } 2522 } 2523 printf("%*s", newcol - col, ""); 2524 }