1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <unistd.h> 36*7c478bd9Sstevel@tonic-gate #include <limits.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate # 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate /* 42*7c478bd9Sstevel@tonic-gate * diff3 - 3-way differential file comparison 43*7c478bd9Sstevel@tonic-gate */ 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate /* 46*7c478bd9Sstevel@tonic-gate * diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3] 47*7c478bd9Sstevel@tonic-gate * 48*7c478bd9Sstevel@tonic-gate * d13 = diff report on f1 vs f3 49*7c478bd9Sstevel@tonic-gate * d23 = diff report on f2 vs f3 50*7c478bd9Sstevel@tonic-gate * f1, f2, f3 the 3 files 51*7c478bd9Sstevel@tonic-gate * if changes in f1 overlap with changes in f3, m1 and m3 are used 52*7c478bd9Sstevel@tonic-gate * to mark the overlaps; otherwise, the file names f1 and f3 are used 53*7c478bd9Sstevel@tonic-gate * (only for options E and X). 54*7c478bd9Sstevel@tonic-gate */ 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate struct range {int from, to; }; 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * from is first in range of changed lines 59*7c478bd9Sstevel@tonic-gate * to is last+1 60*7c478bd9Sstevel@tonic-gate * from = to = line after point of insertion 61*7c478bd9Sstevel@tonic-gate * for added lines 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate struct diff {struct range old, new; }; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate #define NC 4096 66*7c478bd9Sstevel@tonic-gate /* 67*7c478bd9Sstevel@tonic-gate * de is used to gather editing scripts, 68*7c478bd9Sstevel@tonic-gate * that are later spewed out in reverse order. 69*7c478bd9Sstevel@tonic-gate * its first element must be all zero 70*7c478bd9Sstevel@tonic-gate * the "new" component of de contains line positions 71*7c478bd9Sstevel@tonic-gate * or byte positions depending on when you look(!?) 72*7c478bd9Sstevel@tonic-gate */ 73*7c478bd9Sstevel@tonic-gate static struct diff d13[NC]; 74*7c478bd9Sstevel@tonic-gate static struct diff d23[NC]; 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate /* 77*7c478bd9Sstevel@tonic-gate * array overlap indicates which sections in de correspond to 78*7c478bd9Sstevel@tonic-gate * lines that are different in all three files. 79*7c478bd9Sstevel@tonic-gate */ 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate static struct diff de[NC]; 82*7c478bd9Sstevel@tonic-gate static char overlap[NC]; 83*7c478bd9Sstevel@tonic-gate static int overlapcnt = 0; 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate static char line[LINE_MAX+1]; 86*7c478bd9Sstevel@tonic-gate static FILE *fp[3]; 87*7c478bd9Sstevel@tonic-gate /* 88*7c478bd9Sstevel@tonic-gate * the number of the last-read line in each file 89*7c478bd9Sstevel@tonic-gate * is kept in cline[0-2] 90*7c478bd9Sstevel@tonic-gate */ 91*7c478bd9Sstevel@tonic-gate static int cline[3]; 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * the latest known correspondence between line 94*7c478bd9Sstevel@tonic-gate * numbers of the 3 files is stored in last[1-3] 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate static int last[4]; 97*7c478bd9Sstevel@tonic-gate static int eflag; 98*7c478bd9Sstevel@tonic-gate static int oflag; /* indicates whether to mark overlaps (-E or -X) */ 99*7c478bd9Sstevel@tonic-gate static int debug = 0; 100*7c478bd9Sstevel@tonic-gate /* markers for -E and -X: */ 101*7c478bd9Sstevel@tonic-gate static char f1mark[8+MAXPATHLEN], f3mark[8+MAXPATHLEN]; 102*7c478bd9Sstevel@tonic-gate /* Need space for "<<<<<<< " or ">>>>>>> " plus filename */ 103*7c478bd9Sstevel@tonic-gate static int save_err; /* saves errno */ 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate static int readin(char *name, struct diff *dd); 106*7c478bd9Sstevel@tonic-gate static int number(char **lc); 107*7c478bd9Sstevel@tonic-gate static int digit(int c); 108*7c478bd9Sstevel@tonic-gate static int getchange(FILE *b); 109*7c478bd9Sstevel@tonic-gate static int getline(FILE *b); 110*7c478bd9Sstevel@tonic-gate static void merge(int m1, int m2); 111*7c478bd9Sstevel@tonic-gate static void separate(char *s); 112*7c478bd9Sstevel@tonic-gate static void change(int i, struct range *rold, int dup); 113*7c478bd9Sstevel@tonic-gate static void prange(struct range *rold); 114*7c478bd9Sstevel@tonic-gate static void keep(int i, struct range *rnew); 115*7c478bd9Sstevel@tonic-gate static int skip(int i, int from, char *pr); 116*7c478bd9Sstevel@tonic-gate static int duplicate(struct range *r1, struct range *r2); 117*7c478bd9Sstevel@tonic-gate static void repos(int nchar); 118*7c478bd9Sstevel@tonic-gate static void trouble(); 119*7c478bd9Sstevel@tonic-gate static int edit(struct diff *diff, int dup, int j); 120*7c478bd9Sstevel@tonic-gate static void edscript(int n); 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate int 123*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 124*7c478bd9Sstevel@tonic-gate { 125*7c478bd9Sstevel@tonic-gate int i, m, n; 126*7c478bd9Sstevel@tonic-gate eflag = 0; 127*7c478bd9Sstevel@tonic-gate oflag = 0; 128*7c478bd9Sstevel@tonic-gate if (*argv[1] == '-') { 129*7c478bd9Sstevel@tonic-gate switch (argv[1][1]) { 130*7c478bd9Sstevel@tonic-gate default: 131*7c478bd9Sstevel@tonic-gate eflag = 3; 132*7c478bd9Sstevel@tonic-gate break; 133*7c478bd9Sstevel@tonic-gate case '3': 134*7c478bd9Sstevel@tonic-gate eflag = 2; 135*7c478bd9Sstevel@tonic-gate break; 136*7c478bd9Sstevel@tonic-gate case 'x': 137*7c478bd9Sstevel@tonic-gate eflag = 1; 138*7c478bd9Sstevel@tonic-gate break; 139*7c478bd9Sstevel@tonic-gate case 'E': 140*7c478bd9Sstevel@tonic-gate eflag = 3; 141*7c478bd9Sstevel@tonic-gate oflag = 1; 142*7c478bd9Sstevel@tonic-gate break; 143*7c478bd9Sstevel@tonic-gate case 'X': 144*7c478bd9Sstevel@tonic-gate oflag = eflag = 1; 145*7c478bd9Sstevel@tonic-gate break; 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate argv++; 148*7c478bd9Sstevel@tonic-gate argc--; 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate if (argc < 6) { 151*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff3: arg count\n"); 152*7c478bd9Sstevel@tonic-gate exit(1); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate if (oflag) { 156*7c478bd9Sstevel@tonic-gate (void) snprintf(f1mark, sizeof (f1mark), "<<<<<<< %s", 157*7c478bd9Sstevel@tonic-gate argc >= 7 ? argv[6] : argv[3]); 158*7c478bd9Sstevel@tonic-gate (void) snprintf(f3mark, sizeof (f3mark), ">>>>>>> %s", 159*7c478bd9Sstevel@tonic-gate argc >= 8 ? argv[7] : argv[5]); 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate m = readin(argv[1], d13); 163*7c478bd9Sstevel@tonic-gate n = readin(argv[2], d23); 164*7c478bd9Sstevel@tonic-gate for (i = 0; i <= 2; i++) 165*7c478bd9Sstevel@tonic-gate if ((fp[i] = fopen(argv[i+3], "r")) == NULL) { 166*7c478bd9Sstevel@tonic-gate save_err = errno; 167*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff3: can't open %s: ", 168*7c478bd9Sstevel@tonic-gate argv[i+3]); 169*7c478bd9Sstevel@tonic-gate errno = save_err; 170*7c478bd9Sstevel@tonic-gate perror(""); 171*7c478bd9Sstevel@tonic-gate exit(1); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate merge(m, n); 174*7c478bd9Sstevel@tonic-gate return (0); 175*7c478bd9Sstevel@tonic-gate } 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate /* 178*7c478bd9Sstevel@tonic-gate * pick up the line numbers of all changes from 179*7c478bd9Sstevel@tonic-gate * one change file 180*7c478bd9Sstevel@tonic-gate * (this puts the numbers in a vector, which is not 181*7c478bd9Sstevel@tonic-gate * strictly necessary, since the vector is processed 182*7c478bd9Sstevel@tonic-gate * in one sequential pass. The vector could be optimized 183*7c478bd9Sstevel@tonic-gate * out of existence) 184*7c478bd9Sstevel@tonic-gate */ 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate static int 187*7c478bd9Sstevel@tonic-gate readin(char *name, struct diff *dd) 188*7c478bd9Sstevel@tonic-gate { 189*7c478bd9Sstevel@tonic-gate int i; 190*7c478bd9Sstevel@tonic-gate int a, b, c, d; 191*7c478bd9Sstevel@tonic-gate char kind; 192*7c478bd9Sstevel@tonic-gate char *p; 193*7c478bd9Sstevel@tonic-gate if ((fp[0] = fopen(name, "r")) == NULL) { 194*7c478bd9Sstevel@tonic-gate save_err = errno; 195*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff3: can't open %s: ", name); 196*7c478bd9Sstevel@tonic-gate errno = save_err; 197*7c478bd9Sstevel@tonic-gate perror(""); 198*7c478bd9Sstevel@tonic-gate exit(1); 199*7c478bd9Sstevel@tonic-gate } 200*7c478bd9Sstevel@tonic-gate for (i = 0; getchange(fp[0]); i++) { 201*7c478bd9Sstevel@tonic-gate if (i >= NC) { 202*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff3: too many changes\n"); 203*7c478bd9Sstevel@tonic-gate exit(0); 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate p = line; 206*7c478bd9Sstevel@tonic-gate a = b = number(&p); 207*7c478bd9Sstevel@tonic-gate if (*p == ',') { 208*7c478bd9Sstevel@tonic-gate p++; 209*7c478bd9Sstevel@tonic-gate b = number(&p); 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate kind = *p++; 212*7c478bd9Sstevel@tonic-gate c = d = number(&p); 213*7c478bd9Sstevel@tonic-gate if (*p == ',') { 214*7c478bd9Sstevel@tonic-gate p++; 215*7c478bd9Sstevel@tonic-gate d = number(&p); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate if (kind == 'a') 218*7c478bd9Sstevel@tonic-gate a++; 219*7c478bd9Sstevel@tonic-gate if (kind == 'd') 220*7c478bd9Sstevel@tonic-gate c++; 221*7c478bd9Sstevel@tonic-gate b++; 222*7c478bd9Sstevel@tonic-gate d++; 223*7c478bd9Sstevel@tonic-gate dd[i].old.from = a; 224*7c478bd9Sstevel@tonic-gate dd[i].old.to = b; 225*7c478bd9Sstevel@tonic-gate dd[i].new.from = c; 226*7c478bd9Sstevel@tonic-gate dd[i].new.to = d; 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate dd[i].old.from = dd[i-1].old.to; 229*7c478bd9Sstevel@tonic-gate dd[i].new.from = dd[i-1].new.to; 230*7c478bd9Sstevel@tonic-gate (void) fclose(fp[0]); 231*7c478bd9Sstevel@tonic-gate return (i); 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate static int 235*7c478bd9Sstevel@tonic-gate number(char **lc) 236*7c478bd9Sstevel@tonic-gate { 237*7c478bd9Sstevel@tonic-gate int nn; 238*7c478bd9Sstevel@tonic-gate nn = 0; 239*7c478bd9Sstevel@tonic-gate while (digit(**lc)) 240*7c478bd9Sstevel@tonic-gate nn = nn*10 + *(*lc)++ - '0'; 241*7c478bd9Sstevel@tonic-gate return (nn); 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate static int 245*7c478bd9Sstevel@tonic-gate digit(int c) 246*7c478bd9Sstevel@tonic-gate { 247*7c478bd9Sstevel@tonic-gate return (c >= '0' && c <= '9'); 248*7c478bd9Sstevel@tonic-gate } 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate static int 251*7c478bd9Sstevel@tonic-gate getchange(FILE *b) 252*7c478bd9Sstevel@tonic-gate { 253*7c478bd9Sstevel@tonic-gate while (getline(b)) 254*7c478bd9Sstevel@tonic-gate if (digit(line[0])) 255*7c478bd9Sstevel@tonic-gate return (1); 256*7c478bd9Sstevel@tonic-gate return (0); 257*7c478bd9Sstevel@tonic-gate } 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate static int 260*7c478bd9Sstevel@tonic-gate getline(FILE *b) 261*7c478bd9Sstevel@tonic-gate { 262*7c478bd9Sstevel@tonic-gate int i, c; 263*7c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (line)-1; i++) { 264*7c478bd9Sstevel@tonic-gate c = getc(b); 265*7c478bd9Sstevel@tonic-gate if (c == EOF) { 266*7c478bd9Sstevel@tonic-gate line[i] = 0; 267*7c478bd9Sstevel@tonic-gate return (i); 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate line[i] = c; 270*7c478bd9Sstevel@tonic-gate if (c == '\n') { 271*7c478bd9Sstevel@tonic-gate line[++i] = 0; 272*7c478bd9Sstevel@tonic-gate return (i); 273*7c478bd9Sstevel@tonic-gate } 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate return (0); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate static void 279*7c478bd9Sstevel@tonic-gate merge(int m1, int m2) 280*7c478bd9Sstevel@tonic-gate { 281*7c478bd9Sstevel@tonic-gate struct diff *d1, *d2, *d3; 282*7c478bd9Sstevel@tonic-gate int dup; 283*7c478bd9Sstevel@tonic-gate int j; 284*7c478bd9Sstevel@tonic-gate int t1, t2; 285*7c478bd9Sstevel@tonic-gate d1 = d13; 286*7c478bd9Sstevel@tonic-gate d2 = d23; 287*7c478bd9Sstevel@tonic-gate j = 0; 288*7c478bd9Sstevel@tonic-gate for (; (t1 = d1 < d13+m1) | (t2 = d2 < d23+m2); ) { 289*7c478bd9Sstevel@tonic-gate if (debug) { 290*7c478bd9Sstevel@tonic-gate (void) printf("%d,%d=%d,%d %d,%d=%d,%d\n", 291*7c478bd9Sstevel@tonic-gate d1->old.from, d1->old.to, 292*7c478bd9Sstevel@tonic-gate d1->new.from, d1->new.to, 293*7c478bd9Sstevel@tonic-gate d2->old.from, d2->old.to, 294*7c478bd9Sstevel@tonic-gate d2->new.from, d2->new.to); 295*7c478bd9Sstevel@tonic-gate } 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate /* first file is different from others */ 298*7c478bd9Sstevel@tonic-gate if (!t2 || t1 && d1->new.to < d2->new.from) { 299*7c478bd9Sstevel@tonic-gate /* stuff peculiar to 1st file */ 300*7c478bd9Sstevel@tonic-gate if (eflag == 0) { 301*7c478bd9Sstevel@tonic-gate separate("1"); 302*7c478bd9Sstevel@tonic-gate change(1, &d1->old, 0); 303*7c478bd9Sstevel@tonic-gate keep(2, &d1->new); 304*7c478bd9Sstevel@tonic-gate change(3, &d1->new, 0); 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate d1++; 307*7c478bd9Sstevel@tonic-gate continue; 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate 310*7c478bd9Sstevel@tonic-gate /* second file is different from others */ 311*7c478bd9Sstevel@tonic-gate if (!t1 || t2 && d2->new.to < d1->new.from) { 312*7c478bd9Sstevel@tonic-gate if (eflag == 0) { 313*7c478bd9Sstevel@tonic-gate separate("2"); 314*7c478bd9Sstevel@tonic-gate keep(1, &d2->new); 315*7c478bd9Sstevel@tonic-gate change(2, &d2->old, 0); 316*7c478bd9Sstevel@tonic-gate change(3, &d2->new, 0); 317*7c478bd9Sstevel@tonic-gate } 318*7c478bd9Sstevel@tonic-gate d2++; 319*7c478bd9Sstevel@tonic-gate continue; 320*7c478bd9Sstevel@tonic-gate } 321*7c478bd9Sstevel@tonic-gate /* 322*7c478bd9Sstevel@tonic-gate * merge overlapping changes in first file 323*7c478bd9Sstevel@tonic-gate * this happens after extension see below 324*7c478bd9Sstevel@tonic-gate */ 325*7c478bd9Sstevel@tonic-gate if (d1+1 < d13+m1 && d1->new.to >= d1[1].new.from) { 326*7c478bd9Sstevel@tonic-gate d1[1].old.from = d1->old.from; 327*7c478bd9Sstevel@tonic-gate d1[1].new.from = d1->new.from; 328*7c478bd9Sstevel@tonic-gate d1++; 329*7c478bd9Sstevel@tonic-gate continue; 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate /* merge overlapping changes in second */ 333*7c478bd9Sstevel@tonic-gate if (d2+1 < d23+m2 && d2->new.to >= d2[1].new.from) { 334*7c478bd9Sstevel@tonic-gate d2[1].old.from = d2->old.from; 335*7c478bd9Sstevel@tonic-gate d2[1].new.from = d2->new.from; 336*7c478bd9Sstevel@tonic-gate d2++; 337*7c478bd9Sstevel@tonic-gate continue; 338*7c478bd9Sstevel@tonic-gate } 339*7c478bd9Sstevel@tonic-gate 340*7c478bd9Sstevel@tonic-gate /* stuff peculiar to third file or different in all */ 341*7c478bd9Sstevel@tonic-gate if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) { 342*7c478bd9Sstevel@tonic-gate dup = duplicate(&d1->old, &d2->old); 343*7c478bd9Sstevel@tonic-gate /* 344*7c478bd9Sstevel@tonic-gate * dup = 0 means all files differ 345*7c478bd9Sstevel@tonic-gate * dup = 1 meands files 1&2 identical 346*7c478bd9Sstevel@tonic-gate */ 347*7c478bd9Sstevel@tonic-gate if (eflag == 0) { 348*7c478bd9Sstevel@tonic-gate separate(dup?"3":""); 349*7c478bd9Sstevel@tonic-gate change(1, &d1->old, dup); 350*7c478bd9Sstevel@tonic-gate change(2, &d2->old, 0); 351*7c478bd9Sstevel@tonic-gate d3 = d1->old.to > d1->old.from ? d1 : d2; 352*7c478bd9Sstevel@tonic-gate change(3, &d3->new, 0); 353*7c478bd9Sstevel@tonic-gate } else 354*7c478bd9Sstevel@tonic-gate j = edit(d1, dup, j); 355*7c478bd9Sstevel@tonic-gate d1++; 356*7c478bd9Sstevel@tonic-gate d2++; 357*7c478bd9Sstevel@tonic-gate continue; 358*7c478bd9Sstevel@tonic-gate } 359*7c478bd9Sstevel@tonic-gate /* 360*7c478bd9Sstevel@tonic-gate * overlapping changes from file1 & 2 361*7c478bd9Sstevel@tonic-gate * extend changes appropriately to 362*7c478bd9Sstevel@tonic-gate * make them coincide 363*7c478bd9Sstevel@tonic-gate */ 364*7c478bd9Sstevel@tonic-gate if (d1->new.from < d2->new.from) { 365*7c478bd9Sstevel@tonic-gate d2->old.from -= d2->new.from-d1->new.from; 366*7c478bd9Sstevel@tonic-gate d2->new.from = d1->new.from; 367*7c478bd9Sstevel@tonic-gate } else if (d2->new.from < d1->new.from) { 368*7c478bd9Sstevel@tonic-gate d1->old.from -= d1->new.from-d2->new.from; 369*7c478bd9Sstevel@tonic-gate d1->new.from = d2->new.from; 370*7c478bd9Sstevel@tonic-gate } 371*7c478bd9Sstevel@tonic-gate 372*7c478bd9Sstevel@tonic-gate if (d1->new.to > d2->new.to) { 373*7c478bd9Sstevel@tonic-gate d2->old.to += d1->new.to - d2->new.to; 374*7c478bd9Sstevel@tonic-gate d2->new.to = d1->new.to; 375*7c478bd9Sstevel@tonic-gate } else if (d2->new.to > d1->new.to) { 376*7c478bd9Sstevel@tonic-gate d1->old.to += d2->new.to - d1->new.to; 377*7c478bd9Sstevel@tonic-gate d1->new.to = d2->new.to; 378*7c478bd9Sstevel@tonic-gate } 379*7c478bd9Sstevel@tonic-gate } 380*7c478bd9Sstevel@tonic-gate if (eflag) { 381*7c478bd9Sstevel@tonic-gate edscript(j); 382*7c478bd9Sstevel@tonic-gate if (j) 383*7c478bd9Sstevel@tonic-gate (void) printf("w\nq\n"); 384*7c478bd9Sstevel@tonic-gate } 385*7c478bd9Sstevel@tonic-gate } 386*7c478bd9Sstevel@tonic-gate 387*7c478bd9Sstevel@tonic-gate static void 388*7c478bd9Sstevel@tonic-gate separate(char *s) 389*7c478bd9Sstevel@tonic-gate { 390*7c478bd9Sstevel@tonic-gate (void) printf("====%s\n", s); 391*7c478bd9Sstevel@tonic-gate } 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate /* 394*7c478bd9Sstevel@tonic-gate * the range of ines rold.from thru rold.to in file i 395*7c478bd9Sstevel@tonic-gate * is to be changed. it is to be printed only if 396*7c478bd9Sstevel@tonic-gate * it does not duplicate something to be printed later 397*7c478bd9Sstevel@tonic-gate */ 398*7c478bd9Sstevel@tonic-gate static void 399*7c478bd9Sstevel@tonic-gate change(int i, struct range *rold, int dup) 400*7c478bd9Sstevel@tonic-gate { 401*7c478bd9Sstevel@tonic-gate (void) printf("%d:", i); 402*7c478bd9Sstevel@tonic-gate last[i] = rold->to; 403*7c478bd9Sstevel@tonic-gate prange(rold); 404*7c478bd9Sstevel@tonic-gate if (dup) 405*7c478bd9Sstevel@tonic-gate return; 406*7c478bd9Sstevel@tonic-gate if (debug) 407*7c478bd9Sstevel@tonic-gate return; 408*7c478bd9Sstevel@tonic-gate i--; 409*7c478bd9Sstevel@tonic-gate (void) skip(i, rold->from, (char *)0); 410*7c478bd9Sstevel@tonic-gate (void) skip(i, rold->to, " "); 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate /* 414*7c478bd9Sstevel@tonic-gate * print the range of line numbers, rold.from thru rold.to 415*7c478bd9Sstevel@tonic-gate * as n1, n2 or n1 416*7c478bd9Sstevel@tonic-gate */ 417*7c478bd9Sstevel@tonic-gate static void 418*7c478bd9Sstevel@tonic-gate prange(struct range *rold) 419*7c478bd9Sstevel@tonic-gate { 420*7c478bd9Sstevel@tonic-gate if (rold->to <= rold->from) 421*7c478bd9Sstevel@tonic-gate (void) printf("%da\n", rold->from-1); 422*7c478bd9Sstevel@tonic-gate else { 423*7c478bd9Sstevel@tonic-gate (void) printf("%d", rold->from); 424*7c478bd9Sstevel@tonic-gate if (rold->to > rold->from+1) 425*7c478bd9Sstevel@tonic-gate (void) printf(",%d", rold->to-1); 426*7c478bd9Sstevel@tonic-gate (void) printf("c\n"); 427*7c478bd9Sstevel@tonic-gate } 428*7c478bd9Sstevel@tonic-gate } 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate /* 431*7c478bd9Sstevel@tonic-gate * no difference was reported by diff between file 1(or 2) 432*7c478bd9Sstevel@tonic-gate * and file 3, and an artificial dummy difference (trange) 433*7c478bd9Sstevel@tonic-gate * must be ginned up to correspond to the change reported 434*7c478bd9Sstevel@tonic-gate * in the other file 435*7c478bd9Sstevel@tonic-gate */ 436*7c478bd9Sstevel@tonic-gate static void 437*7c478bd9Sstevel@tonic-gate keep(int i, struct range *rnew) 438*7c478bd9Sstevel@tonic-gate { 439*7c478bd9Sstevel@tonic-gate int delta; 440*7c478bd9Sstevel@tonic-gate struct range trange; 441*7c478bd9Sstevel@tonic-gate delta = last[3] - last[i]; 442*7c478bd9Sstevel@tonic-gate trange.from = rnew->from - delta; 443*7c478bd9Sstevel@tonic-gate trange.to = rnew->to - delta; 444*7c478bd9Sstevel@tonic-gate change(i, &trange, 1); 445*7c478bd9Sstevel@tonic-gate } 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate /* 448*7c478bd9Sstevel@tonic-gate * skip to just befor line number from in file i 449*7c478bd9Sstevel@tonic-gate * if "pr" is nonzero, print all skipped stuff 450*7c478bd9Sstevel@tonic-gate * with string pr as a prefix 451*7c478bd9Sstevel@tonic-gate */ 452*7c478bd9Sstevel@tonic-gate static int 453*7c478bd9Sstevel@tonic-gate skip(int i, int from, char *pr) 454*7c478bd9Sstevel@tonic-gate { 455*7c478bd9Sstevel@tonic-gate int j, n; 456*7c478bd9Sstevel@tonic-gate for (n = 0; cline[i] < from-1; n += j) { 457*7c478bd9Sstevel@tonic-gate if ((j = getline(fp[i])) == 0) 458*7c478bd9Sstevel@tonic-gate trouble(); 459*7c478bd9Sstevel@tonic-gate if (pr) 460*7c478bd9Sstevel@tonic-gate (void) printf("%s%s", pr, line); 461*7c478bd9Sstevel@tonic-gate cline[i]++; 462*7c478bd9Sstevel@tonic-gate } 463*7c478bd9Sstevel@tonic-gate return (n); 464*7c478bd9Sstevel@tonic-gate } 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate /* 467*7c478bd9Sstevel@tonic-gate * return 1 or 0 according as the old range 468*7c478bd9Sstevel@tonic-gate * (in file 1) contains exactly the same data 469*7c478bd9Sstevel@tonic-gate * as the new range (in file 2) 470*7c478bd9Sstevel@tonic-gate */ 471*7c478bd9Sstevel@tonic-gate static int 472*7c478bd9Sstevel@tonic-gate duplicate(struct range *r1, struct range *r2) 473*7c478bd9Sstevel@tonic-gate { 474*7c478bd9Sstevel@tonic-gate int c, d; 475*7c478bd9Sstevel@tonic-gate int nchar; 476*7c478bd9Sstevel@tonic-gate int nline; 477*7c478bd9Sstevel@tonic-gate if (r1->to-r1->from != r2->to-r2->from) 478*7c478bd9Sstevel@tonic-gate return (0); 479*7c478bd9Sstevel@tonic-gate (void) skip(0, r1->from, (char *)0); 480*7c478bd9Sstevel@tonic-gate (void) skip(1, r2->from, (char *)0); 481*7c478bd9Sstevel@tonic-gate nchar = 0; 482*7c478bd9Sstevel@tonic-gate for (nline = 0; nline < r1->to-r1->from; nline++) { 483*7c478bd9Sstevel@tonic-gate do { 484*7c478bd9Sstevel@tonic-gate c = getc(fp[0]); 485*7c478bd9Sstevel@tonic-gate d = getc(fp[1]); 486*7c478bd9Sstevel@tonic-gate if (c == -1 || d == -1) 487*7c478bd9Sstevel@tonic-gate trouble(); 488*7c478bd9Sstevel@tonic-gate nchar++; 489*7c478bd9Sstevel@tonic-gate if (c != d) { 490*7c478bd9Sstevel@tonic-gate repos(nchar); 491*7c478bd9Sstevel@tonic-gate return (0); 492*7c478bd9Sstevel@tonic-gate } 493*7c478bd9Sstevel@tonic-gate } while (c != '\n'); 494*7c478bd9Sstevel@tonic-gate } 495*7c478bd9Sstevel@tonic-gate repos(nchar); 496*7c478bd9Sstevel@tonic-gate return (1); 497*7c478bd9Sstevel@tonic-gate } 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate static void 500*7c478bd9Sstevel@tonic-gate repos(int nchar) 501*7c478bd9Sstevel@tonic-gate { 502*7c478bd9Sstevel@tonic-gate int i; 503*7c478bd9Sstevel@tonic-gate for (i = 0; i < 2; i++) 504*7c478bd9Sstevel@tonic-gate (void) fseek(fp[i], (long)-nchar, 1); 505*7c478bd9Sstevel@tonic-gate } 506*7c478bd9Sstevel@tonic-gate 507*7c478bd9Sstevel@tonic-gate static void 508*7c478bd9Sstevel@tonic-gate trouble() 509*7c478bd9Sstevel@tonic-gate { 510*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff3: logic error\n"); 511*7c478bd9Sstevel@tonic-gate abort(); 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate 514*7c478bd9Sstevel@tonic-gate /* 515*7c478bd9Sstevel@tonic-gate * collect an editing script for later regurgitation 516*7c478bd9Sstevel@tonic-gate */ 517*7c478bd9Sstevel@tonic-gate static int 518*7c478bd9Sstevel@tonic-gate edit(struct diff *diff, int dup, int j) 519*7c478bd9Sstevel@tonic-gate { 520*7c478bd9Sstevel@tonic-gate if (((dup+1)&eflag) == 0) 521*7c478bd9Sstevel@tonic-gate return (j); 522*7c478bd9Sstevel@tonic-gate j++; 523*7c478bd9Sstevel@tonic-gate overlap[j] = !dup; 524*7c478bd9Sstevel@tonic-gate if (!dup) overlapcnt++; 525*7c478bd9Sstevel@tonic-gate de[j].old.from = diff->old.from; 526*7c478bd9Sstevel@tonic-gate de[j].old.to = diff->old.to; 527*7c478bd9Sstevel@tonic-gate de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, (char *)0); 528*7c478bd9Sstevel@tonic-gate de[j].new.to = de[j].new.from + skip(2, diff->new.to, (char *)0); 529*7c478bd9Sstevel@tonic-gate return (j); 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate /* regurgitate */ 533*7c478bd9Sstevel@tonic-gate static void 534*7c478bd9Sstevel@tonic-gate edscript(int n) 535*7c478bd9Sstevel@tonic-gate { 536*7c478bd9Sstevel@tonic-gate int j, k; 537*7c478bd9Sstevel@tonic-gate char block[BUFSIZ]; 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate for (n = n; n > 0; n--) { 540*7c478bd9Sstevel@tonic-gate if (!oflag || !overlap[n]) 541*7c478bd9Sstevel@tonic-gate prange(&de[n].old); 542*7c478bd9Sstevel@tonic-gate else 543*7c478bd9Sstevel@tonic-gate (void) printf("%da\n=======\n", de[n].old.to -1); 544*7c478bd9Sstevel@tonic-gate (void) fseek(fp[2], (long)de[n].new.from, 0); 545*7c478bd9Sstevel@tonic-gate for (k = de[n].new.to-de[n].new.from; k > 0; k -= j) { 546*7c478bd9Sstevel@tonic-gate j = k > BUFSIZ?BUFSIZ:k; 547*7c478bd9Sstevel@tonic-gate if (fread(block, 1, j, fp[2]) != j) 548*7c478bd9Sstevel@tonic-gate trouble(); 549*7c478bd9Sstevel@tonic-gate (void) fwrite(block, 1, j, stdout); 550*7c478bd9Sstevel@tonic-gate } 551*7c478bd9Sstevel@tonic-gate if (!oflag || !overlap[n]) 552*7c478bd9Sstevel@tonic-gate (void) printf(".\n"); 553*7c478bd9Sstevel@tonic-gate else { 554*7c478bd9Sstevel@tonic-gate (void) printf("%s\n.\n", f3mark); 555*7c478bd9Sstevel@tonic-gate (void) printf("%da\n%s\n.\n", de[n].old.from-1, f1mark); 556*7c478bd9Sstevel@tonic-gate } 557*7c478bd9Sstevel@tonic-gate } 558*7c478bd9Sstevel@tonic-gate } 559