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>
92 * "from" is first in range of changed lines; "to" is last+1
93 * from=to=line after point of insertion for added lines.
105 static size_t szchanges;
107 static struct diff *d13;
108 static struct diff *d23;
110 * "de" is used to gather editing scripts. These are later spewed out in
111 * reverse order. Its first element must be all zero, the "new" component
112 * of "de" contains line positions or byte positions depending on when you
113 * look (!?). Array overlap indicates which sections in "de" correspond to
114 * lines that are different in all three files.
116 static struct diff *de;
117 static char *overlap;
118 static int overlapcnt;
120 static int cline[3]; /* # of the last-read line in each file (0-2) */
122 * The latest known correspondence between line numbers of the 3 files
123 * is stored in last[1-3];
126 static int Aflag, eflag, iflag, mflag, Tflag;
127 static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
129 static char *f1mark, *f2mark, *f3mark;
131 static bool duplicate(struct range *, struct range *);
132 static int edit(struct diff *, bool, int);
133 static char *getchange(FILE *);
134 static char *get_line(FILE *, size_t *);
135 static int number(char **);
136 static int readin(int fd, struct diff **);
137 static int skip(int, int, const char *);
138 static void change(int, struct range *, bool);
139 static void keep(int, struct range *);
140 static void merge(int, int);
141 static void prange(struct range *);
142 static void repos(int);
143 static void edscript(int) __dead2;
144 static void increase(void);
145 static void usage(void) __dead2;
152 #define DIFF_PATH "/usr/bin/diff"
154 #define OPTIONS "3aAeEiL:mTxX"
155 static struct option longopts[] = {
156 { "ed", no_argument, NULL, 'e' },
157 { "show-overlap", no_argument, NULL, 'E' },
158 { "overlap-only", no_argument, NULL, 'x' },
159 { "initial-tab", no_argument, NULL, 'T' },
160 { "text", no_argument, NULL, 'a' },
161 { "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT },
162 { "show-all", no_argument, NULL, 'A' },
163 { "easy-only", no_argument, NULL, '3' },
164 { "merge", no_argument, NULL, 'm' },
165 { "label", required_argument, NULL, 'L' },
166 { "diff-program", required_argument, NULL, DIFFPROG_OPT },
172 fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L lable1] [-L label2] "
173 "[ -L label3] file1 file2 file3\n");
178 readin(int fd, struct diff **dd)
188 for (i=0; (p = getchange(f)); i++) {
189 if (i >= szchanges - 1)
208 (*dd)[i].old.from = a;
210 (*dd)[i].new.from = c;
214 (*dd)[i].old.from = (*dd)[i-1].old.to;
215 (*dd)[i].new.from = (*dd)[i-1].new.to;
222 diffexec(const char *diffprog, char **diffargv, int fd[])
226 switch (pid = pdfork(&pd, PD_CLOEXEC)) {
229 if (dup2(fd[1], STDOUT_FILENO) == -1)
230 err(2, "child could not duplicate descriptor");
232 execvp(diffprog, diffargv);
233 err(2, "could not execute diff: %s", diffprog);
236 err(2, "could not fork");
249 while (isdigit((unsigned char)(**lc)))
250 nn = nn*10 + *(*lc)++ - '0';
259 while ((line = get_line(b, NULL))) {
260 if (isdigit((unsigned char)line[0]))
268 get_line(FILE *b, size_t *n)
273 static size_t bufsize;
275 if ((cp = fgetln(b, &len)) == NULL)
278 if (cp[len - 1] != '\n')
280 if (len + 1 > bufsize) {
283 } while (len + 1 > bufsize);
284 if ((buf = realloc(buf, bufsize)) == NULL)
285 err(EXIT_FAILURE, NULL);
287 memcpy(buf, cp, len - 1);
296 merge(int m1, int m2)
298 struct diff *d1, *d2, *d3;
306 while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
307 /* first file is different from the others */
308 if (!t2 || (t1 && d1->new.to < d2->new.from)) {
309 /* stuff peculiar to 1st file */
312 change(1, &d1->old, false);
314 change(3, &d1->new, false);
319 /* second file is different from others */
320 if (!t1 || (t2 && d2->new.to < d1->new.from)) {
324 change(3, &d2->new, false);
325 change(2, &d2->old, false);
331 * Merge overlapping changes in first file
332 * this happens after extension (see below).
334 if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
335 d1[1].old.from = d1->old.from;
336 d1[1].new.from = d1->new.from;
341 /* merge overlapping changes in second */
342 if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
343 d2[1].old.from = d2->old.from;
344 d2[1].new.from = d2->new.from;
348 /* stuff peculiar to third file or different in all */
349 if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
350 dup = duplicate(&d1->old, &d2->old);
352 * dup = 0 means all files differ
353 * dup = 1 means files 1 and 2 identical
356 printf("====%s\n", dup ? "3" : "");
357 change(1, &d1->old, dup);
358 change(2, &d2->old, false);
359 d3 = d1->old.to > d1->old.from ? d1 : d2;
360 change(3, &d3->new, false);
362 j = edit(d1, dup, j);
368 * Overlapping changes from file 1 and 2; extend changes
369 * appropriately to make them coincide.
371 if (d1->new.from < d2->new.from) {
372 d2->old.from -= d2->new.from - d1->new.from;
373 d2->new.from = d1->new.from;
374 } else if (d2->new.from < d1->new.from) {
375 d1->old.from -= d1->new.from - d2->new.from;
376 d1->new.from = d2->new.from;
378 if (d1->new.to > d2->new.to) {
379 d2->old.to += d1->new.to - d2->new.to;
380 d2->new.to = d1->new.to;
381 } else if (d2->new.to > d1->new.to) {
382 d1->old.to += d2->new.to - d1->new.to;
383 d1->new.to = d2->new.to;
391 * The range of lines rold.from thru rold.to in file i is to be changed.
392 * It is to be printed only if it does not duplicate something to be
396 change(int i, struct range *rold, bool dup)
405 skip(i, rold->from, NULL);
406 skip(i, rold->to, " ");
410 * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
414 prange(struct range *rold)
417 if (rold->to <= rold->from)
418 printf("%da\n", rold->from - 1);
420 printf("%d", rold->from);
421 if (rold->to > rold->from+1)
422 printf(",%d", rold->to - 1);
428 * No difference was reported by diff between file 1 (or 2) and file 3,
429 * and an artificial dummy difference (trange) must be ginned up to
430 * correspond to the change reported in the other file.
433 keep(int i, struct range *rnew)
438 delta = last[3] - last[i];
439 trange.from = rnew->from - delta;
440 trange.to = rnew->to - delta;
441 change(i, &trange, true);
445 * skip to just before line number from in file "i". If "pr" is non-NULL,
446 * print all skipped stuff with string pr as a prefix.
449 skip(int i, int from, const char *pr)
454 for (n = 0; cline[i] < from - 1; n += j) {
455 if ((line = get_line(fp[i], &j)) == NULL)
456 errx(EXIT_FAILURE, "logic error");
458 printf("%s%s", Tflag == 1? "\t" : pr, line);
465 * Return 1 or 0 according as the old range (in file 1) contains exactly
466 * the same data as the new range (in file 2).
469 duplicate(struct range *r1, struct range *r2)
475 if (r1->to-r1->from != r2->to-r2->from)
477 skip(0, r1->from, NULL);
478 skip(1, r2->from, NULL);
480 for (nline=0; nline < r1->to - r1->from; nline++) {
484 if (c == -1 || d== -1)
485 errx(EXIT_FAILURE, "logic error");
502 for (i = 0; i < 2; i++)
503 (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
506 * collect an editing script for later regurgitation
509 edit(struct diff *diff, bool dup, int j)
512 if (((dup + 1) & eflag) == 0)
518 de[j].old.from = diff->old.from;
519 de[j].old.to = diff->old.to;
520 de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
521 de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
534 if (!oflag || !overlap[n]) {
537 printf("%da\n", de[n].old.to -1);
539 printf("%s\n", f2mark);
540 fseek(fp[1], de[n].old.from, SEEK_SET);
541 for (k = de[n].old.to - de[n].old.from; k > 0; k -= j) {
542 j = k > BUFSIZ ? BUFSIZ : k;
543 if (fread(block, 1, j, fp[1]) != j)
544 errx(2, "logic error");
545 fwrite(block, 1, j, stdout);
551 fseek(fp[2], (long)de[n].new.from, SEEK_SET);
552 for (k = de[n].new.to - de[n].new.from; k > 0; k-= j) {
553 j = k > BUFSIZ ? BUFSIZ : k;
554 if (fread(block, 1, j, fp[2]) != j)
555 errx(2, "logic error");
556 fwrite(block, 1, j, stdout);
558 if (!oflag || !overlap[n])
561 printf("%s\n.\n", f3mark);
562 printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
568 exit(eflag == 0 ? overlapcnt : 0);
578 /* are the memset(3) calls needed? */
579 newsz = szchanges == 0 ? 64 : 2 * szchanges;
580 incr = newsz - szchanges;
582 p = realloc(d13, newsz * sizeof(struct diff));
585 memset(p + szchanges, 0, incr * sizeof(struct diff));
587 p = realloc(d23, newsz * sizeof(struct diff));
590 memset(p + szchanges, 0, incr * sizeof(struct diff));
592 p = realloc(de, newsz * sizeof(struct diff));
595 memset(p + szchanges, 0, incr * sizeof(struct diff));
597 q = realloc(overlap, newsz * sizeof(char));
600 memset(q + szchanges, 0, incr * sizeof(char));
607 main(int argc, char **argv)
609 int ch, nblabels, status, m, n, kq, nke, nleft, i;
610 char *labels[] = { NULL, NULL, NULL };
611 const char *diffprog = DIFF_PATH;
612 char *file1, *file2, *file3;
615 int fd13[2], fd23[2];
617 cap_rights_t rights_ro;
623 diffargv[diffargc++] = __DECONST(char *, diffprog);
624 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
630 diffargv[diffargc++] = __DECONST(char *, "-a");
648 errx(2, "too many file label options");
649 labels[nblabels++] = optarg;
685 if (caph_limit_stdio() == -1)
686 err(2, "unable to limit stdio");
688 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
694 e = malloc(2 * sizeof(struct kevent));
704 asprintf(&f1mark, "<<<<<<< %s",
705 labels[0] != NULL ? labels[0] : file1);
708 asprintf(&f2mark, "||||||| %s",
709 labels[1] != NULL ? labels[1] : file2);
712 asprintf(&f3mark, ">>>>>>> %s",
713 labels[2] != NULL ? labels[2] : file3);
717 fp[0] = fopen(file1, "r");
719 err(2, "Can't open %s", file1);
720 if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
721 err(2, "unable to limit rights on: %s", file1);
723 fp[1] = fopen(file2, "r");
725 err(2, "Can't open %s", file2);
726 if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
727 err(2, "unable to limit rights on: %s", file2);
729 fp[2] = fopen(file3, "r");
731 err(2, "Can't open %s", file3);
732 if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
733 err(2, "unable to limit rights on: %s", file3);
740 diffargv[diffargc] = file1;
741 diffargv[diffargc + 1] = file3;
742 diffargv[diffargc + 2] = NULL;
745 pd13 = diffexec(diffprog, diffargv, fd13);
746 EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
747 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
751 diffargv[diffargc] = file2;
752 pd23 = diffexec(diffprog, diffargv, fd23);
753 EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
754 if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
758 caph_cache_catpages();
759 if (caph_enter() < 0)
760 err(2, "unable to enter capability mode");
764 m = readin(fd13[0], &d13);
765 n = readin(fd23[0], &d23);
767 /* waitpid cooked over pdforks */
769 nke = kevent(kq, NULL, 0, e, nleft, NULL);
772 for (i = 0; i < nke; i++) {
774 if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
775 errx(2, "diff exited abormally");
776 else if (WIFSIGNALED(status))
777 errx(2, "diff killed by signal %d",
784 return (EXIT_SUCCESS);