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 <ctype.h> 37*0Sstevel@tonic-gate #include <locale.h> 38*0Sstevel@tonic-gate #include <sys/types.h> 39*0Sstevel@tonic-gate #include <sys/stat.h> 40*0Sstevel@tonic-gate #include <limits.h> 41*0Sstevel@tonic-gate #include <stdarg.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #define C 3 44*0Sstevel@tonic-gate #define RANGE 30 45*0Sstevel@tonic-gate #define LEN 255 46*0Sstevel@tonic-gate #define INF 16384 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate char *text[2][RANGE]; 49*0Sstevel@tonic-gate long lineno[2] = {1, 1}; /* no. of 1st stored line in each file */ 50*0Sstevel@tonic-gate int ntext[2]; /* number of stored lines in each */ 51*0Sstevel@tonic-gate long n0, n1; /* scan pointer in each */ 52*0Sstevel@tonic-gate int bflag; 53*0Sstevel@tonic-gate int debug = 0; 54*0Sstevel@tonic-gate FILE *file[2]; 55*0Sstevel@tonic-gate static int diffFound = 0; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate static char *getl(int f, long n); 58*0Sstevel@tonic-gate static void clrl(int f, long n); 59*0Sstevel@tonic-gate static void movstr(char *s, char *t); 60*0Sstevel@tonic-gate static int easysynch(void); 61*0Sstevel@tonic-gate static int output(int a, int b); 62*0Sstevel@tonic-gate static void change(long a, int b, long c, int d, char *s); 63*0Sstevel@tonic-gate static void range(long a, int b); 64*0Sstevel@tonic-gate static int cmp(char *s, char *t); 65*0Sstevel@tonic-gate static FILE *dopen(char *f1, char *f2); 66*0Sstevel@tonic-gate static void progerr(char *s); 67*0Sstevel@tonic-gate static void error(char *err, ...); 68*0Sstevel@tonic-gate static int hardsynch(void); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* return pointer to line n of file f */ 71*0Sstevel@tonic-gate static char * 72*0Sstevel@tonic-gate getl(int f, long n) 73*0Sstevel@tonic-gate { 74*0Sstevel@tonic-gate char *t; 75*0Sstevel@tonic-gate int delta, nt; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate again: 78*0Sstevel@tonic-gate delta = n - lineno[f]; 79*0Sstevel@tonic-gate nt = ntext[f]; 80*0Sstevel@tonic-gate if (delta < 0) 81*0Sstevel@tonic-gate progerr("1"); 82*0Sstevel@tonic-gate if (delta < nt) 83*0Sstevel@tonic-gate return (text[f][delta]); 84*0Sstevel@tonic-gate if (delta > nt) 85*0Sstevel@tonic-gate progerr("2"); 86*0Sstevel@tonic-gate if (nt >= RANGE) 87*0Sstevel@tonic-gate progerr("3"); 88*0Sstevel@tonic-gate if (feof(file[f])) 89*0Sstevel@tonic-gate return (NULL); 90*0Sstevel@tonic-gate t = text[f][nt]; 91*0Sstevel@tonic-gate if (t == 0) { 92*0Sstevel@tonic-gate t = text[f][nt] = (char *)malloc(LEN+1); 93*0Sstevel@tonic-gate if (t == NULL) 94*0Sstevel@tonic-gate if (hardsynch()) 95*0Sstevel@tonic-gate goto again; 96*0Sstevel@tonic-gate else 97*0Sstevel@tonic-gate progerr("5"); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate t = fgets(t, LEN, file[f]); 100*0Sstevel@tonic-gate if (t != NULL) 101*0Sstevel@tonic-gate ntext[f]++; 102*0Sstevel@tonic-gate return (t); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* remove thru line n of file f from storage */ 106*0Sstevel@tonic-gate static void 107*0Sstevel@tonic-gate clrl(int f, long n) 108*0Sstevel@tonic-gate { 109*0Sstevel@tonic-gate int i, j; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate j = n-lineno[f]+1; 112*0Sstevel@tonic-gate for (i = 0; i+j < ntext[f]; i++) 113*0Sstevel@tonic-gate movstr(text[f][i+j], text[f][i]); 114*0Sstevel@tonic-gate lineno[f] = n+1; 115*0Sstevel@tonic-gate ntext[f] -= j; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate static void 119*0Sstevel@tonic-gate movstr(char *s, char *t) 120*0Sstevel@tonic-gate { 121*0Sstevel@tonic-gate while (*t++ = *s++) 122*0Sstevel@tonic-gate continue; 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate int 126*0Sstevel@tonic-gate main(int argc, char **argv) 127*0Sstevel@tonic-gate { 128*0Sstevel@tonic-gate char *s0, *s1; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate if ((argc > 1) && (*argv[1] == '-')) { 131*0Sstevel@tonic-gate argc--; 132*0Sstevel@tonic-gate argv++; 133*0Sstevel@tonic-gate while (*++argv[0]) 134*0Sstevel@tonic-gate if (*argv[0] == 'b') 135*0Sstevel@tonic-gate bflag++; 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 139*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 140*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 141*0Sstevel@tonic-gate #endif 142*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (argc != 3) 145*0Sstevel@tonic-gate error(gettext("must have 2 file arguments")); 146*0Sstevel@tonic-gate file[0] = dopen(argv[1], argv[2]); 147*0Sstevel@tonic-gate file[1] = dopen(argv[2], argv[1]); 148*0Sstevel@tonic-gate for (;;) { 149*0Sstevel@tonic-gate s0 = getl(0, ++n0); 150*0Sstevel@tonic-gate s1 = getl(1, ++n1); 151*0Sstevel@tonic-gate if (s0 == NULL || s1 == NULL) 152*0Sstevel@tonic-gate break; 153*0Sstevel@tonic-gate if (cmp(s0, s1) != 0) { 154*0Sstevel@tonic-gate if (!easysynch() && !hardsynch()) 155*0Sstevel@tonic-gate progerr("5"); 156*0Sstevel@tonic-gate } else { 157*0Sstevel@tonic-gate clrl(0, n0); 158*0Sstevel@tonic-gate clrl(1, n1); 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate } 161*0Sstevel@tonic-gate /* diff is expected to return 1 if the files differ */ 162*0Sstevel@tonic-gate if (s0 == NULL && s1 == NULL) 163*0Sstevel@tonic-gate return (diffFound); 164*0Sstevel@tonic-gate if (s0 == NULL) { 165*0Sstevel@tonic-gate (void) output(-1, INF); 166*0Sstevel@tonic-gate return (1); 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate if (s1 == NULL) { 169*0Sstevel@tonic-gate (void) output(INF, -1); 170*0Sstevel@tonic-gate return (1); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate /* NOTREACHED */ 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate /* synch on C successive matches */ 176*0Sstevel@tonic-gate static int 177*0Sstevel@tonic-gate easysynch() 178*0Sstevel@tonic-gate { 179*0Sstevel@tonic-gate int i, j; 180*0Sstevel@tonic-gate int k, m; 181*0Sstevel@tonic-gate char *s0, *s1; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate for (i = j = 1; i < RANGE && j < RANGE; i++, j++) { 184*0Sstevel@tonic-gate s0 = getl(0, n0+i); 185*0Sstevel@tonic-gate if (s0 == NULL) 186*0Sstevel@tonic-gate return (output(INF, INF)); 187*0Sstevel@tonic-gate for (k = C-1; k < j; k++) { 188*0Sstevel@tonic-gate for (m = 0; m < C; m++) 189*0Sstevel@tonic-gate if (cmp(getl(0, n0+i-m), 190*0Sstevel@tonic-gate getl(1, n1+k-m)) != 0) 191*0Sstevel@tonic-gate goto cont1; 192*0Sstevel@tonic-gate return (output(i-C, k-C)); 193*0Sstevel@tonic-gate cont1: 194*0Sstevel@tonic-gate ; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate s1 = getl(1, n1+j); 197*0Sstevel@tonic-gate if (s1 == NULL) 198*0Sstevel@tonic-gate return (output(INF, INF)); 199*0Sstevel@tonic-gate for (k = C-1; k <= i; k++) { 200*0Sstevel@tonic-gate for (m = 0; m < C; m++) 201*0Sstevel@tonic-gate if (cmp(getl(0, n0+k-m), 202*0Sstevel@tonic-gate getl(1, n1+j-m)) != 0) 203*0Sstevel@tonic-gate goto cont2; 204*0Sstevel@tonic-gate return (output(k-C, j-C)); 205*0Sstevel@tonic-gate cont2: 206*0Sstevel@tonic-gate ; 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate return (0); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate static int 213*0Sstevel@tonic-gate output(int a, int b) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate int i; 216*0Sstevel@tonic-gate char *s; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if (a < 0) 219*0Sstevel@tonic-gate change(n0-1, 0, n1, b, "a"); 220*0Sstevel@tonic-gate else if (b < 0) 221*0Sstevel@tonic-gate change(n0, a, n1-1, 0, "d"); 222*0Sstevel@tonic-gate else 223*0Sstevel@tonic-gate change(n0, a, n1, b, "c"); 224*0Sstevel@tonic-gate for (i = 0; i <= a; i++) { 225*0Sstevel@tonic-gate s = getl(0, n0+i); 226*0Sstevel@tonic-gate if (s == NULL) 227*0Sstevel@tonic-gate break; 228*0Sstevel@tonic-gate (void) printf("< %s", s); 229*0Sstevel@tonic-gate clrl(0, n0+i); 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate n0 += i-1; 232*0Sstevel@tonic-gate if (a >= 0 && b >= 0) 233*0Sstevel@tonic-gate (void) printf("---\n"); 234*0Sstevel@tonic-gate for (i = 0; i <= b; i++) { 235*0Sstevel@tonic-gate s = getl(1, n1+i); 236*0Sstevel@tonic-gate if (s == NULL) 237*0Sstevel@tonic-gate break; 238*0Sstevel@tonic-gate (void) printf("> %s", s); 239*0Sstevel@tonic-gate clrl(1, n1+i); 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate diffFound = 1; 242*0Sstevel@tonic-gate n1 += i-1; 243*0Sstevel@tonic-gate return (1); 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate static void 247*0Sstevel@tonic-gate change(long a, int b, long c, int d, char *s) 248*0Sstevel@tonic-gate { 249*0Sstevel@tonic-gate range(a, b); 250*0Sstevel@tonic-gate (void) printf("%s", s); 251*0Sstevel@tonic-gate range(c, d); 252*0Sstevel@tonic-gate (void) printf("\n"); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate static void 256*0Sstevel@tonic-gate range(long a, int b) 257*0Sstevel@tonic-gate { 258*0Sstevel@tonic-gate if (b == INF) 259*0Sstevel@tonic-gate (void) printf("%ld,$", a); 260*0Sstevel@tonic-gate else if (b == 0) 261*0Sstevel@tonic-gate (void) printf("%ld", a); 262*0Sstevel@tonic-gate else 263*0Sstevel@tonic-gate (void) printf("%ld,%ld", a, a+b); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate static int 267*0Sstevel@tonic-gate cmp(char *s, char *t) 268*0Sstevel@tonic-gate { 269*0Sstevel@tonic-gate if (debug) 270*0Sstevel@tonic-gate (void) printf("%s:%s\n", s, t); 271*0Sstevel@tonic-gate for (;;) { 272*0Sstevel@tonic-gate if (bflag && isspace(*s) && isspace(*t)) { 273*0Sstevel@tonic-gate while (isspace(*++s)) 274*0Sstevel@tonic-gate ; 275*0Sstevel@tonic-gate while (isspace(*++t)) 276*0Sstevel@tonic-gate ; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate if (*s != *t || *s == 0) 279*0Sstevel@tonic-gate break; 280*0Sstevel@tonic-gate s++; 281*0Sstevel@tonic-gate t++; 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate return (*s-*t); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate static FILE * 287*0Sstevel@tonic-gate dopen(char *f1, char *f2) 288*0Sstevel@tonic-gate { 289*0Sstevel@tonic-gate FILE *f; 290*0Sstevel@tonic-gate char b[PATH_MAX], *bptr, *eptr; 291*0Sstevel@tonic-gate struct stat statbuf; 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate if (cmp(f1, "-") == 0) { 294*0Sstevel@tonic-gate if (cmp(f2, "-") == 0) 295*0Sstevel@tonic-gate error(gettext("can't do - -")); 296*0Sstevel@tonic-gate else { 297*0Sstevel@tonic-gate if (fstat(fileno(stdin), &statbuf) == -1) 298*0Sstevel@tonic-gate error(gettext("can't access stdin")); 299*0Sstevel@tonic-gate else 300*0Sstevel@tonic-gate return (stdin); 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate if (stat(f1, &statbuf) == -1) 304*0Sstevel@tonic-gate error(gettext("can't access %s"), f1); 305*0Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) == S_IFDIR) { 306*0Sstevel@tonic-gate for (bptr = b; *bptr = *f1++; bptr++) 307*0Sstevel@tonic-gate ; 308*0Sstevel@tonic-gate *bptr++ = '/'; 309*0Sstevel@tonic-gate for (eptr = f2; *eptr; eptr++) 310*0Sstevel@tonic-gate if (*eptr == '/' && eptr[1] != 0 && eptr[1] != '/') 311*0Sstevel@tonic-gate f2 = eptr+1; 312*0Sstevel@tonic-gate while (*bptr++ = *f2++) 313*0Sstevel@tonic-gate ; 314*0Sstevel@tonic-gate f1 = b; 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate f = fopen(f1, "r"); 317*0Sstevel@tonic-gate if (f == NULL) 318*0Sstevel@tonic-gate error(gettext("can't open %s"), f1); 319*0Sstevel@tonic-gate return (f); 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate static void 324*0Sstevel@tonic-gate progerr(char *s) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate error(gettext("program error %s"), s); 327*0Sstevel@tonic-gate } 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate static void 330*0Sstevel@tonic-gate error(char *err, ...) 331*0Sstevel@tonic-gate { 332*0Sstevel@tonic-gate va_list ap; 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate va_start(ap, err); 335*0Sstevel@tonic-gate (void) fprintf(stderr, "diffh: "); 336*0Sstevel@tonic-gate (void) vfprintf(stderr, err, ap); 337*0Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 338*0Sstevel@tonic-gate va_end(ap); 339*0Sstevel@tonic-gate exit(2); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate /* stub for resychronization beyond limits of text buf */ 343*0Sstevel@tonic-gate static int 344*0Sstevel@tonic-gate hardsynch() 345*0Sstevel@tonic-gate { 346*0Sstevel@tonic-gate change(n0, INF, n1, INF, "c"); 347*0Sstevel@tonic-gate (void) printf(gettext("---change record omitted\n")); 348*0Sstevel@tonic-gate error(gettext("can't resynchronize")); 349*0Sstevel@tonic-gate return (0); 350*0Sstevel@tonic-gate } 351