1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdarg.h>
#define C 3
#define RANGE 30
#define LEN 255
#define INF 16384
char *text[2][RANGE];
long lineno[2] = {1, 1}; /* no. of 1st stored line in each file */
int ntext[2]; /* number of stored lines in each */
long n0, n1; /* scan pointer in each */
int bflag;
int debug = 0;
FILE *file[2];
static int diffFound = 0;
static char *getl(int f, long n);
static void clrl(int f, long n);
static void movstr(char *s, char *t);
static int easysynch(void);
static int output(int a, int b);
static void change(long a, int b, long c, int d, char *s);
static void range(long a, int b);
static int cmp(char *s, char *t);
static FILE *dopen(char *f1, char *f2);
static void progerr(char *s);
static void error(char *err, ...);
static int hardsynch(void);
/* return pointer to line n of file f */
static char *
getl(int f, long n)
{
char *t;
int delta, nt;
again:
delta = n - lineno[f];
nt = ntext[f];
if (delta < 0)
progerr("1");
if (delta < nt)
return (text[f][delta]);
if (delta > nt)
progerr("2");
if (nt >= RANGE)
progerr("3");
if (feof(file[f]))
return (NULL);
t = text[f][nt];
if (t == 0) {
t = text[f][nt] = (char *)malloc(LEN+1);
if (t == NULL)
if (hardsynch())
goto again;
else
progerr("5");
}
t = fgets(t, LEN, file[f]);
if (t != NULL)
ntext[f]++;
return (t);
}
/* remove thru line n of file f from storage */
static void
clrl(int f, long n)
{
int i, j;
j = n-lineno[f]+1;
for (i = 0; i+j < ntext[f]; i++)
movstr(text[f][i+j], text[f][i]);
lineno[f] = n+1;
ntext[f] -= j;
}
static void
movstr(char *s, char *t)
{
while (*t++ = *s++)
continue;
}
int
main(int argc, char **argv)
{
char *s0, *s1;
if ((argc > 1) && (*argv[1] == '-')) {
argc--;
argv++;
while (*++argv[0])
if (*argv[0] == 'b')
bflag++;
}
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
if (argc != 3)
error(gettext("must have 2 file arguments"));
file[0] = dopen(argv[1], argv[2]);
file[1] = dopen(argv[2], argv[1]);
for (;;) {
s0 = getl(0, ++n0);
s1 = getl(1, ++n1);
if (s0 == NULL || s1 == NULL)
break;
if (cmp(s0, s1) != 0) {
if (!easysynch() && !hardsynch())
progerr("5");
} else {
clrl(0, n0);
clrl(1, n1);
}
}
/* diff is expected to return 1 if the files differ */
if (s0 == NULL && s1 == NULL)
return (diffFound);
if (s0 == NULL) {
(void) output(-1, INF);
return (1);
}
if (s1 == NULL) {
(void) output(INF, -1);
return (1);
}
/* NOTREACHED */
return (0);
}
/* synch on C successive matches */
static int
easysynch()
{
int i, j;
int k, m;
char *s0, *s1;
for (i = j = 1; i < RANGE && j < RANGE; i++, j++) {
s0 = getl(0, n0+i);
if (s0 == NULL)
return (output(INF, INF));
for (k = C-1; k < j; k++) {
for (m = 0; m < C; m++)
if (cmp(getl(0, n0+i-m),
getl(1, n1+k-m)) != 0)
goto cont1;
return (output(i-C, k-C));
cont1:
;
}
s1 = getl(1, n1+j);
if (s1 == NULL)
return (output(INF, INF));
for (k = C-1; k <= i; k++) {
for (m = 0; m < C; m++)
if (cmp(getl(0, n0+k-m),
getl(1, n1+j-m)) != 0)
goto cont2;
return (output(k-C, j-C));
cont2:
;
}
}
return (0);
}
static int
output(int a, int b)
{
int i;
char *s;
if (a < 0)
change(n0-1, 0, n1, b, "a");
else if (b < 0)
change(n0, a, n1-1, 0, "d");
else
change(n0, a, n1, b, "c");
for (i = 0; i <= a; i++) {
s = getl(0, n0+i);
if (s == NULL)
break;
(void) printf("< %s", s);
clrl(0, n0+i);
}
n0 += i-1;
if (a >= 0 && b >= 0)
(void) printf("---\n");
for (i = 0; i <= b; i++) {
s = getl(1, n1+i);
if (s == NULL)
break;
(void) printf("> %s", s);
clrl(1, n1+i);
}
diffFound = 1;
n1 += i-1;
return (1);
}
static void
change(long a, int b, long c, int d, char *s)
{
range(a, b);
(void) printf("%s", s);
range(c, d);
(void) printf("\n");
}
static void
range(long a, int b)
{
if (b == INF)
(void) printf("%ld,$", a);
else if (b == 0)
(void) printf("%ld", a);
else
(void) printf("%ld,%ld", a, a+b);
}
static int
cmp(char *s, char *t)
{
if (debug)
(void) printf("%s:%s\n", s, t);
for (;;) {
if (bflag && isspace(*s) && isspace(*t)) {
while (isspace(*++s))
;
while (isspace(*++t))
;
}
if (*s != *t || *s == 0)
break;
s++;
t++;
}
return (*s-*t);
}
static FILE *
dopen(char *f1, char *f2)
{
FILE *f;
char b[PATH_MAX], *bptr, *eptr;
struct stat statbuf;
if (cmp(f1, "-") == 0) {
if (cmp(f2, "-") == 0)
error(gettext("can't do - -"));
else {
if (fstat(fileno(stdin), &statbuf) == -1)
error(gettext("can't access stdin"));
else
return (stdin);
}
}
if (stat(f1, &statbuf) == -1)
error(gettext("can't access %s"), f1);
if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
for (bptr = b; *bptr = *f1++; bptr++)
;
*bptr++ = '/';
for (eptr = f2; *eptr; eptr++)
if (*eptr == '/' && eptr[1] != 0 && eptr[1] != '/')
f2 = eptr+1;
while (*bptr++ = *f2++)
;
f1 = b;
}
f = fopen(f1, "r");
if (f == NULL)
error(gettext("can't open %s"), f1);
return (f);
}
static void
progerr(char *s)
{
error(gettext("program error %s"), s);
}
static void
error(char *err, ...)
{
va_list ap;
va_start(ap, err);
(void) fprintf(stderr, "diffh: ");
(void) vfprintf(stderr, err, ap);
(void) fprintf(stderr, "\n");
va_end(ap);
exit(2);
}
/* stub for resychronization beyond limits of text buf */
static int
hardsynch()
{
change(n0, INF, n1, INF, "c");
(void) printf(gettext("---change record omitted\n"));
error(gettext("can't resynchronize"));
return (0);
}
|