xref: /freebsd-src/usr.bin/diff3/diff3.c (revision 9520071f7e2e3eb910dfe08a1f46e50a96c403c4)
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 "new" component
114  * of "de" contains line positions or byte positions depending on when you
115  * look (!?).  Array overlap indicates which sections in "de" correspond to
116  * lines that are different in all three files.
117  */
118 static struct diff *de;
119 static char *overlap;
120 static int  overlapcnt;
121 static FILE *fp[3];
122 static int cline[3];		/* # of the last-read line in each file (0-2) */
123 /*
124  * The latest known correspondence between line numbers of the 3 files
125  * is stored in last[1-3];
126  */
127 static int last[4];
128 static int Aflag, eflag, iflag, mflag, Tflag;
129 static int oflag;		/* indicates whether to mark overlaps (-E or -X)*/
130 static int strip_cr;
131 static char *f1mark, *f2mark, *f3mark;
132 
133 static bool duplicate(struct range *, struct range *);
134 static int edit(struct diff *, bool, int);
135 static char *getchange(FILE *);
136 static char *get_line(FILE *, size_t *);
137 static int readin(int fd, struct diff **);
138 static int skip(int, int, const char *);
139 static void change(int, struct range *, bool);
140 static void keep(int, struct range *);
141 static void merge(int, int);
142 static void prange(struct range *);
143 static void repos(int);
144 static void edscript(int) __dead2;
145 static void increase(void);
146 static void usage(void) __dead2;
147 
148 enum {
149 	DIFFPROG_OPT,
150 	STRIPCR_OPT,
151 };
152 
153 #define DIFF_PATH "/usr/bin/diff"
154 
155 #define OPTIONS "3aAeEiL:mTxX"
156 static struct option longopts[] = {
157 	{ "ed",			no_argument,		NULL,	'e' },
158 	{ "show-overlap",	no_argument,		NULL,	'E' },
159 	{ "overlap-only",	no_argument,		NULL,	'x' },
160 	{ "initial-tab",	no_argument,		NULL,	'T' },
161 	{ "text",		no_argument,		NULL,	'a' },
162 	{ "strip-trailing-cr",	no_argument,		NULL,	STRIPCR_OPT },
163 	{ "show-all",		no_argument,		NULL,	'A' },
164 	{ "easy-only",		no_argument,		NULL,	'3' },
165 	{ "merge",		no_argument,		NULL,	'm' },
166 	{ "label",		required_argument,	NULL,	'L' },
167 	{ "diff-program",	required_argument,	NULL,	DIFFPROG_OPT },
168 };
169 
170 static void
171 usage(void)
172 {
173 	fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L lable1] [-L label2] "
174 	    "[ -L label3] file1 file2 file3\n");
175 	exit (2);
176 }
177 
178 static int
179 readin(int fd, struct diff **dd)
180 {
181 	int a, b, c, d;
182 	size_t i;
183 	char kind, *p;
184 	FILE *f;
185 
186 	f = fdopen(fd, "r");
187 	if (f == NULL)
188 		err(2, "fdopen");
189 	for (i=0; (p = getchange(f)); i++) {
190 		if (i >= szchanges - 1)
191 			increase();
192 		a = b = (int)strtoimax(p, &p, 10);
193 		if (*p == ',') {
194 			p++;
195 			b = (int)strtoimax(p, &p, 10);
196 		}
197 		kind = *p++;
198 		c = d = (int)strtoimax(p, &p, 10);
199 		if (*p==',') {
200 			p++;
201 			d = (int)strtoimax(p, &p, 10);
202 		}
203 		if (kind == 'a')
204 			a++;
205 		if (kind == 'd')
206 			c++;
207 		b++;
208 		d++;
209 		(*dd)[i].old.from = a;
210 		(*dd)[i].old.to = b;
211 		(*dd)[i].new.from = c;
212 		(*dd)[i].new.to = d;
213 	}
214 	if (i) {
215 		(*dd)[i].old.from = (*dd)[i-1].old.to;
216 		(*dd)[i].new.from = (*dd)[i-1].new.to;
217 	}
218 	fclose(f);
219 	return (i);
220 }
221 
222 static int
223 diffexec(const char *diffprog, char **diffargv, int fd[])
224 {
225 	int pid, pd;
226 
227 	switch (pid = pdfork(&pd, PD_CLOEXEC)) {
228 	case 0:
229 		close(fd[0]);
230 		if (dup2(fd[1], STDOUT_FILENO) == -1)
231 			err(2, "child could not duplicate descriptor");
232 		close(fd[1]);
233 		execvp(diffprog, diffargv);
234 		err(2, "could not execute diff: %s", diffprog);
235 		break;
236 	case -1:
237 		err(2, "could not fork");
238 		break;
239 	}
240 	close(fd[1]);
241 	return (pd);
242 }
243 
244 static char *
245 getchange(FILE *b)
246 {
247 	char *line;
248 
249 	while ((line = get_line(b, NULL))) {
250 		if (isdigit((unsigned char)line[0]))
251 			return (line);
252 	}
253 	return (NULL);
254 }
255 
256 
257 static char *
258 get_line(FILE *b, size_t *n)
259 {
260 	char *cp;
261 	size_t len;
262 	static char *buf;
263 	static size_t bufsize;
264 
265 	if ((cp = fgetln(b, &len)) == NULL)
266 		return (NULL);
267 
268 	if (cp[len - 1] != '\n')
269 		len++;
270 	if (len + 1 > bufsize) {
271 		do {
272 			bufsize += 1024;
273 		} while (len + 1 > bufsize);
274 		if ((buf = realloc(buf, bufsize)) == NULL)
275 			err(EXIT_FAILURE, NULL);
276 	}
277 	memcpy(buf, cp, len - 1);
278 	buf[len - 1] = '\n';
279 	buf[len] = '\0';
280 	if (n != NULL)
281 		*n = len;
282 	return (buf);
283 }
284 
285 static void
286 merge(int m1, int m2)
287 {
288 	struct diff *d1, *d2, *d3;
289 	int j, t1, t2;
290 	bool dup = false;
291 
292 	d1 = d13;
293 	d2 = d23;
294 	j = 0;
295 
296 	while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
297 		/* first file is different from the others */
298 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
299 			/* stuff peculiar to 1st file */
300 			if (eflag == 0) {
301 				printf("====1\n");
302 				change(1, &d1->old, false);
303 				keep(2, &d1->new);
304 				change(3, &d1->new, false);
305 			}
306 			d1++;
307 			continue;
308 		}
309 		/* second file is different from others */
310 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
311 			if (eflag == 0) {
312 				printf("====2\n");
313 				keep(1, &d2->new);
314 				change(3, &d2->new, false);
315 				change(2, &d2->old, false);
316 			}
317 			d2++;
318 			continue;
319 		}
320 		/*
321 		 * Merge overlapping changes in first file
322 		 * this happens after extension (see below).
323 		 */
324 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
325 			d1[1].old.from = d1->old.from;
326 			d1[1].new.from = d1->new.from;
327 			d1++;
328 			continue;
329 		}
330 
331 		/* merge overlapping changes in second */
332 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
333 			d2[1].old.from = d2->old.from;
334 			d2[1].new.from = d2->new.from;
335 			d2++;
336 			continue;
337 		}
338 		/* stuff peculiar to third file or different in all */
339 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
340 			dup = duplicate(&d1->old, &d2->old);
341 			/*
342 			 * dup = 0 means all files differ
343 			 * dup = 1 means files 1 and 2 identical
344 			 */
345 			if (eflag == 0) {
346 				printf("====%s\n", dup ? "3" : "");
347 				change(1, &d1->old, dup);
348 				change(2, &d2->old, false);
349 				d3 = d1->old.to > d1->old.from ? d1 : d2;
350 				change(3, &d3->new, false);
351 			} else
352 				j = edit(d1, dup, j);
353 			d1++;
354 			d2++;
355 			continue;
356 		}
357 		/*
358 		 * Overlapping changes from file 1 and 2; extend changes
359 		 * appropriately to make them coincide.
360 		 */
361 		if (d1->new.from < d2->new.from) {
362 			d2->old.from -= d2->new.from - d1->new.from;
363 			d2->new.from = d1->new.from;
364 		} else if (d2->new.from < d1->new.from) {
365 			d1->old.from -= d1->new.from - d2->new.from;
366 			d1->new.from = d2->new.from;
367 		}
368 		if (d1->new.to > d2->new.to) {
369 			d2->old.to += d1->new.to - d2->new.to;
370 			d2->new.to = d1->new.to;
371 		} else if (d2->new.to > d1->new.to) {
372 			d1->old.to += d2->new.to - d1->new.to;
373 			d1->new.to = d2->new.to;
374 		}
375 	}
376 	if (eflag)
377 		edscript(j);
378 }
379 
380 /*
381  * The range of lines rold.from thru rold.to in file i is to be changed.
382  * It is to be printed only if it does not duplicate something to be
383  * printed later.
384  */
385 static void
386 change(int i, struct range *rold, bool dup)
387 {
388 
389 	printf("%d:", i);
390 	last[i] = rold->to;
391 	prange(rold);
392 	if (dup)
393 		return;
394 	i--;
395 	skip(i, rold->from, NULL);
396 	skip(i, rold->to, "  ");
397 }
398 
399 /*
400  * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
401  * n1.
402  */
403 static void
404 prange(struct range *rold)
405 {
406 
407 	if (rold->to <= rold->from)
408 		printf("%da\n", rold->from - 1);
409 	else {
410 		printf("%d", rold->from);
411 		if (rold->to > rold->from+1)
412 			printf(",%d", rold->to - 1);
413 		printf("c\n");
414 	}
415 }
416 
417 /*
418  * No difference was reported by diff between file 1 (or 2) and file 3,
419  * and an artificial dummy difference (trange) must be ginned up to
420  * correspond to the change reported in the other file.
421  */
422 static void
423 keep(int i, struct range *rnew)
424 {
425 	int delta;
426 	struct range trange;
427 
428 	delta = last[3] - last[i];
429 	trange.from = rnew->from - delta;
430 	trange.to = rnew->to - delta;
431 	change(i, &trange, true);
432 }
433 
434 /*
435  * skip to just before line number from in file "i".  If "pr" is non-NULL,
436  * print all skipped stuff with string pr as a prefix.
437  */
438 static int
439 skip(int i, int from, const char *pr)
440 {
441 	size_t j, n;
442 	char *line;
443 
444 	for (n = 0; cline[i] < from - 1; n += j) {
445 		if ((line = get_line(fp[i], &j)) == NULL)
446 			errx(EXIT_FAILURE, "logic error");
447 		if (pr != NULL)
448 			printf("%s%s", Tflag == 1? "\t" : pr, line);
449 		cline[i]++;
450 	}
451 	return ((int) n);
452 }
453 
454 /*
455  * Return 1 or 0 according as the old range (in file 1) contains exactly
456  * the same data as the new range (in file 2).
457  */
458 static bool
459 duplicate(struct range *r1, struct range *r2)
460 {
461 	int c, d;
462 	int nchar;
463 	int nline;
464 
465 	if (r1->to-r1->from != r2->to-r2->from)
466 		return (0);
467 	skip(0, r1->from, NULL);
468 	skip(1, r2->from, NULL);
469 	nchar = 0;
470 	for (nline=0; nline < r1->to - r1->from; nline++) {
471 		do {
472 			c = getc(fp[0]);
473 			d = getc(fp[1]);
474 			if (c == -1 || d== -1)
475 				errx(EXIT_FAILURE, "logic error");
476 			nchar++;
477 			if (c != d) {
478 				repos(nchar);
479 				return (0);
480 			}
481 		} while (c != '\n');
482 	}
483 	repos(nchar);
484 	return (1);
485 }
486 
487 static void
488 repos(int nchar)
489 {
490 	int i;
491 
492 	for (i = 0; i < 2; i++)
493 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
494 }
495 /*
496  * collect an editing script for later regurgitation
497  */
498 static int
499 edit(struct diff *diff, bool dup, int j)
500 {
501 
502 	if (((dup + 1) & eflag) == 0)
503 		return (j);
504 	j++;
505 	overlap[j] = !dup;
506 	if (!dup)
507 		overlapcnt++;
508 	de[j].old.from = diff->old.from;
509 	de[j].old.to = diff->old.to;
510 	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
511 	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
512 	return (j);
513 }
514 
515 /* regurgitate */
516 static void
517 edscript(int n)
518 {
519 	int k;
520 	size_t j;
521 	char block[BUFSIZ];
522 
523 	for (; n > 0; n--) {
524 		if (!oflag || !overlap[n]) {
525 			prange(&de[n].old);
526 		} else {
527 			printf("%da\n", de[n].old.to -1);
528 			if (Aflag) {
529 				printf("%s\n", f2mark);
530 				fseek(fp[1], de[n].old.from, SEEK_SET);
531 				for (k = de[n].old.to - de[n].old.from; k > 0; k -= j) {
532 					j = k > BUFSIZ ? BUFSIZ : k;
533 					if (fread(block, 1, j, fp[1]) != j)
534 						errx(2, "logic error");
535 					fwrite(block, 1, j, stdout);
536 				}
537 				printf("\n");
538 			}
539 			printf("=======\n");
540 		}
541 		fseek(fp[2], (long)de[n].new.from, SEEK_SET);
542 		for (k = de[n].new.to - de[n].new.from; k > 0; k-= j) {
543 			j = k > BUFSIZ ? BUFSIZ : k;
544 			if (fread(block, 1, j, fp[2]) != j)
545 				errx(2, "logic error");
546 			fwrite(block, 1, j, stdout);
547 		}
548 		if (!oflag || !overlap[n])
549 			printf(".\n");
550 		else {
551 			printf("%s\n.\n", f3mark);
552 			printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
553 		}
554 	}
555 	if (iflag)
556 		printf("w\nq\n");
557 
558 	exit(eflag == 0 ? overlapcnt : 0);
559 }
560 
561 static void
562 increase(void)
563 {
564 	struct diff *p;
565 	char *q;
566 	size_t newsz, incr;
567 
568 	/* are the memset(3) calls needed? */
569 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
570 	incr = newsz - szchanges;
571 
572 	p = realloc(d13, newsz * sizeof(struct diff));
573 	if (p == NULL)
574 		err(1, NULL);
575 	memset(p + szchanges, 0, incr * sizeof(struct diff));
576 	d13 = p;
577 	p = realloc(d23, newsz * sizeof(struct diff));
578 	if (p == NULL)
579 		err(1, NULL);
580 	memset(p + szchanges, 0, incr * sizeof(struct diff));
581 	d23 = p;
582 	p = realloc(de, newsz * sizeof(struct diff));
583 	if (p == NULL)
584 		err(1, NULL);
585 	memset(p + szchanges, 0, incr * sizeof(struct diff));
586 	de = p;
587 	q = realloc(overlap, newsz * sizeof(char));
588 	if (q == NULL)
589 		err(1, NULL);
590 	memset(q + szchanges, 0, incr * sizeof(char));
591 	overlap = q;
592 	szchanges = newsz;
593 }
594 
595 
596 int
597 main(int argc, char **argv)
598 {
599 	int ch, nblabels, status, m, n, kq, nke, nleft, i;
600 	char *labels[] = { NULL, NULL, NULL };
601 	const char *diffprog = DIFF_PATH;
602 	char *file1, *file2, *file3;
603 	char *diffargv[6];
604 	int diffargc = 0;
605 	int fd13[2], fd23[2];
606 	int pd13, pd23;
607 	cap_rights_t rights_ro;
608 	struct kevent *e;
609 
610 	nblabels = 0;
611 	eflag = 0;
612 	oflag = 0;
613 	diffargv[diffargc++] = __DECONST(char *, diffprog);
614 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
615 		switch (ch) {
616 		case '3':
617 			eflag = 2;
618 			break;
619 		case 'a':
620 			diffargv[diffargc++] = __DECONST(char *, "-a");
621 			break;
622 		case 'A':
623 			Aflag = 1;
624 			break;
625 		case 'e':
626 			eflag = 3;
627 			break;
628 		case 'E':
629 			eflag = 3;
630 			oflag = 1;
631 			break;
632 		case 'i':
633 			iflag = 1;
634 			break;
635 		case 'L':
636 			oflag = 1;
637 			if (nblabels >= 3)
638 				errx(2, "too many file label options");
639 			labels[nblabels++] = optarg;
640 			break;
641 		case 'm':
642 			Aflag = 1;
643 			oflag = 1;
644 			mflag = 1;
645 			break;
646 		case 'T':
647 			Tflag = 1;
648 			break;
649 		case 'x':
650 			eflag = 1;
651 			break;
652 		case 'X':
653 			oflag = 1;
654 			eflag = 1;
655 			break;
656 		case DIFFPROG_OPT:
657 			diffprog = optarg;
658 			break;
659 		case STRIPCR_OPT:
660 			strip_cr = 1;
661 			break;
662 		}
663 	}
664 	argc -= optind;
665 	argv += optind;
666 
667 	if (Aflag) {
668 		eflag = 3;
669 		oflag = 1;
670 	}
671 
672 	if (argc != 3)
673 		usage();
674 
675 	if (caph_limit_stdio() == -1)
676 		err(2, "unable to limit stdio");
677 
678 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
679 
680 	kq = kqueue();
681 	if (kq == -1)
682 		err(2, "kqueue");
683 
684 	e = malloc(2 * sizeof(struct kevent));
685 	if (e == NULL)
686 		err(2, "malloc");
687 
688 	/* TODO stdio */
689 	file1 = argv[0];
690 	file2 = argv[1];
691 	file3 = argv[2];
692 
693 	if (oflag) {
694 		asprintf(&f1mark, "<<<<<<< %s",
695 		    labels[0] != NULL ? labels[0] : file1);
696 		if (f1mark == NULL)
697 			err(2, "asprintf");
698 		asprintf(&f2mark, "||||||| %s",
699 		    labels[1] != NULL ? labels[1] : file2);
700 		if (f2mark == NULL)
701 			err(2, "asprintf");
702 		asprintf(&f3mark, ">>>>>>> %s",
703 		    labels[2] != NULL ? labels[2] : file3);
704 		if (f3mark == NULL)
705 			err(2, "asprintf");
706 	}
707 	fp[0] = fopen(file1, "r");
708 	if (fp[0] == NULL)
709 		err(2, "Can't open %s", file1);
710 	if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
711 		err(2, "unable to limit rights on: %s", file1);
712 
713 	fp[1] = fopen(file2, "r");
714 	if (fp[1] == NULL)
715 		err(2, "Can't open %s", file2);
716 	if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
717 		err(2, "unable to limit rights on: %s", file2);
718 
719 	fp[2] = fopen(file3, "r");
720 	if (fp[2] == NULL)
721 		err(2, "Can't open %s", file3);
722 	if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
723 		err(2, "unable to limit rights on: %s", file3);
724 
725 	if (pipe(fd13))
726 		err(2, "pipe");
727 	if (pipe(fd23))
728 		err(2, "pipe");
729 
730 	diffargv[diffargc] = file1;
731 	diffargv[diffargc + 1] = file3;
732 	diffargv[diffargc + 2] = NULL;
733 
734 	nleft = 0;
735 	pd13 = diffexec(diffprog, diffargv, fd13);
736 	EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
737 	if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
738 		err(2, "kevent1");
739 	nleft++;
740 
741 	diffargv[diffargc] = file2;
742 	pd23 = diffexec(diffprog, diffargv, fd23);
743 	EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
744 	if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
745 		err(2, "kevent2");
746 	nleft++;
747 
748 	caph_cache_catpages();
749 	if (caph_enter() < 0)
750 		err(2, "unable to enter capability mode");
751 
752 	/* parse diffs */
753 	increase();
754 	m = readin(fd13[0], &d13);
755 	n = readin(fd23[0], &d23);
756 
757 	/* waitpid cooked over pdforks */
758 	while (nleft > 0) {
759 		nke = kevent(kq, NULL, 0, e, nleft, NULL);
760 		if (nke == -1)
761 			err(2, "kevent");
762 		for (i = 0; i < nke; i++) {
763 			status = e[i].data;
764 			if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
765 				errx(2, "diff exited abormally");
766 			else if (WIFSIGNALED(status))
767 				errx(2, "diff killed by signal %d",
768 				    WTERMSIG(status));
769 		}
770 		nleft -= nke;
771 	}
772 	merge(m, n);
773 
774 	return (EXIT_SUCCESS);
775 }
776