10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51969Sjohnlev * Common Development and Distribution License (the "License"). 61969Sjohnlev * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate 220Sstevel@tonic-gate /* 23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 26*13093SRoger.Faulkner@Oracle.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*13093SRoger.Faulkner@Oracle.COM /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <unistd.h> 320Sstevel@tonic-gate #include <limits.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <errno.h> 350Sstevel@tonic-gate # 360Sstevel@tonic-gate 370Sstevel@tonic-gate /* 380Sstevel@tonic-gate * diff3 - 3-way differential file comparison 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3] 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * d13 = diff report on f1 vs f3 450Sstevel@tonic-gate * d23 = diff report on f2 vs f3 460Sstevel@tonic-gate * f1, f2, f3 the 3 files 470Sstevel@tonic-gate * if changes in f1 overlap with changes in f3, m1 and m3 are used 480Sstevel@tonic-gate * to mark the overlaps; otherwise, the file names f1 and f3 are used 490Sstevel@tonic-gate * (only for options E and X). 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate struct range {int from, to; }; 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * from is first in range of changed lines 550Sstevel@tonic-gate * to is last+1 560Sstevel@tonic-gate * from = to = line after point of insertion 570Sstevel@tonic-gate * for added lines 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate struct diff {struct range old, new; }; 600Sstevel@tonic-gate 610Sstevel@tonic-gate #define NC 4096 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * de is used to gather editing scripts, 640Sstevel@tonic-gate * that are later spewed out in reverse order. 650Sstevel@tonic-gate * its first element must be all zero 660Sstevel@tonic-gate * the "new" component of de contains line positions 670Sstevel@tonic-gate * or byte positions depending on when you look(!?) 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate static struct diff d13[NC]; 700Sstevel@tonic-gate static struct diff d23[NC]; 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * array overlap indicates which sections in de correspond to 740Sstevel@tonic-gate * lines that are different in all three files. 750Sstevel@tonic-gate */ 760Sstevel@tonic-gate 770Sstevel@tonic-gate static struct diff de[NC]; 780Sstevel@tonic-gate static char overlap[NC]; 790Sstevel@tonic-gate static int overlapcnt = 0; 800Sstevel@tonic-gate 810Sstevel@tonic-gate static char line[LINE_MAX+1]; 820Sstevel@tonic-gate static FILE *fp[3]; 830Sstevel@tonic-gate /* 840Sstevel@tonic-gate * the number of the last-read line in each file 850Sstevel@tonic-gate * is kept in cline[0-2] 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate static int cline[3]; 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * the latest known correspondence between line 900Sstevel@tonic-gate * numbers of the 3 files is stored in last[1-3] 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate static int last[4]; 930Sstevel@tonic-gate static int eflag; 940Sstevel@tonic-gate static int oflag; /* indicates whether to mark overlaps (-E or -X) */ 950Sstevel@tonic-gate static int debug = 0; 960Sstevel@tonic-gate /* markers for -E and -X: */ 970Sstevel@tonic-gate static char f1mark[8+MAXPATHLEN], f3mark[8+MAXPATHLEN]; 980Sstevel@tonic-gate /* Need space for "<<<<<<< " or ">>>>>>> " plus filename */ 990Sstevel@tonic-gate static int save_err; /* saves errno */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static int readin(char *name, struct diff *dd); 1020Sstevel@tonic-gate static int number(char **lc); 1030Sstevel@tonic-gate static int digit(int c); 1040Sstevel@tonic-gate static int getchange(FILE *b); 105*13093SRoger.Faulkner@Oracle.COM static int getaline(FILE *b); 1060Sstevel@tonic-gate static void merge(int m1, int m2); 1070Sstevel@tonic-gate static void separate(char *s); 1080Sstevel@tonic-gate static void change(int i, struct range *rold, int dup); 1090Sstevel@tonic-gate static void prange(struct range *rold); 1100Sstevel@tonic-gate static void keep(int i, struct range *rnew); 1110Sstevel@tonic-gate static int skip(int i, int from, char *pr); 1120Sstevel@tonic-gate static int duplicate(struct range *r1, struct range *r2); 1130Sstevel@tonic-gate static void repos(int nchar); 1140Sstevel@tonic-gate static void trouble(); 1150Sstevel@tonic-gate static int edit(struct diff *diff, int dup, int j); 1160Sstevel@tonic-gate static void edscript(int n); 1171969Sjohnlev static void usage(); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate int 1200Sstevel@tonic-gate main(int argc, char **argv) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate int i, m, n; 1230Sstevel@tonic-gate eflag = 0; 1240Sstevel@tonic-gate oflag = 0; 1251969Sjohnlev if ((argc > 1) && (*argv[1] == '-')) { 1260Sstevel@tonic-gate switch (argv[1][1]) { 1271969Sjohnlev case 'e': 1280Sstevel@tonic-gate eflag = 3; 1290Sstevel@tonic-gate break; 1300Sstevel@tonic-gate case '3': 1310Sstevel@tonic-gate eflag = 2; 1320Sstevel@tonic-gate break; 1330Sstevel@tonic-gate case 'x': 1340Sstevel@tonic-gate eflag = 1; 1350Sstevel@tonic-gate break; 1360Sstevel@tonic-gate case 'E': 1370Sstevel@tonic-gate eflag = 3; 1380Sstevel@tonic-gate oflag = 1; 1390Sstevel@tonic-gate break; 1400Sstevel@tonic-gate case 'X': 1410Sstevel@tonic-gate oflag = eflag = 1; 1420Sstevel@tonic-gate break; 1431969Sjohnlev default: 1441969Sjohnlev usage(); 1451969Sjohnlev break; 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate argv++; 1480Sstevel@tonic-gate argc--; 1490Sstevel@tonic-gate } 1501969Sjohnlev if (argc < 6) 1511969Sjohnlev usage(); 1520Sstevel@tonic-gate if (oflag) { 1530Sstevel@tonic-gate (void) snprintf(f1mark, sizeof (f1mark), "<<<<<<< %s", 154*13093SRoger.Faulkner@Oracle.COM argc >= 7 ? argv[6] : argv[3]); 1550Sstevel@tonic-gate (void) snprintf(f3mark, sizeof (f3mark), ">>>>>>> %s", 156*13093SRoger.Faulkner@Oracle.COM argc >= 8 ? argv[7] : argv[5]); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate m = readin(argv[1], d13); 1600Sstevel@tonic-gate n = readin(argv[2], d23); 1610Sstevel@tonic-gate for (i = 0; i <= 2; i++) 1620Sstevel@tonic-gate if ((fp[i] = fopen(argv[i+3], "r")) == NULL) { 1630Sstevel@tonic-gate save_err = errno; 1640Sstevel@tonic-gate (void) fprintf(stderr, "diff3: can't open %s: ", 165*13093SRoger.Faulkner@Oracle.COM argv[i+3]); 1660Sstevel@tonic-gate errno = save_err; 1670Sstevel@tonic-gate perror(""); 1680Sstevel@tonic-gate exit(1); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate merge(m, n); 1710Sstevel@tonic-gate return (0); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * pick up the line numbers of all changes from 1760Sstevel@tonic-gate * one change file 1770Sstevel@tonic-gate * (this puts the numbers in a vector, which is not 1780Sstevel@tonic-gate * strictly necessary, since the vector is processed 1790Sstevel@tonic-gate * in one sequential pass. The vector could be optimized 1800Sstevel@tonic-gate * out of existence) 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate static int 1840Sstevel@tonic-gate readin(char *name, struct diff *dd) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate int i; 1870Sstevel@tonic-gate int a, b, c, d; 1880Sstevel@tonic-gate char kind; 1890Sstevel@tonic-gate char *p; 1900Sstevel@tonic-gate if ((fp[0] = fopen(name, "r")) == NULL) { 1910Sstevel@tonic-gate save_err = errno; 1920Sstevel@tonic-gate (void) fprintf(stderr, "diff3: can't open %s: ", name); 1930Sstevel@tonic-gate errno = save_err; 1940Sstevel@tonic-gate perror(""); 1950Sstevel@tonic-gate exit(1); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate for (i = 0; getchange(fp[0]); i++) { 1980Sstevel@tonic-gate if (i >= NC) { 1990Sstevel@tonic-gate (void) fprintf(stderr, "diff3: too many changes\n"); 2000Sstevel@tonic-gate exit(0); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate p = line; 2030Sstevel@tonic-gate a = b = number(&p); 2040Sstevel@tonic-gate if (*p == ',') { 2050Sstevel@tonic-gate p++; 2060Sstevel@tonic-gate b = number(&p); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate kind = *p++; 2090Sstevel@tonic-gate c = d = number(&p); 2100Sstevel@tonic-gate if (*p == ',') { 2110Sstevel@tonic-gate p++; 2120Sstevel@tonic-gate d = number(&p); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate if (kind == 'a') 2150Sstevel@tonic-gate a++; 2160Sstevel@tonic-gate if (kind == 'd') 2170Sstevel@tonic-gate c++; 2180Sstevel@tonic-gate b++; 2190Sstevel@tonic-gate d++; 2200Sstevel@tonic-gate dd[i].old.from = a; 2210Sstevel@tonic-gate dd[i].old.to = b; 2220Sstevel@tonic-gate dd[i].new.from = c; 2230Sstevel@tonic-gate dd[i].new.to = d; 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate dd[i].old.from = dd[i-1].old.to; 2260Sstevel@tonic-gate dd[i].new.from = dd[i-1].new.to; 2270Sstevel@tonic-gate (void) fclose(fp[0]); 2280Sstevel@tonic-gate return (i); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate static int 2320Sstevel@tonic-gate number(char **lc) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate int nn; 2350Sstevel@tonic-gate nn = 0; 2360Sstevel@tonic-gate while (digit(**lc)) 2370Sstevel@tonic-gate nn = nn*10 + *(*lc)++ - '0'; 2380Sstevel@tonic-gate return (nn); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static int 2420Sstevel@tonic-gate digit(int c) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate return (c >= '0' && c <= '9'); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate static int 2480Sstevel@tonic-gate getchange(FILE *b) 2490Sstevel@tonic-gate { 250*13093SRoger.Faulkner@Oracle.COM while (getaline(b)) 2510Sstevel@tonic-gate if (digit(line[0])) 2520Sstevel@tonic-gate return (1); 2530Sstevel@tonic-gate return (0); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate static int 257*13093SRoger.Faulkner@Oracle.COM getaline(FILE *b) 2580Sstevel@tonic-gate { 2590Sstevel@tonic-gate int i, c; 2600Sstevel@tonic-gate for (i = 0; i < sizeof (line)-1; i++) { 2610Sstevel@tonic-gate c = getc(b); 2620Sstevel@tonic-gate if (c == EOF) { 2630Sstevel@tonic-gate line[i] = 0; 2640Sstevel@tonic-gate return (i); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate line[i] = c; 2670Sstevel@tonic-gate if (c == '\n') { 2680Sstevel@tonic-gate line[++i] = 0; 2690Sstevel@tonic-gate return (i); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate return (0); 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate static void 2760Sstevel@tonic-gate merge(int m1, int m2) 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate struct diff *d1, *d2, *d3; 2790Sstevel@tonic-gate int dup; 2800Sstevel@tonic-gate int j; 2810Sstevel@tonic-gate int t1, t2; 2820Sstevel@tonic-gate d1 = d13; 2830Sstevel@tonic-gate d2 = d23; 2840Sstevel@tonic-gate j = 0; 2850Sstevel@tonic-gate for (; (t1 = d1 < d13+m1) | (t2 = d2 < d23+m2); ) { 2860Sstevel@tonic-gate if (debug) { 2870Sstevel@tonic-gate (void) printf("%d,%d=%d,%d %d,%d=%d,%d\n", 288*13093SRoger.Faulkner@Oracle.COM d1->old.from, d1->old.to, 289*13093SRoger.Faulkner@Oracle.COM d1->new.from, d1->new.to, 290*13093SRoger.Faulkner@Oracle.COM d2->old.from, d2->old.to, 291*13093SRoger.Faulkner@Oracle.COM d2->new.from, d2->new.to); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* first file is different from others */ 2950Sstevel@tonic-gate if (!t2 || t1 && d1->new.to < d2->new.from) { 2960Sstevel@tonic-gate /* stuff peculiar to 1st file */ 2970Sstevel@tonic-gate if (eflag == 0) { 2980Sstevel@tonic-gate separate("1"); 2990Sstevel@tonic-gate change(1, &d1->old, 0); 3000Sstevel@tonic-gate keep(2, &d1->new); 3010Sstevel@tonic-gate change(3, &d1->new, 0); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate d1++; 3040Sstevel@tonic-gate continue; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* second file is different from others */ 3080Sstevel@tonic-gate if (!t1 || t2 && d2->new.to < d1->new.from) { 3090Sstevel@tonic-gate if (eflag == 0) { 3100Sstevel@tonic-gate separate("2"); 3110Sstevel@tonic-gate keep(1, &d2->new); 3120Sstevel@tonic-gate change(2, &d2->old, 0); 3130Sstevel@tonic-gate change(3, &d2->new, 0); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate d2++; 3160Sstevel@tonic-gate continue; 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * merge overlapping changes in first file 3200Sstevel@tonic-gate * this happens after extension see below 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate if (d1+1 < d13+m1 && d1->new.to >= d1[1].new.from) { 3230Sstevel@tonic-gate d1[1].old.from = d1->old.from; 3240Sstevel@tonic-gate d1[1].new.from = d1->new.from; 3250Sstevel@tonic-gate d1++; 3260Sstevel@tonic-gate continue; 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate /* merge overlapping changes in second */ 3300Sstevel@tonic-gate if (d2+1 < d23+m2 && d2->new.to >= d2[1].new.from) { 3310Sstevel@tonic-gate d2[1].old.from = d2->old.from; 3320Sstevel@tonic-gate d2[1].new.from = d2->new.from; 3330Sstevel@tonic-gate d2++; 3340Sstevel@tonic-gate continue; 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* stuff peculiar to third file or different in all */ 3380Sstevel@tonic-gate if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) { 3390Sstevel@tonic-gate dup = duplicate(&d1->old, &d2->old); 3400Sstevel@tonic-gate /* 3410Sstevel@tonic-gate * dup = 0 means all files differ 3420Sstevel@tonic-gate * dup = 1 meands files 1&2 identical 3430Sstevel@tonic-gate */ 3440Sstevel@tonic-gate if (eflag == 0) { 3450Sstevel@tonic-gate separate(dup?"3":""); 3460Sstevel@tonic-gate change(1, &d1->old, dup); 3470Sstevel@tonic-gate change(2, &d2->old, 0); 3480Sstevel@tonic-gate d3 = d1->old.to > d1->old.from ? d1 : d2; 3490Sstevel@tonic-gate change(3, &d3->new, 0); 3500Sstevel@tonic-gate } else 3510Sstevel@tonic-gate j = edit(d1, dup, j); 3520Sstevel@tonic-gate d1++; 3530Sstevel@tonic-gate d2++; 3540Sstevel@tonic-gate continue; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * overlapping changes from file1 & 2 3580Sstevel@tonic-gate * extend changes appropriately to 3590Sstevel@tonic-gate * make them coincide 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate if (d1->new.from < d2->new.from) { 3620Sstevel@tonic-gate d2->old.from -= d2->new.from-d1->new.from; 3630Sstevel@tonic-gate d2->new.from = d1->new.from; 3640Sstevel@tonic-gate } else if (d2->new.from < d1->new.from) { 3650Sstevel@tonic-gate d1->old.from -= d1->new.from-d2->new.from; 3660Sstevel@tonic-gate d1->new.from = d2->new.from; 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate if (d1->new.to > d2->new.to) { 3700Sstevel@tonic-gate d2->old.to += d1->new.to - d2->new.to; 3710Sstevel@tonic-gate d2->new.to = d1->new.to; 3720Sstevel@tonic-gate } else if (d2->new.to > d1->new.to) { 3730Sstevel@tonic-gate d1->old.to += d2->new.to - d1->new.to; 3740Sstevel@tonic-gate d1->new.to = d2->new.to; 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate if (eflag) { 3780Sstevel@tonic-gate edscript(j); 3790Sstevel@tonic-gate if (j) 3800Sstevel@tonic-gate (void) printf("w\nq\n"); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate static void 3850Sstevel@tonic-gate separate(char *s) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate (void) printf("====%s\n", s); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * the range of ines rold.from thru rold.to in file i 3920Sstevel@tonic-gate * is to be changed. it is to be printed only if 3930Sstevel@tonic-gate * it does not duplicate something to be printed later 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate static void 3960Sstevel@tonic-gate change(int i, struct range *rold, int dup) 3970Sstevel@tonic-gate { 3980Sstevel@tonic-gate (void) printf("%d:", i); 3990Sstevel@tonic-gate last[i] = rold->to; 4000Sstevel@tonic-gate prange(rold); 4010Sstevel@tonic-gate if (dup) 4020Sstevel@tonic-gate return; 4030Sstevel@tonic-gate if (debug) 4040Sstevel@tonic-gate return; 4050Sstevel@tonic-gate i--; 4060Sstevel@tonic-gate (void) skip(i, rold->from, (char *)0); 4070Sstevel@tonic-gate (void) skip(i, rold->to, " "); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * print the range of line numbers, rold.from thru rold.to 4120Sstevel@tonic-gate * as n1, n2 or n1 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate static void 4150Sstevel@tonic-gate prange(struct range *rold) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate if (rold->to <= rold->from) 4180Sstevel@tonic-gate (void) printf("%da\n", rold->from-1); 4190Sstevel@tonic-gate else { 4200Sstevel@tonic-gate (void) printf("%d", rold->from); 4210Sstevel@tonic-gate if (rold->to > rold->from+1) 4220Sstevel@tonic-gate (void) printf(",%d", rold->to-1); 4230Sstevel@tonic-gate (void) printf("c\n"); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * no difference was reported by diff between file 1(or 2) 4290Sstevel@tonic-gate * and file 3, and an artificial dummy difference (trange) 4300Sstevel@tonic-gate * must be ginned up to correspond to the change reported 4310Sstevel@tonic-gate * in the other file 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate static void 4340Sstevel@tonic-gate keep(int i, struct range *rnew) 4350Sstevel@tonic-gate { 4360Sstevel@tonic-gate int delta; 4370Sstevel@tonic-gate struct range trange; 4380Sstevel@tonic-gate delta = last[3] - last[i]; 4390Sstevel@tonic-gate trange.from = rnew->from - delta; 4400Sstevel@tonic-gate trange.to = rnew->to - delta; 4410Sstevel@tonic-gate change(i, &trange, 1); 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * skip to just befor line number from in file i 4460Sstevel@tonic-gate * if "pr" is nonzero, print all skipped stuff 4470Sstevel@tonic-gate * with string pr as a prefix 4480Sstevel@tonic-gate */ 4490Sstevel@tonic-gate static int 4500Sstevel@tonic-gate skip(int i, int from, char *pr) 4510Sstevel@tonic-gate { 4520Sstevel@tonic-gate int j, n; 4530Sstevel@tonic-gate for (n = 0; cline[i] < from-1; n += j) { 454*13093SRoger.Faulkner@Oracle.COM if ((j = getaline(fp[i])) == 0) 4550Sstevel@tonic-gate trouble(); 4560Sstevel@tonic-gate if (pr) 4570Sstevel@tonic-gate (void) printf("%s%s", pr, line); 4580Sstevel@tonic-gate cline[i]++; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate return (n); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * return 1 or 0 according as the old range 4650Sstevel@tonic-gate * (in file 1) contains exactly the same data 4660Sstevel@tonic-gate * as the new range (in file 2) 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate static int 4690Sstevel@tonic-gate duplicate(struct range *r1, struct range *r2) 4700Sstevel@tonic-gate { 4710Sstevel@tonic-gate int c, d; 4720Sstevel@tonic-gate int nchar; 4730Sstevel@tonic-gate int nline; 4740Sstevel@tonic-gate if (r1->to-r1->from != r2->to-r2->from) 4750Sstevel@tonic-gate return (0); 4760Sstevel@tonic-gate (void) skip(0, r1->from, (char *)0); 4770Sstevel@tonic-gate (void) skip(1, r2->from, (char *)0); 4780Sstevel@tonic-gate nchar = 0; 4790Sstevel@tonic-gate for (nline = 0; nline < r1->to-r1->from; nline++) { 4800Sstevel@tonic-gate do { 4810Sstevel@tonic-gate c = getc(fp[0]); 4820Sstevel@tonic-gate d = getc(fp[1]); 4830Sstevel@tonic-gate if (c == -1 || d == -1) 4840Sstevel@tonic-gate trouble(); 4850Sstevel@tonic-gate nchar++; 4860Sstevel@tonic-gate if (c != d) { 4870Sstevel@tonic-gate repos(nchar); 4880Sstevel@tonic-gate return (0); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate } while (c != '\n'); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate repos(nchar); 4930Sstevel@tonic-gate return (1); 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate static void 4970Sstevel@tonic-gate repos(int nchar) 4980Sstevel@tonic-gate { 4990Sstevel@tonic-gate int i; 5000Sstevel@tonic-gate for (i = 0; i < 2; i++) 5010Sstevel@tonic-gate (void) fseek(fp[i], (long)-nchar, 1); 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate static void 5050Sstevel@tonic-gate trouble() 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate (void) fprintf(stderr, "diff3: logic error\n"); 5080Sstevel@tonic-gate abort(); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * collect an editing script for later regurgitation 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate static int 5150Sstevel@tonic-gate edit(struct diff *diff, int dup, int j) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate if (((dup+1)&eflag) == 0) 5180Sstevel@tonic-gate return (j); 5190Sstevel@tonic-gate j++; 5200Sstevel@tonic-gate overlap[j] = !dup; 5210Sstevel@tonic-gate if (!dup) overlapcnt++; 5220Sstevel@tonic-gate de[j].old.from = diff->old.from; 5230Sstevel@tonic-gate de[j].old.to = diff->old.to; 5240Sstevel@tonic-gate de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, (char *)0); 5250Sstevel@tonic-gate de[j].new.to = de[j].new.from + skip(2, diff->new.to, (char *)0); 5260Sstevel@tonic-gate return (j); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* regurgitate */ 5300Sstevel@tonic-gate static void 5310Sstevel@tonic-gate edscript(int n) 5320Sstevel@tonic-gate { 5330Sstevel@tonic-gate int j, k; 5340Sstevel@tonic-gate char block[BUFSIZ]; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate for (n = n; n > 0; n--) { 5370Sstevel@tonic-gate if (!oflag || !overlap[n]) 5380Sstevel@tonic-gate prange(&de[n].old); 5390Sstevel@tonic-gate else 5400Sstevel@tonic-gate (void) printf("%da\n=======\n", de[n].old.to -1); 5410Sstevel@tonic-gate (void) fseek(fp[2], (long)de[n].new.from, 0); 5420Sstevel@tonic-gate for (k = de[n].new.to-de[n].new.from; k > 0; k -= j) { 5430Sstevel@tonic-gate j = k > BUFSIZ?BUFSIZ:k; 5440Sstevel@tonic-gate if (fread(block, 1, j, fp[2]) != j) 5450Sstevel@tonic-gate trouble(); 5460Sstevel@tonic-gate (void) fwrite(block, 1, j, stdout); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate if (!oflag || !overlap[n]) 5490Sstevel@tonic-gate (void) printf(".\n"); 5500Sstevel@tonic-gate else { 5510Sstevel@tonic-gate (void) printf("%s\n.\n", f3mark); 5520Sstevel@tonic-gate (void) printf("%da\n%s\n.\n", de[n].old.from-1, f1mark); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate } 5561969Sjohnlev 5571969Sjohnlev static void 5581969Sjohnlev usage() 5591969Sjohnlev { 5601969Sjohnlev (void) fprintf(stderr, 5611969Sjohnlev "\tusage: diff3prog [-ex3EX] d13 d23 f1 f2 f3 [m1 m2]\n"); 5621969Sjohnlev exit(1); 5631969Sjohnlev } 564