xref: /freebsd/usr.bin/diff3/diff3.c (revision cc9e6590773dba57440750c124173ed531349a06)
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 *, bool);
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 label1] [-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 pd;
226 
227 	switch (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 	ssize_t len;
261 	static char *buf = NULL;
262 	static size_t bufsize = 0;
263 
264 	if ((len = getline(&buf, &bufsize, b)) < 0)
265 		return (NULL);
266 
267 	if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
268 		buf[len - 2] = '\n';
269 		buf[len - 1] = '\0';
270 		len--;
271 	}
272 
273 	if (n != NULL)
274 		*n = len;
275 
276 	return (buf);
277 }
278 
279 static void
280 merge(int m1, int m2)
281 {
282 	struct diff *d1, *d2, *d3;
283 	int j, t1, t2;
284 	bool dup = false;
285 
286 	d1 = d13;
287 	d2 = d23;
288 	j = 0;
289 
290 	while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
291 		/* first file is different from the others */
292 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
293 			/* stuff peculiar to 1st file */
294 			if (eflag == 0) {
295 				printf("====1\n");
296 				change(1, &d1->old, false);
297 				keep(2, &d1->new);
298 				change(3, &d1->new, false);
299 			}
300 			d1++;
301 			continue;
302 		}
303 		/* second file is different from others */
304 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
305 			if (eflag == 0) {
306 				printf("====2\n");
307 				keep(1, &d2->new);
308 				change(3, &d2->new, false);
309 				change(2, &d2->old, false);
310 			}
311 			d2++;
312 			continue;
313 		}
314 		/*
315 		 * Merge overlapping changes in first file
316 		 * this happens after extension (see below).
317 		 */
318 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
319 			d1[1].old.from = d1->old.from;
320 			d1[1].new.from = d1->new.from;
321 			d1++;
322 			continue;
323 		}
324 
325 		/* merge overlapping changes in second */
326 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
327 			d2[1].old.from = d2->old.from;
328 			d2[1].new.from = d2->new.from;
329 			d2++;
330 			continue;
331 		}
332 		/* stuff peculiar to third file or different in all */
333 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
334 			dup = duplicate(&d1->old, &d2->old);
335 			/*
336 			 * dup = 0 means all files differ
337 			 * dup = 1 means files 1 and 2 identical
338 			 */
339 			if (eflag == 0) {
340 				printf("====%s\n", dup ? "3" : "");
341 				change(1, &d1->old, dup);
342 				change(2, &d2->old, false);
343 				d3 = d1->old.to > d1->old.from ? d1 : d2;
344 				change(3, &d3->new, false);
345 			} else
346 				j = edit(d1, dup, j);
347 			d1++;
348 			d2++;
349 			continue;
350 		}
351 		/*
352 		 * Overlapping changes from file 1 and 2; extend changes
353 		 * appropriately to make them coincide.
354 		 */
355 		if (d1->new.from < d2->new.from) {
356 			d2->old.from -= d2->new.from - d1->new.from;
357 			d2->new.from = d1->new.from;
358 		} else if (d2->new.from < d1->new.from) {
359 			d1->old.from -= d1->new.from - d2->new.from;
360 			d1->new.from = d2->new.from;
361 		}
362 		if (d1->new.to > d2->new.to) {
363 			d2->old.to += d1->new.to - d2->new.to;
364 			d2->new.to = d1->new.to;
365 		} else if (d2->new.to > d1->new.to) {
366 			d1->old.to += d2->new.to - d1->new.to;
367 			d1->new.to = d2->new.to;
368 		}
369 	}
370 	if (eflag)
371 		edscript(j);
372 }
373 
374 /*
375  * The range of lines rold.from thru rold.to in file i is to be changed.
376  * It is to be printed only if it does not duplicate something to be
377  * printed later.
378  */
379 static void
380 change(int i, struct range *rold, bool dup)
381 {
382 
383 	printf("%d:", i);
384 	last[i] = rold->to;
385 	prange(rold, false);
386 	if (dup)
387 		return;
388 	i--;
389 	skip(i, rold->from, NULL);
390 	skip(i, rold->to, "  ");
391 }
392 
393 /*
394  * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
395  * n1.
396  */
397 static void
398 prange(struct range *rold, bool delete)
399 {
400 
401 	if (rold->to <= rold->from)
402 		printf("%da\n", rold->from - 1);
403 	else {
404 		printf("%d", rold->from);
405 		if (rold->to > rold->from + 1)
406 			printf(",%d", rold->to - 1);
407 		if (delete)
408 			printf("d\n");
409 		else
410 			printf("c\n");
411 	}
412 }
413 
414 /*
415  * No difference was reported by diff between file 1 (or 2) and file 3,
416  * and an artificial dummy difference (trange) must be ginned up to
417  * correspond to the change reported in the other file.
418  */
419 static void
420 keep(int i, struct range *rnew)
421 {
422 	int delta;
423 	struct range trange;
424 
425 	delta = last[3] - last[i];
426 	trange.from = rnew->from - delta;
427 	trange.to = rnew->to - delta;
428 	change(i, &trange, true);
429 }
430 
431 /*
432  * skip to just before line number from in file "i".  If "pr" is non-NULL,
433  * print all skipped stuff with string pr as a prefix.
434  */
435 static int
436 skip(int i, int from, const char *pr)
437 {
438 	size_t j, n;
439 	char *line;
440 
441 	for (n = 0; cline[i] < from - 1; n += j) {
442 		if ((line = get_line(fp[i], &j)) == NULL)
443 			errx(EXIT_FAILURE, "logic error");
444 		if (pr != NULL)
445 			printf("%s%s", Tflag == 1 ? "\t" : pr, line);
446 		cline[i]++;
447 	}
448 	return ((int) n);
449 }
450 
451 /*
452  * Return 1 or 0 according as the old range (in file 1) contains exactly
453  * the same data as the new range (in file 2).
454  */
455 static bool
456 duplicate(struct range *r1, struct range *r2)
457 {
458 	int c, d;
459 	int nchar;
460 	int nline;
461 
462 	if (r1->to-r1->from != r2->to-r2->from)
463 		return (0);
464 	skip(0, r1->from, NULL);
465 	skip(1, r2->from, NULL);
466 	nchar = 0;
467 	for (nline = 0; nline < r1->to - r1->from; nline++) {
468 		do {
469 			c = getc(fp[0]);
470 			d = getc(fp[1]);
471 			if (c == -1 && d == -1)
472 				break;
473 			if (c == -1 || d == -1)
474 				errx(EXIT_FAILURE, "logic error");
475 			nchar++;
476 			if (c != d) {
477 				repos(nchar);
478 				return (0);
479 			}
480 		} while (c != '\n');
481 	}
482 	repos(nchar);
483 	return (1);
484 }
485 
486 static void
487 repos(int nchar)
488 {
489 	int i;
490 
491 	for (i = 0; i < 2; i++)
492 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
493 }
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 	bool delete;
521 	size_t j;
522 	char block[BUFSIZ];
523 
524 	for (; n > 0; n--) {
525 		delete = (de[n].new.from == de[n].new.to);
526 		if (!oflag || !overlap[n]) {
527 			prange(&de[n].old, delete);
528 		} else {
529 			printf("%da\n", de[n].old.to - 1);
530 			if (Aflag) {
531 				printf("%s\n", f2mark);
532 				fseek(fp[1], de[n].old.from, SEEK_SET);
533 				for (k = de[n].old.to - de[n].old.from; k > 0; k -= j) {
534 					j = k > BUFSIZ ? BUFSIZ : k;
535 					if (fread(block, 1, j, fp[1]) != j)
536 						errx(2, "logic error");
537 					fwrite(block, 1, j, stdout);
538 				}
539 				printf("\n");
540 			}
541 			printf("=======\n");
542 		}
543 		fseek(fp[2], (long)de[n].new.from, SEEK_SET);
544 		for (k = de[n].new.to - de[n].new.from; k > 0; k -= j) {
545 			size_t r;
546 
547 			j = k > BUFSIZ ? BUFSIZ : k;
548 			r = fread(block, 1, j, fp[2]);
549 			if (r == 0) {
550 				if (feof(fp[2]))
551 					break;
552 				errx(2, "logic error");
553 			}
554 			if (r != j)
555 				j = r;
556 			(void)fwrite(block, 1, j, stdout);
557 		}
558 		if (!oflag || !overlap[n]) {
559 			if (!delete)
560 				printf(".\n");
561 		} else {
562 			printf("%s\n.\n", f3mark);
563 			printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
564 		}
565 	}
566 	if (iflag)
567 		printf("w\nq\n");
568 
569 	exit(eflag == 0 ? overlapcnt : 0);
570 }
571 
572 static void
573 increase(void)
574 {
575 	struct diff *p;
576 	char *q;
577 	size_t newsz, incr;
578 
579 	/* are the memset(3) calls needed? */
580 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
581 	incr = newsz - szchanges;
582 
583 	p = reallocarray(d13, newsz, sizeof(struct diff));
584 	if (p == NULL)
585 		err(1, NULL);
586 	memset(p + szchanges, 0, incr * sizeof(struct diff));
587 	d13 = p;
588 	p = reallocarray(d23, newsz, sizeof(struct diff));
589 	if (p == NULL)
590 		err(1, NULL);
591 	memset(p + szchanges, 0, incr * sizeof(struct diff));
592 	d23 = p;
593 	p = reallocarray(de, newsz, sizeof(struct diff));
594 	if (p == NULL)
595 		err(1, NULL);
596 	memset(p + szchanges, 0, incr * sizeof(struct diff));
597 	de = p;
598 	q = reallocarray(overlap, newsz, sizeof(char));
599 	if (q == NULL)
600 		err(1, NULL);
601 	memset(q + szchanges, 0, incr * sizeof(char));
602 	overlap = q;
603 	szchanges = newsz;
604 }
605 
606 
607 int
608 main(int argc, char **argv)
609 {
610 	int ch, nblabels, status, m, n, kq, nke, nleft, i;
611 	char *labels[] = { NULL, NULL, NULL };
612 	const char *diffprog = DIFF_PATH;
613 	char *file1, *file2, *file3;
614 	char *diffargv[7];
615 	int diffargc = 0;
616 	int fd13[2], fd23[2];
617 	int pd13, pd23;
618 	cap_rights_t rights_ro;
619 	struct kevent *e;
620 
621 	nblabels = 0;
622 	eflag = 0;
623 	oflag = 0;
624 	diffargv[diffargc++] = __DECONST(char *, diffprog);
625 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
626 		switch (ch) {
627 		case '3':
628 			eflag = 2;
629 			break;
630 		case 'a':
631 			diffargv[diffargc++] = __DECONST(char *, "-a");
632 			break;
633 		case 'A':
634 			Aflag = 1;
635 			break;
636 		case 'e':
637 			eflag = 3;
638 			break;
639 		case 'E':
640 			eflag = 3;
641 			oflag = 1;
642 			break;
643 		case 'i':
644 			iflag = 1;
645 			break;
646 		case 'L':
647 			oflag = 1;
648 			if (nblabels >= 3)
649 				errx(2, "too many file label options");
650 			labels[nblabels++] = optarg;
651 			break;
652 		case 'm':
653 			Aflag = 1;
654 			oflag = 1;
655 			mflag = 1;
656 			break;
657 		case 'T':
658 			Tflag = 1;
659 			break;
660 		case 'x':
661 			eflag = 1;
662 			break;
663 		case 'X':
664 			oflag = 1;
665 			eflag = 1;
666 			break;
667 		case DIFFPROG_OPT:
668 			diffprog = optarg;
669 			break;
670 		case STRIPCR_OPT:
671 			strip_cr = 1;
672 			diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
673 			break;
674 		}
675 	}
676 	argc -= optind;
677 	argv += optind;
678 
679 	if (Aflag) {
680 		eflag = 3;
681 		oflag = 1;
682 	}
683 
684 	if (argc != 3)
685 		usage();
686 
687 	if (caph_limit_stdio() == -1)
688 		err(2, "unable to limit stdio");
689 
690 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
691 
692 	kq = kqueue();
693 	if (kq == -1)
694 		err(2, "kqueue");
695 
696 	e = malloc(2 * sizeof(struct kevent));
697 	if (e == NULL)
698 		err(2, "malloc");
699 
700 	/* TODO stdio */
701 	file1 = argv[0];
702 	file2 = argv[1];
703 	file3 = argv[2];
704 
705 	if (oflag) {
706 		asprintf(&f1mark, "<<<<<<< %s",
707 		    labels[0] != NULL ? labels[0] : file1);
708 		if (f1mark == NULL)
709 			err(2, "asprintf");
710 		asprintf(&f2mark, "||||||| %s",
711 		    labels[1] != NULL ? labels[1] : file2);
712 		if (f2mark == NULL)
713 			err(2, "asprintf");
714 		asprintf(&f3mark, ">>>>>>> %s",
715 		    labels[2] != NULL ? labels[2] : file3);
716 		if (f3mark == NULL)
717 			err(2, "asprintf");
718 	}
719 	fp[0] = fopen(file1, "r");
720 	if (fp[0] == NULL)
721 		err(2, "Can't open %s", file1);
722 	if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
723 		err(2, "unable to limit rights on: %s", file1);
724 
725 	fp[1] = fopen(file2, "r");
726 	if (fp[1] == NULL)
727 		err(2, "Can't open %s", file2);
728 	if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
729 		err(2, "unable to limit rights on: %s", file2);
730 
731 	fp[2] = fopen(file3, "r");
732 	if (fp[2] == NULL)
733 		err(2, "Can't open %s", file3);
734 	if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
735 		err(2, "unable to limit rights on: %s", file3);
736 
737 	if (pipe(fd13))
738 		err(2, "pipe");
739 	if (pipe(fd23))
740 		err(2, "pipe");
741 
742 	diffargv[diffargc] = file1;
743 	diffargv[diffargc + 1] = file3;
744 	diffargv[diffargc + 2] = NULL;
745 
746 	nleft = 0;
747 	pd13 = diffexec(diffprog, diffargv, fd13);
748 	EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
749 	if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
750 		err(2, "kevent1");
751 	nleft++;
752 
753 	diffargv[diffargc] = file2;
754 	pd23 = diffexec(diffprog, diffargv, fd23);
755 	EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
756 	if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
757 		err(2, "kevent2");
758 	nleft++;
759 
760 	caph_cache_catpages();
761 	if (caph_enter() < 0)
762 		err(2, "unable to enter capability mode");
763 
764 	/* parse diffs */
765 	increase();
766 	m = readin(fd13[0], &d13);
767 	n = readin(fd23[0], &d23);
768 
769 	/* waitpid cooked over pdforks */
770 	while (nleft > 0) {
771 		nke = kevent(kq, NULL, 0, e, nleft, NULL);
772 		if (nke == -1)
773 			err(2, "kevent");
774 		for (i = 0; i < nke; i++) {
775 			status = e[i].data;
776 			if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
777 				errx(2, "diff exited abnormally");
778 			else if (WIFSIGNALED(status))
779 				errx(2, "diff killed by signal %d",
780 				    WTERMSIG(status));
781 		}
782 		nleft -= nke;
783 	}
784 	merge(m, n);
785 
786 	return (EXIT_SUCCESS);
787 }
788