1 /* $OpenBSD: diff3prog.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
4 * Copyright (C) Caldera International Inc. 2001-2002.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 * Copyright (c) 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)diff3.c 8.1 (Berkeley) 6/6/93
69 static char sccsid[] = "@(#)diff3.c 8.1 (Berkeley) 6/6/93";
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
75 #include <sys/capsicum.h>
76 #include <sys/procdesc.h>
77 #include <sys/types.h>
78 #include <sys/event.h>
81 #include <capsicum_helpers.h>
94 * "from" is first in range of changed lines; "to" is last+1
95 * from=to=line after point of insertion for added lines.
107 static size_t szchanges;
109 static struct diff *d13;
110 static struct diff *d23;
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.
118 static struct diff *de;
119 static char *overlap;
120 static int overlapcnt;
122 static int cline[3]; /* # of the last-read line in each file (0-2) */
124 * The latest known correspondence between line numbers of the 3 files
125 * is stored in last[1-3];
128 static int Aflag, eflag, iflag, mflag, Tflag;
129 static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
131 static char *f1mark, *f2mark, *f3mark;
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;
153 #define DIFF_PATH "/usr/bin/diff"
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 },
173 fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L lable1] [-L label2] "
174 "[ -L label3] file1 file2 file3\n");
179 readin(int fd, struct diff **dd)
189 for (i=0; (p = getchange(f)); i++) {
190 if (i >= szchanges - 1)
192 a = b = (int)strtoimax(p, &p, 10);
195 b = (int)strtoimax(p, &p, 10);
198 c = d = (int)strtoimax(p, &p, 10);
201 d = (int)strtoimax(p, &p, 10);
209 (*dd)[i].old.from = a;
211 (*dd)[i].new.from = c;
215 (*dd)[i].old.from = (*dd)[i-1].old.to;
216 (*dd)[i].new.from = (*dd)[i-1].new.to;
223 diffexec(const char *diffprog, char **diffargv, int fd[])
227 switch (pid = pdfork(&pd, PD_CLOEXEC)) {
230 if (dup2(fd[1], STDOUT_FILENO) == -1)
231 err(2, "child could not duplicate descriptor");
233 execvp(diffprog, diffargv);
234 err(2, "could not execute diff: %s", diffprog);
237 err(2, "could not fork");
249 while ((line = get_line(b, NULL))) {
250 if (isdigit((unsigned char)line[0]))
258 get_line(FILE *b, size_t *n)
261 static char *buf = NULL;
262 static size_t bufsize = 0;
264 if ((len = getline(&buf, &bufsize, b)) < 0)
274 merge(int m1, int m2)
276 struct diff *d1, *d2, *d3;
284 while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
285 /* first file is different from the others */
286 if (!t2 || (t1 && d1->new.to < d2->new.from)) {
287 /* stuff peculiar to 1st file */
290 change(1, &d1->old, false);
292 change(3, &d1->new, false);
297 /* second file is different from others */
298 if (!t1 || (t2 && d2->new.to < d1->new.from)) {
302 change(3, &d2->new, false);
303 change(2, &d2->old, false);
309 * Merge overlapping changes in first file
310 * this happens after extension (see below).
312 if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
313 d1[1].old.from = d1->old.from;
314 d1[1].new.from = d1->new.from;
319 /* merge overlapping changes in second */
320 if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
321 d2[1].old.from = d2->old.from;
322 d2[1].new.from = d2->new.from;
326 /* stuff peculiar to third file or different in all */
327 if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
328 dup = duplicate(&d1->old, &d2->old);
330 * dup = 0 means all files differ
331 * dup = 1 means files 1 and 2 identical
334 printf("====%s\n", dup ? "3" : "");
335 change(1, &d1->old, dup);
336 change(2, &d2->old, false);
337 d3 = d1->old.to > d1->old.from ? d1 : d2;
338 change(3, &d3->new, false);
340 j = edit(d1, dup, j);
346 * Overlapping changes from file 1 and 2; extend changes
347 * appropriately to make them coincide.
349 if (d1->new.from < d2->new.from) {
350 d2->old.from -= d2->new.from - d1->new.from;
351 d2->new.from = d1->new.from;
352 } else if (d2->new.from < d1->new.from) {
353 d1->old.from -= d1->new.from - d2->new.from;
354 d1->new.from = d2->new.from;
356 if (d1->new.to > d2->new.to) {
357 d2->old.to += d1->new.to - d2->new.to;
358 d2->new.to = d1->new.to;
359 } else if (d2->new.to > d1->new.to) {
360 d1->old.to += d2->new.to - d1->new.to;
361 d1->new.to = d2->new.to;
369 * The range of lines rold.from thru rold.to in file i is to be changed.
370 * It is to be printed only if it does not duplicate something to be
374 change(int i, struct range *rold, bool dup)
383 skip(i, rold->from, NULL);
384 skip(i, rold->to, " ");
388 * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
392 prange(struct range *rold)
395 if (rold->to <= rold->from)
396 printf("%da\n", rold->from - 1);
398 printf("%d", rold->from);
399 if (rold->to > rold->from+1)
400 printf(",%d", rold->to - 1);
406 * No difference was reported by diff between file 1 (or 2) and file 3,
407 * and an artificial dummy difference (trange) must be ginned up to
408 * correspond to the change reported in the other file.
411 keep(int i, struct range *rnew)
416 delta = last[3] - last[i];
417 trange.from = rnew->from - delta;
418 trange.to = rnew->to - delta;
419 change(i, &trange, true);
423 * skip to just before line number from in file "i". If "pr" is non-NULL,
424 * print all skipped stuff with string pr as a prefix.
427 skip(int i, int from, const char *pr)
432 for (n = 0; cline[i] < from - 1; n += j) {
433 if ((line = get_line(fp[i], &j)) == NULL)
434 errx(EXIT_FAILURE, "logic error");
436 printf("%s%s", Tflag == 1? "\t" : pr, line);
443 * Return 1 or 0 according as the old range (in file 1) contains exactly
444 * the same data as the new range (in file 2).
447 duplicate(struct range *r1, struct range *r2)
453 if (r1->to-r1->from != r2->to-r2->from)
455 skip(0, r1->from, NULL);
456 skip(1, r2->from, NULL);
458 for (nline=0; nline < r1->to - r1->from; nline++) {
462 if (c == -1 && d == -1)
464 if (c == -1 || d== -1)
465 errx(EXIT_FAILURE, "logic error");
482 for (i = 0; i < 2; i++)
483 (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
486 * collect an editing script for later regurgitation
489 edit(struct diff *diff, bool dup, int j)
492 if (((dup + 1) & eflag) == 0)
498 de[j].old.from = diff->old.from;
499 de[j].old.to = diff->old.to;
500 de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
501 de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
514 if (!oflag || !overlap[n]) {
517 printf("%da\n", de[n].old.to -1);
519 printf("%s\n", f2mark);
520 fseek(fp[1], de[n].old.from, SEEK_SET);
521 for (k = de[n].old.to - de[n].old.from; k > 0; k -= j) {
522 j = k > BUFSIZ ? BUFSIZ : k;
523 if (fread(block, 1, j, fp[1]) != j)
524 errx(2, "logic error");
525 fwrite(block, 1, j, stdout);
531 fseek(fp[2], (long)de[n].new.from, SEEK_SET);
532 for (k = de[n].new.to - de[n].new.from; k > 0; k-= j) {
534 j = k > BUFSIZ ? BUFSIZ : k;
535 r = fread(block, 1, j, fp[2]);
539 errx(2, "logic error");
543 (void)fwrite(block, 1, j, stdout);
545 if (!oflag || !overlap[n])
548 printf("%s\n.\n", f3mark);
549 printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
555 exit(eflag == 0 ? overlapcnt : 0);
565 /* are the memset(3) calls needed? */
566 newsz = szchanges == 0 ? 64 : 2 * szchanges;
567 incr = newsz - szchanges;
569 p = reallocarray(d13, newsz, sizeof(struct diff));
572 memset(p + szchanges, 0, incr * sizeof(struct diff));
574 p = reallocarray(d23, newsz, sizeof(struct diff));
577 memset(p + szchanges, 0, incr * sizeof(struct diff));
579 p = reallocarray(de, newsz, sizeof(struct diff));
582 memset(p + szchanges, 0, incr * sizeof(struct diff));
584 q = reallocarray(overlap, newsz, sizeof(char));
587 memset(q + szchanges, 0, incr * sizeof(char));
594 main(int argc, char **argv)
596 int ch, nblabels, status, m, n, kq, nke, nleft, i;
597 char *labels[] = { NULL, NULL, NULL };
598 const char *diffprog = DIFF_PATH;
599 char *file1, *file2, *file3;
602 int fd13[2], fd23[2];
604 cap_rights_t rights_ro;
610 diffargv[diffargc++] = __DECONST(char *, diffprog);
611 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
617 diffargv[diffargc++] = __DECONST(char *, "-a");
635 errx(2, "too many file label options");
636 labels[nblabels++] = optarg;
672 if (caph_limit_stdio() == -1)
673 err(2, "unable to limit stdio");
675 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
681 e = malloc(2 * sizeof(struct kevent));
691 asprintf(&f1mark, "<<<<<<< %s",
692 labels[0] != NULL ? labels[0] : file1);
695 asprintf(&f2mark, "||||||| %s",
696 labels[1] != NULL ? labels[1] : file2);
699 asprintf(&f3mark, ">>>>>>> %s",
700 labels[2] != NULL ? labels[2] : file3);
704 fp[0] = fopen(file1, "r");
706 err(2, "Can't open %s", file1);
707 if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
708 err(2, "unable to limit rights on: %s", file1);
710 fp[1] = fopen(file2, "r");
712 err(2, "Can't open %s", file2);
713 if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
714 err(2, "unable to limit rights on: %s", file2);
716 fp[2] = fopen(file3, "r");
718 err(2, "Can't open %s", file3);
719 if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
720 err(2, "unable to limit rights on: %s", file3);
727 diffargv[diffargc] = file1;
728 diffargv[diffargc + 1] = file3;
729 diffargv[diffargc + 2] = NULL;
732 pd13 = diffexec(diffprog, diffargv, fd13);
733 EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
734 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
738 diffargv[diffargc] = file2;
739 pd23 = diffexec(diffprog, diffargv, fd23);
740 EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
741 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
745 caph_cache_catpages();
746 if (caph_enter() < 0)
747 err(2, "unable to enter capability mode");
751 m = readin(fd13[0], &d13);
752 n = readin(fd23[0], &d23);
754 /* waitpid cooked over pdforks */
756 nke = kevent(kq, NULL, 0, e, nleft, NULL);
759 for (i = 0; i < nke; i++) {
761 if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
762 errx(2, "diff exited abormally");
763 else if (WIFSIGNALED(status))
764 errx(2, "diff killed by signal %d",
771 return (EXIT_SUCCESS);