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