xref: /titanic_51/usr/src/cmd/diff3/diff3prog.c (revision b369f4b871a39ef94e220443957975f445f52eb6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*b369f4b8Sjohnlev  * Common Development and Distribution License (the "License").
6*b369f4b8Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*b369f4b8Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * diff3 - 3-way differential file comparison
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * d13 = diff report on f1 vs f3
487c478bd9Sstevel@tonic-gate  * d23 = diff report on f2 vs f3
497c478bd9Sstevel@tonic-gate  * f1, f2, f3 the 3 files
507c478bd9Sstevel@tonic-gate  * if changes in f1 overlap with changes in f3, m1 and m3 are used
517c478bd9Sstevel@tonic-gate  * to mark the overlaps; otherwise, the file names f1 and f3 are used
527c478bd9Sstevel@tonic-gate  * (only for options E and X).
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate struct  range {int from, to; };
567c478bd9Sstevel@tonic-gate 	/*
577c478bd9Sstevel@tonic-gate 	 * from is first in range of changed lines
587c478bd9Sstevel@tonic-gate 	 * to is last+1
597c478bd9Sstevel@tonic-gate 	 * from = to = line after point of insertion
607c478bd9Sstevel@tonic-gate 	 * for added lines
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate struct diff {struct range old, new; };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	NC 4096
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * de is used to gather editing scripts,
677c478bd9Sstevel@tonic-gate  * that are later spewed out in reverse order.
687c478bd9Sstevel@tonic-gate  * its first element must be all zero
697c478bd9Sstevel@tonic-gate  * the "new" component of de contains line positions
707c478bd9Sstevel@tonic-gate  * or byte positions depending on when you look(!?)
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static struct diff d13[NC];
737c478bd9Sstevel@tonic-gate static struct diff d23[NC];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * array overlap indicates which sections in de correspond to
777c478bd9Sstevel@tonic-gate  * lines that are different in all three files.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static struct diff de[NC];
817c478bd9Sstevel@tonic-gate static char overlap[NC];
827c478bd9Sstevel@tonic-gate static int  overlapcnt = 0;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static char line[LINE_MAX+1];
857c478bd9Sstevel@tonic-gate static FILE *fp[3];
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  *	the number of the last-read line in each file
887c478bd9Sstevel@tonic-gate  *	is kept in cline[0-2]
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate static int cline[3];
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  *	the latest known correspondence between line
937c478bd9Sstevel@tonic-gate  *	numbers of the 3 files is stored in last[1-3]
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static int last[4];
967c478bd9Sstevel@tonic-gate static int eflag;
977c478bd9Sstevel@tonic-gate static int oflag;	/* indicates whether to mark overlaps (-E or -X) */
987c478bd9Sstevel@tonic-gate static int debug  = 0;
997c478bd9Sstevel@tonic-gate /* markers for -E and -X: */
1007c478bd9Sstevel@tonic-gate static char f1mark[8+MAXPATHLEN], f3mark[8+MAXPATHLEN];
1017c478bd9Sstevel@tonic-gate 		/* Need space for "<<<<<<< " or ">>>>>>> " plus filename   */
1027c478bd9Sstevel@tonic-gate static int save_err;	/* saves errno */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int readin(char *name, struct diff *dd);
1057c478bd9Sstevel@tonic-gate static int number(char **lc);
1067c478bd9Sstevel@tonic-gate static int digit(int c);
1077c478bd9Sstevel@tonic-gate static int getchange(FILE *b);
1087c478bd9Sstevel@tonic-gate static int getline(FILE *b);
1097c478bd9Sstevel@tonic-gate static void merge(int m1, int m2);
1107c478bd9Sstevel@tonic-gate static void separate(char *s);
1117c478bd9Sstevel@tonic-gate static void change(int i, struct range *rold, int dup);
1127c478bd9Sstevel@tonic-gate static void prange(struct range *rold);
1137c478bd9Sstevel@tonic-gate static void keep(int i, struct range *rnew);
1147c478bd9Sstevel@tonic-gate static int skip(int i, int from, char *pr);
1157c478bd9Sstevel@tonic-gate static int duplicate(struct range *r1, struct range *r2);
1167c478bd9Sstevel@tonic-gate static void repos(int nchar);
1177c478bd9Sstevel@tonic-gate static void trouble();
1187c478bd9Sstevel@tonic-gate static int edit(struct diff *diff, int dup, int j);
1197c478bd9Sstevel@tonic-gate static void edscript(int n);
120*b369f4b8Sjohnlev static void usage();
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
1237c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int i, m, n;
1267c478bd9Sstevel@tonic-gate 	eflag  = 0;
1277c478bd9Sstevel@tonic-gate 	oflag  = 0;
128*b369f4b8Sjohnlev 	if ((argc > 1) && (*argv[1] == '-')) {
1297c478bd9Sstevel@tonic-gate 		switch (argv[1][1]) {
130*b369f4b8Sjohnlev 		case 'e':
1317c478bd9Sstevel@tonic-gate 			eflag = 3;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		case '3':
1347c478bd9Sstevel@tonic-gate 			eflag = 2;
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case 'x':
1377c478bd9Sstevel@tonic-gate 			eflag = 1;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case 'E':
1407c478bd9Sstevel@tonic-gate 			eflag = 3;
1417c478bd9Sstevel@tonic-gate 			oflag = 1;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		case 'X':
1447c478bd9Sstevel@tonic-gate 			oflag = eflag = 1;
1457c478bd9Sstevel@tonic-gate 			break;
146*b369f4b8Sjohnlev 		default:
147*b369f4b8Sjohnlev 			usage();
148*b369f4b8Sjohnlev 			break;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		argv++;
1517c478bd9Sstevel@tonic-gate 		argc--;
1527c478bd9Sstevel@tonic-gate 	}
153*b369f4b8Sjohnlev 	if (argc < 6)
154*b369f4b8Sjohnlev 		usage();
1557c478bd9Sstevel@tonic-gate 	if (oflag) {
1567c478bd9Sstevel@tonic-gate 		(void) snprintf(f1mark, sizeof (f1mark), "<<<<<<< %s",
1577c478bd9Sstevel@tonic-gate 						argc >= 7 ? argv[6] : argv[3]);
1587c478bd9Sstevel@tonic-gate 		(void) snprintf(f3mark, sizeof (f3mark), ">>>>>>> %s",
1597c478bd9Sstevel@tonic-gate 						argc >= 8 ? argv[7] : argv[5]);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	m = readin(argv[1], d13);
1637c478bd9Sstevel@tonic-gate 	n = readin(argv[2], d23);
1647c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 2; i++)
1657c478bd9Sstevel@tonic-gate 		if ((fp[i] = fopen(argv[i+3], "r")) == NULL) {
1667c478bd9Sstevel@tonic-gate 			save_err = errno;
1677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "diff3: can't open %s: ",
1687c478bd9Sstevel@tonic-gate 				argv[i+3]);
1697c478bd9Sstevel@tonic-gate 			errno = save_err;
1707c478bd9Sstevel@tonic-gate 			perror("");
1717c478bd9Sstevel@tonic-gate 			exit(1);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	merge(m, n);
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * pick up the line numbers of all changes from
1797c478bd9Sstevel@tonic-gate  * one change file
1807c478bd9Sstevel@tonic-gate  * (this puts the numbers in a vector, which is not
1817c478bd9Sstevel@tonic-gate  * strictly necessary, since the vector is processed
1827c478bd9Sstevel@tonic-gate  * in one sequential pass. The vector could be optimized
1837c478bd9Sstevel@tonic-gate  * out of existence)
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static int
1877c478bd9Sstevel@tonic-gate readin(char *name, struct diff *dd)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int i;
1907c478bd9Sstevel@tonic-gate 	int a, b, c, d;
1917c478bd9Sstevel@tonic-gate 	char kind;
1927c478bd9Sstevel@tonic-gate 	char *p;
1937c478bd9Sstevel@tonic-gate 	if ((fp[0] = fopen(name, "r")) == NULL) {
1947c478bd9Sstevel@tonic-gate 		save_err = errno;
1957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "diff3: can't open %s: ", name);
1967c478bd9Sstevel@tonic-gate 		errno = save_err;
1977c478bd9Sstevel@tonic-gate 		perror("");
1987c478bd9Sstevel@tonic-gate 		exit(1);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	for (i = 0; getchange(fp[0]); i++) {
2017c478bd9Sstevel@tonic-gate 		if (i >= NC) {
2027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "diff3: too many changes\n");
2037c478bd9Sstevel@tonic-gate 			exit(0);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		p = line;
2067c478bd9Sstevel@tonic-gate 		a = b = number(&p);
2077c478bd9Sstevel@tonic-gate 		if (*p == ',') {
2087c478bd9Sstevel@tonic-gate 			p++;
2097c478bd9Sstevel@tonic-gate 			b = number(&p);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 		kind = *p++;
2127c478bd9Sstevel@tonic-gate 		c = d = number(&p);
2137c478bd9Sstevel@tonic-gate 		if (*p == ',') {
2147c478bd9Sstevel@tonic-gate 			p++;
2157c478bd9Sstevel@tonic-gate 			d = number(&p);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		if (kind == 'a')
2187c478bd9Sstevel@tonic-gate 			a++;
2197c478bd9Sstevel@tonic-gate 		if (kind == 'd')
2207c478bd9Sstevel@tonic-gate 			c++;
2217c478bd9Sstevel@tonic-gate 		b++;
2227c478bd9Sstevel@tonic-gate 		d++;
2237c478bd9Sstevel@tonic-gate 		dd[i].old.from = a;
2247c478bd9Sstevel@tonic-gate 		dd[i].old.to = b;
2257c478bd9Sstevel@tonic-gate 		dd[i].new.from = c;
2267c478bd9Sstevel@tonic-gate 		dd[i].new.to = d;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	dd[i].old.from = dd[i-1].old.to;
2297c478bd9Sstevel@tonic-gate 	dd[i].new.from = dd[i-1].new.to;
2307c478bd9Sstevel@tonic-gate 	(void) fclose(fp[0]);
2317c478bd9Sstevel@tonic-gate 	return (i);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static int
2357c478bd9Sstevel@tonic-gate number(char **lc)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	int nn;
2387c478bd9Sstevel@tonic-gate 	nn = 0;
2397c478bd9Sstevel@tonic-gate 	while (digit(**lc))
2407c478bd9Sstevel@tonic-gate 		nn = nn*10 + *(*lc)++ - '0';
2417c478bd9Sstevel@tonic-gate 	return (nn);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int
2457c478bd9Sstevel@tonic-gate digit(int c)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	return (c >= '0' && c <= '9');
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static int
2517c478bd9Sstevel@tonic-gate getchange(FILE *b)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	while (getline(b))
2547c478bd9Sstevel@tonic-gate 		if (digit(line[0]))
2557c478bd9Sstevel@tonic-gate 			return (1);
2567c478bd9Sstevel@tonic-gate 	return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate getline(FILE *b)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	int i, c;
2637c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (line)-1; i++) {
2647c478bd9Sstevel@tonic-gate 		c = getc(b);
2657c478bd9Sstevel@tonic-gate 		if (c == EOF) {
2667c478bd9Sstevel@tonic-gate 			line[i] = 0;
2677c478bd9Sstevel@tonic-gate 			return (i);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		line[i] = c;
2707c478bd9Sstevel@tonic-gate 		if (c == '\n') {
2717c478bd9Sstevel@tonic-gate 			line[++i] = 0;
2727c478bd9Sstevel@tonic-gate 			return (i);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	return (0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate static void
2797c478bd9Sstevel@tonic-gate merge(int m1, int m2)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	struct diff *d1, *d2, *d3;
2827c478bd9Sstevel@tonic-gate 	int dup;
2837c478bd9Sstevel@tonic-gate 	int j;
2847c478bd9Sstevel@tonic-gate 	int t1, t2;
2857c478bd9Sstevel@tonic-gate 	d1 = d13;
2867c478bd9Sstevel@tonic-gate 	d2 = d23;
2877c478bd9Sstevel@tonic-gate 	j = 0;
2887c478bd9Sstevel@tonic-gate 	for (; (t1 = d1 < d13+m1) | (t2 = d2 < d23+m2); ) {
2897c478bd9Sstevel@tonic-gate 		if (debug) {
2907c478bd9Sstevel@tonic-gate 			(void) printf("%d,%d=%d,%d %d,%d=%d,%d\n",
2917c478bd9Sstevel@tonic-gate 			d1->old.from, d1->old.to,
2927c478bd9Sstevel@tonic-gate 			d1->new.from, d1->new.to,
2937c478bd9Sstevel@tonic-gate 			d2->old.from, d2->old.to,
2947c478bd9Sstevel@tonic-gate 			d2->new.from, d2->new.to);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/* first file is different from others */
2987c478bd9Sstevel@tonic-gate 		if (!t2 || t1 && d1->new.to < d2->new.from) {
2997c478bd9Sstevel@tonic-gate 			/* stuff peculiar to 1st file */
3007c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
3017c478bd9Sstevel@tonic-gate 				separate("1");
3027c478bd9Sstevel@tonic-gate 				change(1, &d1->old, 0);
3037c478bd9Sstevel@tonic-gate 				keep(2, &d1->new);
3047c478bd9Sstevel@tonic-gate 				change(3, &d1->new, 0);
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 			d1++;
3077c478bd9Sstevel@tonic-gate 			continue;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		/* second file is different from others */
3117c478bd9Sstevel@tonic-gate 		if (!t1 || t2 && d2->new.to < d1->new.from) {
3127c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
3137c478bd9Sstevel@tonic-gate 				separate("2");
3147c478bd9Sstevel@tonic-gate 				keep(1, &d2->new);
3157c478bd9Sstevel@tonic-gate 				change(2, &d2->old, 0);
3167c478bd9Sstevel@tonic-gate 				change(3, &d2->new, 0);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			d2++;
3197c478bd9Sstevel@tonic-gate 			continue;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * merge overlapping changes in first file
3237c478bd9Sstevel@tonic-gate 		 * this happens after extension see below
3247c478bd9Sstevel@tonic-gate 		 */
3257c478bd9Sstevel@tonic-gate 		if (d1+1 < d13+m1 && d1->new.to >= d1[1].new.from) {
3267c478bd9Sstevel@tonic-gate 			d1[1].old.from = d1->old.from;
3277c478bd9Sstevel@tonic-gate 			d1[1].new.from = d1->new.from;
3287c478bd9Sstevel@tonic-gate 			d1++;
3297c478bd9Sstevel@tonic-gate 			continue;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		/* merge overlapping changes in second */
3337c478bd9Sstevel@tonic-gate 		if (d2+1 < d23+m2 && d2->new.to >= d2[1].new.from) {
3347c478bd9Sstevel@tonic-gate 			d2[1].old.from = d2->old.from;
3357c478bd9Sstevel@tonic-gate 			d2[1].new.from = d2->new.from;
3367c478bd9Sstevel@tonic-gate 			d2++;
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		/* stuff peculiar to third file or different in all */
3417c478bd9Sstevel@tonic-gate 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
3427c478bd9Sstevel@tonic-gate 			dup = duplicate(&d1->old, &d2->old);
3437c478bd9Sstevel@tonic-gate 			/*
3447c478bd9Sstevel@tonic-gate 			 * dup = 0 means all files differ
3457c478bd9Sstevel@tonic-gate 			 * dup = 1 meands files 1&2 identical
3467c478bd9Sstevel@tonic-gate 			 */
3477c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
3487c478bd9Sstevel@tonic-gate 				separate(dup?"3":"");
3497c478bd9Sstevel@tonic-gate 				change(1, &d1->old, dup);
3507c478bd9Sstevel@tonic-gate 				change(2, &d2->old, 0);
3517c478bd9Sstevel@tonic-gate 				d3 = d1->old.to > d1->old.from ? d1 : d2;
3527c478bd9Sstevel@tonic-gate 				change(3, &d3->new, 0);
3537c478bd9Sstevel@tonic-gate 			} else
3547c478bd9Sstevel@tonic-gate 				j = edit(d1, dup, j);
3557c478bd9Sstevel@tonic-gate 			d1++;
3567c478bd9Sstevel@tonic-gate 			d2++;
3577c478bd9Sstevel@tonic-gate 			continue;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * overlapping changes from file1 & 2
3617c478bd9Sstevel@tonic-gate 		 * extend changes appropriately to
3627c478bd9Sstevel@tonic-gate 		 * make them coincide
3637c478bd9Sstevel@tonic-gate 		 */
3647c478bd9Sstevel@tonic-gate 		if (d1->new.from < d2->new.from) {
3657c478bd9Sstevel@tonic-gate 			d2->old.from -= d2->new.from-d1->new.from;
3667c478bd9Sstevel@tonic-gate 			d2->new.from = d1->new.from;
3677c478bd9Sstevel@tonic-gate 		} else if (d2->new.from < d1->new.from) {
3687c478bd9Sstevel@tonic-gate 			d1->old.from -= d1->new.from-d2->new.from;
3697c478bd9Sstevel@tonic-gate 			d1->new.from = d2->new.from;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (d1->new.to > d2->new.to) {
3737c478bd9Sstevel@tonic-gate 			d2->old.to += d1->new.to - d2->new.to;
3747c478bd9Sstevel@tonic-gate 			d2->new.to = d1->new.to;
3757c478bd9Sstevel@tonic-gate 		} else if (d2->new.to > d1->new.to) {
3767c478bd9Sstevel@tonic-gate 			d1->old.to += d2->new.to - d1->new.to;
3777c478bd9Sstevel@tonic-gate 			d1->new.to = d2->new.to;
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	if (eflag) {
3817c478bd9Sstevel@tonic-gate 		edscript(j);
3827c478bd9Sstevel@tonic-gate 		if (j)
3837c478bd9Sstevel@tonic-gate 			(void) printf("w\nq\n");
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate separate(char *s)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	(void) printf("====%s\n", s);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * the range of ines rold.from thru rold.to in file i
3957c478bd9Sstevel@tonic-gate  * is to be changed. it is to be printed only if
3967c478bd9Sstevel@tonic-gate  * it does not duplicate something to be printed later
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static void
3997c478bd9Sstevel@tonic-gate change(int i, struct range *rold, int dup)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	(void) printf("%d:", i);
4027c478bd9Sstevel@tonic-gate 	last[i] = rold->to;
4037c478bd9Sstevel@tonic-gate 	prange(rold);
4047c478bd9Sstevel@tonic-gate 	if (dup)
4057c478bd9Sstevel@tonic-gate 		return;
4067c478bd9Sstevel@tonic-gate 	if (debug)
4077c478bd9Sstevel@tonic-gate 		return;
4087c478bd9Sstevel@tonic-gate 	i--;
4097c478bd9Sstevel@tonic-gate 	(void) skip(i, rold->from, (char *)0);
4107c478bd9Sstevel@tonic-gate 	(void) skip(i, rold->to, "  ");
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * print the range of line numbers, rold.from  thru rold.to
4157c478bd9Sstevel@tonic-gate  * as n1, n2 or n1
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate static void
4187c478bd9Sstevel@tonic-gate prange(struct range *rold)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	if (rold->to <= rold->from)
4217c478bd9Sstevel@tonic-gate 		(void) printf("%da\n", rold->from-1);
4227c478bd9Sstevel@tonic-gate 	else {
4237c478bd9Sstevel@tonic-gate 		(void) printf("%d", rold->from);
4247c478bd9Sstevel@tonic-gate 		if (rold->to > rold->from+1)
4257c478bd9Sstevel@tonic-gate 			(void) printf(",%d", rold->to-1);
4267c478bd9Sstevel@tonic-gate 		(void) printf("c\n");
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * no difference was reported by diff between file 1(or 2)
4327c478bd9Sstevel@tonic-gate  * and file 3, and an artificial dummy difference (trange)
4337c478bd9Sstevel@tonic-gate  * must be ginned up to correspond to the change reported
4347c478bd9Sstevel@tonic-gate  * in the other file
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static void
4377c478bd9Sstevel@tonic-gate keep(int i, struct range *rnew)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	int delta;
4407c478bd9Sstevel@tonic-gate 	struct range trange;
4417c478bd9Sstevel@tonic-gate 	delta = last[3] - last[i];
4427c478bd9Sstevel@tonic-gate 	trange.from = rnew->from - delta;
4437c478bd9Sstevel@tonic-gate 	trange.to = rnew->to - delta;
4447c478bd9Sstevel@tonic-gate 	change(i, &trange, 1);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * skip to just befor line number from in file i
4497c478bd9Sstevel@tonic-gate  * if "pr" is nonzero, print all skipped stuff
4507c478bd9Sstevel@tonic-gate  * with string pr as a prefix
4517c478bd9Sstevel@tonic-gate  */
4527c478bd9Sstevel@tonic-gate static int
4537c478bd9Sstevel@tonic-gate skip(int i, int from, char *pr)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	int j, n;
4567c478bd9Sstevel@tonic-gate 	for (n = 0; cline[i] < from-1; n += j) {
4577c478bd9Sstevel@tonic-gate 		if ((j = getline(fp[i])) == 0)
4587c478bd9Sstevel@tonic-gate 			trouble();
4597c478bd9Sstevel@tonic-gate 		if (pr)
4607c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", pr, line);
4617c478bd9Sstevel@tonic-gate 		cline[i]++;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 	return (n);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * return 1 or 0 according as the old range
4687c478bd9Sstevel@tonic-gate  * (in file 1) contains exactly the same data
4697c478bd9Sstevel@tonic-gate  * as the new range (in file 2)
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate static int
4727c478bd9Sstevel@tonic-gate duplicate(struct range *r1, struct range *r2)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	int c, d;
4757c478bd9Sstevel@tonic-gate 	int nchar;
4767c478bd9Sstevel@tonic-gate 	int nline;
4777c478bd9Sstevel@tonic-gate 	if (r1->to-r1->from != r2->to-r2->from)
4787c478bd9Sstevel@tonic-gate 		return (0);
4797c478bd9Sstevel@tonic-gate 	(void) skip(0, r1->from, (char *)0);
4807c478bd9Sstevel@tonic-gate 	(void) skip(1, r2->from, (char *)0);
4817c478bd9Sstevel@tonic-gate 	nchar = 0;
4827c478bd9Sstevel@tonic-gate 	for (nline = 0; nline < r1->to-r1->from; nline++) {
4837c478bd9Sstevel@tonic-gate 		do {
4847c478bd9Sstevel@tonic-gate 			c = getc(fp[0]);
4857c478bd9Sstevel@tonic-gate 			d = getc(fp[1]);
4867c478bd9Sstevel@tonic-gate 			if (c == -1 || d == -1)
4877c478bd9Sstevel@tonic-gate 				trouble();
4887c478bd9Sstevel@tonic-gate 			nchar++;
4897c478bd9Sstevel@tonic-gate 			if (c != d) {
4907c478bd9Sstevel@tonic-gate 				repos(nchar);
4917c478bd9Sstevel@tonic-gate 				return (0);
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 		} while (c != '\n');
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	repos(nchar);
4967c478bd9Sstevel@tonic-gate 	return (1);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static void
5007c478bd9Sstevel@tonic-gate repos(int nchar)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	int i;
5037c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++)
5047c478bd9Sstevel@tonic-gate 		(void) fseek(fp[i], (long)-nchar, 1);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static void
5087c478bd9Sstevel@tonic-gate trouble()
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "diff3: logic error\n");
5117c478bd9Sstevel@tonic-gate 	abort();
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * collect an editing script for later regurgitation
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate static int
5187c478bd9Sstevel@tonic-gate edit(struct diff *diff, int dup, int j)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	if (((dup+1)&eflag) == 0)
5217c478bd9Sstevel@tonic-gate 		return (j);
5227c478bd9Sstevel@tonic-gate 	j++;
5237c478bd9Sstevel@tonic-gate 	overlap[j] = !dup;
5247c478bd9Sstevel@tonic-gate 	if (!dup) overlapcnt++;
5257c478bd9Sstevel@tonic-gate 	de[j].old.from = diff->old.from;
5267c478bd9Sstevel@tonic-gate 	de[j].old.to = diff->old.to;
5277c478bd9Sstevel@tonic-gate 	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, (char *)0);
5287c478bd9Sstevel@tonic-gate 	de[j].new.to = de[j].new.from + skip(2, diff->new.to, (char *)0);
5297c478bd9Sstevel@tonic-gate 	return (j);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*		regurgitate */
5337c478bd9Sstevel@tonic-gate static void
5347c478bd9Sstevel@tonic-gate edscript(int n)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	int j, k;
5377c478bd9Sstevel@tonic-gate 	char	 block[BUFSIZ];
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	for (n = n; n > 0; n--) {
5407c478bd9Sstevel@tonic-gate 		if (!oflag || !overlap[n])
5417c478bd9Sstevel@tonic-gate 			prange(&de[n].old);
5427c478bd9Sstevel@tonic-gate 		else
5437c478bd9Sstevel@tonic-gate 			(void) printf("%da\n=======\n", de[n].old.to -1);
5447c478bd9Sstevel@tonic-gate 		(void) fseek(fp[2], (long)de[n].new.from, 0);
5457c478bd9Sstevel@tonic-gate 		for (k = de[n].new.to-de[n].new.from; k > 0; k -= j) {
5467c478bd9Sstevel@tonic-gate 			j = k > BUFSIZ?BUFSIZ:k;
5477c478bd9Sstevel@tonic-gate 			if (fread(block, 1, j, fp[2]) != j)
5487c478bd9Sstevel@tonic-gate 				trouble();
5497c478bd9Sstevel@tonic-gate 			(void) fwrite(block, 1, j, stdout);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		if (!oflag || !overlap[n])
5527c478bd9Sstevel@tonic-gate 			(void) printf(".\n");
5537c478bd9Sstevel@tonic-gate 		else {
5547c478bd9Sstevel@tonic-gate 			(void) printf("%s\n.\n", f3mark);
5557c478bd9Sstevel@tonic-gate 			(void) printf("%da\n%s\n.\n", de[n].old.from-1, f1mark);
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate }
559*b369f4b8Sjohnlev 
560*b369f4b8Sjohnlev static void
561*b369f4b8Sjohnlev usage()
562*b369f4b8Sjohnlev {
563*b369f4b8Sjohnlev 	(void) fprintf(stderr,
564*b369f4b8Sjohnlev 	    "\tusage: diff3prog [-ex3EX] d13 d23 f1 f2 f3 [m1 m2]\n");
565*b369f4b8Sjohnlev 	exit(1);
566*b369f4b8Sjohnlev }
567