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