1 /* $OpenBSD: diff3prog.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */ 2 3 /* 4 * Copyright (C) Caldera International Inc. 2001-2002. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code and documentation must retain the above 11 * copyright notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed or owned by Caldera 18 * International, Inc. 19 * 4. Neither the name of Caldera International, Inc. nor the names of other 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA 24 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, 28 * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 /*- 37 * Copyright (c) 1991, 1993 38 * The Regents of the University of California. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)diff3.c 8.1 (Berkeley) 6/6/93 65 */ 66 67 #if 0 68 #ifndef lint 69 static char sccsid[] = "@(#)diff3.c 8.1 (Berkeley) 6/6/93"; 70 #endif 71 #endif /* not lint */ 72 #include <sys/cdefs.h> 73 __FBSDID("$FreeBSD$"); 74 75 #include <sys/capsicum.h> 76 #include <sys/procdesc.h> 77 #include <sys/types.h> 78 #include <sys/event.h> 79 #include <sys/wait.h> 80 81 #include <capsicum_helpers.h> 82 #include <ctype.h> 83 #include <err.h> 84 #include <getopt.h> 85 #include <stdio.h> 86 #include <stdlib.h> 87 #include <limits.h> 88 #include <inttypes.h> 89 #include <string.h> 90 #include <unistd.h> 91 92 93 /* 94 * "from" is first in range of changed lines; "to" is last+1 95 * from=to=line after point of insertion for added lines. 96 */ 97 struct range { 98 int from; 99 int to; 100 }; 101 102 struct diff { 103 struct range old; 104 struct range new; 105 }; 106 107 static size_t szchanges; 108 109 static struct diff *d13; 110 static struct diff *d23; 111 /* 112 * "de" is used to gather editing scripts. These are later spewed out in 113 * reverse order. Its first element must be all zero, the "old" and "new" 114 * components of "de" contain line positions. Array overlap indicates which 115 * sections in "de" correspond to lines that are different in all three files. 116 */ 117 static struct diff *de; 118 static char *overlap; 119 static int overlapcnt; 120 static FILE *fp[3]; 121 static int cline[3]; /* # of the last-read line in each file (0-2) */ 122 /* 123 * The latest known correspondence between line numbers of the 3 files 124 * is stored in last[1-3]; 125 */ 126 static int last[4]; 127 static int Aflag, eflag, iflag, mflag, Tflag; 128 static int oflag; /* indicates whether to mark overlaps (-E or -X) */ 129 static int strip_cr; 130 static char *f1mark, *f2mark, *f3mark; 131 static const char *oldmark = "<<<<<<<"; 132 static const char *newmark = ">>>>>>>"; 133 134 static bool duplicate(struct range *, struct range *); 135 static int edit(struct diff *, bool, int); 136 static char *getchange(FILE *); 137 static char *get_line(FILE *, size_t *); 138 static int readin(int fd, struct diff **); 139 static int skip(int, int, const char *); 140 static void change(int, struct range *, bool); 141 static void keep(int, struct range *); 142 static void merge(int, int); 143 static void prange(struct range *, bool); 144 static void repos(int); 145 static void edscript(int) __dead2; 146 static void increase(void); 147 static void usage(void) __dead2; 148 static void printrange(FILE *, struct range *); 149 150 enum { 151 DIFFPROG_OPT, 152 STRIPCR_OPT, 153 }; 154 155 #define DIFF_PATH "/usr/bin/diff" 156 157 #define OPTIONS "3aAeEiL:mTxX" 158 static struct option longopts[] = { 159 { "ed", no_argument, NULL, 'e' }, 160 { "show-overlap", no_argument, NULL, 'E' }, 161 { "overlap-only", no_argument, NULL, 'x' }, 162 { "initial-tab", no_argument, NULL, 'T' }, 163 { "text", no_argument, NULL, 'a' }, 164 { "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT }, 165 { "show-all", no_argument, NULL, 'A' }, 166 { "easy-only", no_argument, NULL, '3' }, 167 { "merge", no_argument, NULL, 'm' }, 168 { "label", required_argument, NULL, 'L' }, 169 { "diff-program", required_argument, NULL, DIFFPROG_OPT }, 170 }; 171 172 static void 173 usage(void) 174 { 175 fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L label1] [-L label2] " 176 "[-L label3] file1 file2 file3\n"); 177 exit(2); 178 } 179 180 static int 181 readin(int fd, struct diff **dd) 182 { 183 int a, b, c, d; 184 size_t i; 185 char kind, *p; 186 FILE *f; 187 188 f = fdopen(fd, "r"); 189 if (f == NULL) 190 err(2, "fdopen"); 191 for (i = 0; (p = getchange(f)); i++) { 192 if (i >= szchanges - 1) 193 increase(); 194 a = b = (int)strtoimax(p, &p, 10); 195 if (*p == ',') { 196 p++; 197 b = (int)strtoimax(p, &p, 10); 198 } 199 kind = *p++; 200 c = d = (int)strtoimax(p, &p, 10); 201 if (*p == ',') { 202 p++; 203 d = (int)strtoimax(p, &p, 10); 204 } 205 if (kind == 'a') 206 a++; 207 if (kind == 'd') 208 c++; 209 b++; 210 d++; 211 (*dd)[i].old.from = a; 212 (*dd)[i].old.to = b; 213 (*dd)[i].new.from = c; 214 (*dd)[i].new.to = d; 215 } 216 if (i) { 217 (*dd)[i].old.from = (*dd)[i - 1].old.to; 218 (*dd)[i].new.from = (*dd)[i - 1].new.to; 219 } 220 fclose(f); 221 return (i); 222 } 223 224 static int 225 diffexec(const char *diffprog, char **diffargv, int fd[]) 226 { 227 int pd; 228 229 switch (pdfork(&pd, PD_CLOEXEC)) { 230 case 0: 231 close(fd[0]); 232 if (dup2(fd[1], STDOUT_FILENO) == -1) 233 err(2, "child could not duplicate descriptor"); 234 close(fd[1]); 235 execvp(diffprog, diffargv); 236 err(2, "could not execute diff: %s", diffprog); 237 break; 238 case -1: 239 err(2, "could not fork"); 240 break; 241 } 242 close(fd[1]); 243 return (pd); 244 } 245 246 static char * 247 getchange(FILE *b) 248 { 249 char *line; 250 251 while ((line = get_line(b, NULL))) { 252 if (isdigit((unsigned char)line[0])) 253 return (line); 254 } 255 return (NULL); 256 } 257 258 259 static char * 260 get_line(FILE *b, size_t *n) 261 { 262 ssize_t len; 263 static char *buf = NULL; 264 static size_t bufsize = 0; 265 266 if ((len = getline(&buf, &bufsize, b)) < 0) 267 return (NULL); 268 269 if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) { 270 buf[len - 2] = '\n'; 271 buf[len - 1] = '\0'; 272 len--; 273 } 274 275 if (n != NULL) 276 *n = len; 277 278 return (buf); 279 } 280 281 static void 282 merge(int m1, int m2) 283 { 284 struct diff *d1, *d2, *d3; 285 int j, t1, t2; 286 bool dup = false; 287 288 d1 = d13; 289 d2 = d23; 290 j = 0; 291 292 while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) { 293 /* first file is different from the others */ 294 if (!t2 || (t1 && d1->new.to < d2->new.from)) { 295 /* stuff peculiar to 1st file */ 296 if (eflag == 0) { 297 printf("====1\n"); 298 change(1, &d1->old, false); 299 keep(2, &d1->new); 300 change(3, &d1->new, false); 301 } 302 d1++; 303 continue; 304 } 305 /* second file is different from others */ 306 if (!t1 || (t2 && d2->new.to < d1->new.from)) { 307 if (eflag == 0) { 308 printf("====2\n"); 309 keep(1, &d2->new); 310 change(3, &d2->new, false); 311 change(2, &d2->old, false); 312 } 313 d2++; 314 continue; 315 } 316 /* 317 * Merge overlapping changes in first file 318 * this happens after extension (see below). 319 */ 320 if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) { 321 d1[1].old.from = d1->old.from; 322 d1[1].new.from = d1->new.from; 323 d1++; 324 continue; 325 } 326 327 /* merge overlapping changes in second */ 328 if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) { 329 d2[1].old.from = d2->old.from; 330 d2[1].new.from = d2->new.from; 331 d2++; 332 continue; 333 } 334 /* stuff peculiar to third file or different in all */ 335 if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) { 336 dup = duplicate(&d1->old, &d2->old); 337 /* 338 * dup = 0 means all files differ 339 * dup = 1 means files 1 and 2 identical 340 */ 341 if (eflag == 0) { 342 printf("====%s\n", dup ? "3" : ""); 343 change(1, &d1->old, dup); 344 change(2, &d2->old, false); 345 d3 = d1->old.to > d1->old.from ? d1 : d2; 346 change(3, &d3->new, false); 347 } else 348 j = edit(d1, dup, j); 349 d1++; 350 d2++; 351 continue; 352 } 353 /* 354 * Overlapping changes from file 1 and 2; extend changes 355 * appropriately to make them coincide. 356 */ 357 if (d1->new.from < d2->new.from) { 358 d2->old.from -= d2->new.from - d1->new.from; 359 d2->new.from = d1->new.from; 360 } else if (d2->new.from < d1->new.from) { 361 d1->old.from -= d1->new.from - d2->new.from; 362 d1->new.from = d2->new.from; 363 } 364 if (d1->new.to > d2->new.to) { 365 d2->old.to += d1->new.to - d2->new.to; 366 d2->new.to = d1->new.to; 367 } else if (d2->new.to > d1->new.to) { 368 d1->old.to += d2->new.to - d1->new.to; 369 d1->new.to = d2->new.to; 370 } 371 } 372 if (eflag) 373 edscript(j); 374 } 375 376 /* 377 * The range of lines rold.from thru rold.to in file i is to be changed. 378 * It is to be printed only if it does not duplicate something to be 379 * printed later. 380 */ 381 static void 382 change(int i, struct range *rold, bool dup) 383 { 384 385 printf("%d:", i); 386 last[i] = rold->to; 387 prange(rold, false); 388 if (dup) 389 return; 390 i--; 391 skip(i, rold->from, NULL); 392 skip(i, rold->to, " "); 393 } 394 395 /* 396 * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or 397 * n1. 398 */ 399 static void 400 prange(struct range *rold, bool delete) 401 { 402 403 if (rold->to <= rold->from) 404 printf("%da\n", rold->from - 1); 405 else { 406 printf("%d", rold->from); 407 if (rold->to > rold->from + 1) 408 printf(",%d", rold->to - 1); 409 if (delete) 410 printf("d\n"); 411 else 412 printf("c\n"); 413 } 414 } 415 416 /* 417 * No difference was reported by diff between file 1 (or 2) and file 3, 418 * and an artificial dummy difference (trange) must be ginned up to 419 * correspond to the change reported in the other file. 420 */ 421 static void 422 keep(int i, struct range *rnew) 423 { 424 int delta; 425 struct range trange; 426 427 delta = last[3] - last[i]; 428 trange.from = rnew->from - delta; 429 trange.to = rnew->to - delta; 430 change(i, &trange, true); 431 } 432 433 /* 434 * skip to just before line number from in file "i". If "pr" is non-NULL, 435 * print all skipped stuff with string pr as a prefix. 436 */ 437 static int 438 skip(int i, int from, const char *pr) 439 { 440 size_t j, n; 441 char *line; 442 443 for (n = 0; cline[i] < from - 1; n += j) { 444 if ((line = get_line(fp[i], &j)) == NULL) 445 errx(EXIT_FAILURE, "logic error"); 446 if (pr != NULL) 447 printf("%s%s", Tflag == 1 ? "\t" : pr, line); 448 cline[i]++; 449 } 450 return ((int) n); 451 } 452 453 /* 454 * Return 1 or 0 according as the old range (in file 1) contains exactly 455 * the same data as the new range (in file 2). 456 */ 457 static bool 458 duplicate(struct range *r1, struct range *r2) 459 { 460 int c, d; 461 int nchar; 462 int nline; 463 464 if (r1->to-r1->from != r2->to-r2->from) 465 return (0); 466 skip(0, r1->from, NULL); 467 skip(1, r2->from, NULL); 468 nchar = 0; 469 for (nline = 0; nline < r1->to - r1->from; nline++) { 470 do { 471 c = getc(fp[0]); 472 d = getc(fp[1]); 473 if (c == -1 && d == -1) 474 break; 475 if (c == -1 || d == -1) 476 errx(EXIT_FAILURE, "logic error"); 477 nchar++; 478 if (c != d) { 479 repos(nchar); 480 return (0); 481 } 482 } while (c != '\n'); 483 } 484 repos(nchar); 485 return (1); 486 } 487 488 static void 489 repos(int nchar) 490 { 491 int i; 492 493 for (i = 0; i < 2; i++) 494 (void)fseek(fp[i], (long)-nchar, SEEK_CUR); 495 } 496 497 /* 498 * collect an editing script for later regurgitation 499 */ 500 static int 501 edit(struct diff *diff, bool dup, int j) 502 { 503 504 if (((dup + 1) & eflag) == 0) 505 return (j); 506 j++; 507 overlap[j] = !dup; 508 if (!dup) 509 overlapcnt++; 510 de[j].old.from = diff->old.from; 511 de[j].old.to = diff->old.to; 512 de[j].new.from = diff->new.from; 513 de[j].new.to = diff->new.to; 514 return (j); 515 } 516 517 static void 518 printrange(FILE *p, struct range *r) 519 { 520 char *line = NULL; 521 size_t len = 0; 522 int i = 1; 523 ssize_t rlen = 0; 524 525 /* We haven't been asked to print anything */ 526 if (r->from == r->to) 527 return; 528 529 if (r->from > r->to) 530 errx(EXIT_FAILURE, "invalid print range"); 531 532 /* 533 * XXX-THJ: We read through all of the file for each range printed. 534 * This duplicates work and will probably impact performance on large 535 * files with lots of ranges. 536 */ 537 fseek(p, 0L, SEEK_SET); 538 while ((rlen = getline(&line, &len, p)) > 0) { 539 if (i >= r->from) 540 printf("%s", line); 541 if (++i > r->to - 1) 542 break; 543 } 544 free(line); 545 } 546 547 /* regurgitate */ 548 static void 549 edscript(int n) 550 { 551 bool delete; 552 553 for (; n > 0; n--) { 554 delete = (de[n].new.from == de[n].new.to); 555 if (!oflag || !overlap[n]) { 556 prange(&de[n].old, delete); 557 } else { 558 printf("%da\n", de[n].old.to - 1); 559 printf("=======\n"); 560 } 561 printrange(fp[2], &de[n].new); 562 if (!oflag || !overlap[n]) { 563 if (!delete) 564 printf(".\n"); 565 } else { 566 printf("%s %s\n.\n", newmark, f3mark); 567 printf("%da\n%s %s\n.\n", de[n].old.from - 1, 568 oldmark, f1mark); 569 } 570 } 571 if (iflag) 572 printf("w\nq\n"); 573 574 exit(eflag == 0 ? overlapcnt : 0); 575 } 576 577 static void 578 increase(void) 579 { 580 struct diff *p; 581 char *q; 582 size_t newsz, incr; 583 584 /* are the memset(3) calls needed? */ 585 newsz = szchanges == 0 ? 64 : 2 * szchanges; 586 incr = newsz - szchanges; 587 588 p = reallocarray(d13, newsz, sizeof(struct diff)); 589 if (p == NULL) 590 err(1, NULL); 591 memset(p + szchanges, 0, incr * sizeof(struct diff)); 592 d13 = p; 593 p = reallocarray(d23, newsz, sizeof(struct diff)); 594 if (p == NULL) 595 err(1, NULL); 596 memset(p + szchanges, 0, incr * sizeof(struct diff)); 597 d23 = p; 598 p = reallocarray(de, newsz, sizeof(struct diff)); 599 if (p == NULL) 600 err(1, NULL); 601 memset(p + szchanges, 0, incr * sizeof(struct diff)); 602 de = p; 603 q = reallocarray(overlap, newsz, sizeof(char)); 604 if (q == NULL) 605 err(1, NULL); 606 memset(q + szchanges, 0, incr * sizeof(char)); 607 overlap = q; 608 szchanges = newsz; 609 } 610 611 612 int 613 main(int argc, char **argv) 614 { 615 int ch, nblabels, status, m, n, kq, nke, nleft, i; 616 char *labels[] = { NULL, NULL, NULL }; 617 const char *diffprog = DIFF_PATH; 618 char *file1, *file2, *file3; 619 char *diffargv[7]; 620 int diffargc = 0; 621 int fd13[2], fd23[2]; 622 int pd13, pd23; 623 cap_rights_t rights_ro; 624 struct kevent *e; 625 626 nblabels = 0; 627 eflag = 0; 628 oflag = 0; 629 diffargv[diffargc++] = __DECONST(char *, diffprog); 630 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 631 switch (ch) { 632 case '3': 633 eflag = 2; 634 break; 635 case 'a': 636 diffargv[diffargc++] = __DECONST(char *, "-a"); 637 break; 638 case 'A': 639 Aflag = 1; 640 break; 641 case 'e': 642 eflag = 3; 643 break; 644 case 'E': 645 eflag = 3; 646 oflag = 1; 647 break; 648 case 'i': 649 iflag = 1; 650 break; 651 case 'L': 652 oflag = 1; 653 if (nblabels >= 3) 654 errx(2, "too many file label options"); 655 labels[nblabels++] = optarg; 656 break; 657 case 'm': 658 Aflag = 1; 659 oflag = 1; 660 mflag = 1; 661 break; 662 case 'T': 663 Tflag = 1; 664 break; 665 case 'x': 666 eflag = 1; 667 break; 668 case 'X': 669 oflag = 1; 670 eflag = 1; 671 break; 672 case DIFFPROG_OPT: 673 diffprog = optarg; 674 break; 675 case STRIPCR_OPT: 676 strip_cr = 1; 677 diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr"); 678 break; 679 } 680 } 681 argc -= optind; 682 argv += optind; 683 684 if (Aflag) { 685 eflag = 3; 686 oflag = 1; 687 } 688 689 if (argc != 3) 690 usage(); 691 692 if (caph_limit_stdio() == -1) 693 err(2, "unable to limit stdio"); 694 695 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK); 696 697 kq = kqueue(); 698 if (kq == -1) 699 err(2, "kqueue"); 700 701 e = malloc(2 * sizeof(struct kevent)); 702 if (e == NULL) 703 err(2, "malloc"); 704 705 /* TODO stdio */ 706 file1 = argv[0]; 707 file2 = argv[1]; 708 file3 = argv[2]; 709 710 if (oflag) { 711 asprintf(&f1mark, "%s", 712 labels[0] != NULL ? labels[0] : file1); 713 if (f1mark == NULL) 714 err(2, "asprintf"); 715 asprintf(&f2mark, "%s", 716 labels[1] != NULL ? labels[1] : file2); 717 if (f2mark == NULL) 718 err(2, "asprintf"); 719 asprintf(&f3mark, "%s", 720 labels[2] != NULL ? labels[2] : file3); 721 if (f3mark == NULL) 722 err(2, "asprintf"); 723 } 724 fp[0] = fopen(file1, "r"); 725 if (fp[0] == NULL) 726 err(2, "Can't open %s", file1); 727 if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0) 728 err(2, "unable to limit rights on: %s", file1); 729 730 fp[1] = fopen(file2, "r"); 731 if (fp[1] == NULL) 732 err(2, "Can't open %s", file2); 733 if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0) 734 err(2, "unable to limit rights on: %s", file2); 735 736 fp[2] = fopen(file3, "r"); 737 if (fp[2] == NULL) 738 err(2, "Can't open %s", file3); 739 if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0) 740 err(2, "unable to limit rights on: %s", file3); 741 742 if (pipe(fd13)) 743 err(2, "pipe"); 744 if (pipe(fd23)) 745 err(2, "pipe"); 746 747 diffargv[diffargc] = file1; 748 diffargv[diffargc + 1] = file3; 749 diffargv[diffargc + 2] = NULL; 750 751 nleft = 0; 752 pd13 = diffexec(diffprog, diffargv, fd13); 753 EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL); 754 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) 755 err(2, "kevent1"); 756 nleft++; 757 758 diffargv[diffargc] = file2; 759 pd23 = diffexec(diffprog, diffargv, fd23); 760 EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL); 761 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) 762 err(2, "kevent2"); 763 nleft++; 764 765 caph_cache_catpages(); 766 if (caph_enter() < 0) 767 err(2, "unable to enter capability mode"); 768 769 /* parse diffs */ 770 increase(); 771 m = readin(fd13[0], &d13); 772 n = readin(fd23[0], &d23); 773 774 /* waitpid cooked over pdforks */ 775 while (nleft > 0) { 776 nke = kevent(kq, NULL, 0, e, nleft, NULL); 777 if (nke == -1) 778 err(2, "kevent"); 779 for (i = 0; i < nke; i++) { 780 status = e[i].data; 781 if (WIFEXITED(status) && WEXITSTATUS(status) >= 2) 782 errx(2, "diff exited abnormally"); 783 else if (WIFSIGNALED(status)) 784 errx(2, "diff killed by signal %d", 785 WTERMSIG(status)); 786 } 787 nleft -= nke; 788 } 789 merge(m, n); 790 791 return (EXIT_SUCCESS); 792 } 793