summaryrefslogtreecommitdiff
path: root/src/lib/libcmd/cmp.c
blob: 7433020445a8256ee1be1b94453ac87e21c27fdb (plain)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1992-2012 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * David Korn
 * Glenn Fowler
 * AT&T Bell Laboratories
 *
 * cmp
 */

static const char usage[] =
"[-?\n@(#)$Id: cmp (AT&T Research) 2010-04-11 $\n]"
USAGE_LICENSE
"[+NAME?cmp - compare two files]"
"[+DESCRIPTION?\bcmp\b compares two files \afile1\a and \afile2\a. "
    "\bcmp\b writes no output if the files are the same. By default, if the "
    "files differ, the byte and line number at which the first difference "
    "occurred are written to standard output. Bytes and lines are numbered "
    "beginning with 1.]"
"[+?If \askip1\a or \askip2\a are specified, or the \b-i\b option is "
    "specified, initial bytes of the corresponding file are skipped before "
    "beginning the compare. The skip values are in bytes or can have a "
    "suffix of \bk\b for kilobytes or \bm\b for megabytes.]"
"[+?If either \afile1\a or \afiles2\a is \b-\b, \bcmp\b uses standard "
    "input starting at the current location.]"
"[b:print-bytes?Print differing bytes as 3 digit octal values.]"
"[c:print-chars?Print differing bytes as follows: non-space printable "
    "characters as themselves; space and control characters as \b^\b "
    "followed by a letter of the alphabet; and characters with the high bit "
    "set as the lower 7 bit character prefixed by \bM^\b for 7 bit space and "
    "non-printable characters and \bM-\b for all other characters. If the 7 "
    "bit character encoding is not ASCII then the characters are converted "
    "to ASCII to determine \ahigh bit set\a, and if set it is cleared and "
    "converted back to the native encoding. Multibyte characters in the "
    "current locale are treated as printable characters.]"
"[d:differences?Print at most \adifferences\a differences using "
    "\b--verbose\b output format. \b--differences=0\b is equivalent to "
    "\b--silent\b.]#[differences]"
"[i:ignore-initial|skip?Skip the the first \askip1\a bytes in \afile1\a "
    "and the first \askip2\a bytes in \afile2\a. If \askip2\a is omitted "
    "then \askip1\a is used.]:[skip1[::skip2]]:=0::0]"
"[l:verbose?Write the decimal byte number and the differing bytes (in "
    "octal) for each difference.]"
"[n:count|bytes?Compare at most \acount\a bytes.]#[count]"
"[s:quiet|silent?Write nothing for differing files; return non-zero exit "
    "status only.]"
"\n"
"\nfile1 file2 [skip1 [skip2]]\n"
"\n"
"[+EXIT STATUS?]"
    "{"
        "[+0?The files or portions compared are identical.]"
        "[+1?The files are different.]"
        "[+>1?An error occurred.]"
    "}"
"[+SEE ALSO?\bcomm\b(1), \bdiff\b(1), \bcat\b(1)]"
;

#include <cmd.h>
#include <ls.h>
#include <ctype.h>
#include <ccode.h>

#define CMP_VERBOSE	0x01
#define CMP_SILENT	0x02
#define CMP_CHARS	0x04
#define CMP_BYTES	0x08

static void
pretty(Sfio_t *out, int o, int delim, int flags)
{
	int	c;
	int	m;
	char*	s;
	char	buf[10];

	s = buf;
	if ((flags & CMP_BYTES) || !(flags & CMP_CHARS))
	{
		*s++ = ' ';
		if ((flags & CMP_CHARS) && delim != -1)
			*s++ = ' ';
		*s++ = '0' + ((o >> 6) & 07);
		*s++ = '0' + ((o >> 3) & 07);
		*s++ = '0' + (o & 07);
	}
	if (flags & CMP_CHARS)
	{
		*s++ = ' ';
		c = ccmapc(o, CC_NATIVE, CC_ASCII);
		if (c & 0x80)
		{
			m = 1;
			*s++ = 'M';
			c &= 0x7f;
			o = ccmapc(c, CC_ASCII, CC_NATIVE);
		}
		else
			m = 0;
		if (isspace(o) || !isprint(o))
		{
			if (!m)
				*s++ = ' ';
			*s++ = '^';
			c ^= 0x40;
			o = ccmapc(c, CC_ASCII, CC_NATIVE);
		}
		else if (m)
			*s++ = '-';
		else
		{
			*s++ = ' ';
			*s++ = ' ';
		}
		*s++ = o;
	}
	*s = 0;
	sfputr(out, buf, delim);
}

/*
 * compare two files
 */

static int
cmp(const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags, Sfoff_t count, Sfoff_t differences)
{
	register int		c1;
	register int		c2;
	register unsigned char*	p1 = 0;
	register unsigned char*	p2 = 0;
	register Sfoff_t	lines = 1;
	register unsigned char*	e1 = 0;
	register unsigned char*	e2 = 0;
	Sfoff_t			pos = 0;
	int			n1 = 0;
	int			ret = 0;
	unsigned char*		last;

	for (;;)
	{
		if ((c1 = e1 - p1) <= 0)
		{
			if (count > 0 && !(count -= n1))
				return ret;
			if (!(p1 = (unsigned char*)sfreserve(f1, SF_UNBOUND, 0)) || (c1 = sfvalue(f1)) <= 0)
			{
				if ((e2 - p2) > 0 || sfreserve(f2, SF_UNBOUND, 0) && sfvalue(f2) > 0)
				{
					ret = 1;
					if (!(flags & CMP_SILENT))
						error(ERROR_exit(1), "EOF on %s", file1);
				}
				return ret;
			}
			if (count > 0 && c1 > count)
				c1 = (int)count;
			e1 = p1 + c1;
			n1 = c1;
		}
		if ((c2 = e2 - p2) <= 0)
		{
			if (!(p2 = (unsigned char*)sfreserve(f2, SF_UNBOUND, 0)) || (c2 = sfvalue(f2)) <= 0)
			{
				if (!(flags & CMP_SILENT))
					error(ERROR_exit(1), "EOF on %s", file2);
				return 1;
			}
			e2 = p2 + c2;
		}
		if (c1 > c2)
			c1 = c2;
		pos += c1;
		if (flags & CMP_SILENT)
		{
			if (memcmp(p1, p2, c1))
				return 1;
			p1 += c1;
			p2 += c1;
		}
		else
		{
			last = p1 + c1;
			while (p1 < last)
			{
				if ((c1 = *p1++) != *p2++)
				{
					if (differences >= 0)
					{
						if (!differences)
							return 1;
						differences--;
					}
#if 0
					if (!flags)
						sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u\n", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
					else
					{
						sfprintf(sfstdout, "%6I*d", sizeof(pos), pos - (last - p1));
						pretty(sfstdout, c1, -1, flags);
						pretty(sfstdout, *(p2-1), '\n', flags);
					}
#else
					if (flags & CMP_VERBOSE)
						sfprintf(sfstdout, "%6I*d", sizeof(pos), pos - (last - p1));
					else
						sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
					if (flags & (CMP_BYTES|CMP_CHARS|CMP_VERBOSE))
					{
						sfputc(sfstdout, (flags & CMP_VERBOSE) ? ' ' : ',');
						pretty(sfstdout, c1, -1, flags);
						pretty(sfstdout, *(p2-1), '\n', flags);
					}
					else
						sfputc(sfstdout, '\n');
#endif
					if (!differences || differences < 0 && !(flags & CMP_VERBOSE))
						return 1;
					ret = 1;
				}
				if (c1 == '\n')
					lines++;
			}
		}
	}
}

int
b_cmp(int argc, register char** argv, Shbltin_t* context)
{
	char*		s;
	char*		e;
	char*		file1;
	char*		file2;
	int		n;
	struct stat	s1;
	struct stat	s2;

	Sfio_t*		f1 = 0;
	Sfio_t*		f2 = 0;
	Sfoff_t		o1 = 0;
	Sfoff_t		o2 = 0;
	Sfoff_t		count = -1;
	Sfoff_t		differences = -1;
	int		flags = 0;

	NoP(argc);
	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
	for (;;)
	{
		switch (optget(argv, usage))
		{
		case 'b':
			flags |= CMP_BYTES;
			continue;
		case 'c':
			flags |= CMP_CHARS;
			continue;
		case 'd':
			flags |= CMP_VERBOSE;
			differences = opt_info.number;
			continue;
		case 'i':
			o1 = strtoll(opt_info.arg, &e, 0);
			if (*e == ':')
				o2 = strtoll(e + 1, &e, 0);
			else
				o2 = o1;
			if (*e)
			{
				error(2, "%s: skip1:skip2 expected", opt_info.arg);
				break;
			}
			continue;
		case 'l':
			flags |= CMP_VERBOSE;
			continue;
		case 'n':
			count = opt_info.number;
			continue;
		case 's':
			flags |= CMP_SILENT;
			continue;
		case ':':
			error(2, "%s", opt_info.arg);
			break;
		case '?':
			error(ERROR_usage(2), "%s", opt_info.arg);
			break;
		}
		break;
	}
	argv += opt_info.index;
	if (error_info.errors || !(file1 = *argv++) || !(file2 = *argv++))
		error(ERROR_usage(2), "%s", optusage(NiL));
	n = 2;
	if (streq(file1, "-"))
		f1 = sfstdin;
	else if (!(f1 = sfopen(NiL, file1, "r")))
	{
		if (!(flags & CMP_SILENT))
			error(ERROR_system(0), "%s: cannot open", file1);
		goto done;
	}
	if (streq(file2, "-"))
		f2 = sfstdin;
	else if (!(f2 = sfopen(NiL, file2, "r")))
	{
		if (!(flags & CMP_SILENT))
			error(ERROR_system(0), "%s: cannot open", file2);
		goto done;
	}
	if (s = *argv++)
	{
		o1 = strtoll(s, &e, 0);
		if (*e)
		{
			error(ERROR_exit(0), "%s: %s: invalid skip", file1, s);
			goto done;
		}
		if (s = *argv++)
		{
			o2 = strtoll(s, &e, 0);
			if (*e)
			{
				error(ERROR_exit(0), "%s: %s: invalid skip", file2, s);
				goto done;
			}
		}
		if (*argv)
		{
			error(ERROR_usage(0), "%s", optusage(NiL));
			goto done;
		}
	}
	if (o1 && sfseek(f1, o1, SEEK_SET) != o1)
	{
		if (!(flags & CMP_SILENT))
			error(ERROR_exit(0), "EOF on %s", file1);
		n = 1;
		goto done;
	}
	if (o2 && sfseek(f2, o2, SEEK_SET) != o2)
	{
		if (!(flags & CMP_SILENT))
			error(ERROR_exit(0), "EOF on %s", file2);
		n = 1;
		goto done;
	}
	if (fstat(sffileno(f1), &s1))
		error(ERROR_system(0), "%s: cannot stat", file1);
	else if (fstat(sffileno(f2), &s2))
		error(ERROR_system(0), "%s: cannot stat", file1);
	else if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev && o1 == o2)
		n = 0;
	else
		n = ((flags & CMP_SILENT) && S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode) && (s1.st_size - o1) != (s2.st_size - o2)) ? 1 : cmp(file1, f1, file2, f2, flags, count, differences);
 done:
	if (f1 && f1 != sfstdin)
		sfclose(f1);
	if (f2 && f2 != sfstdin)
		sfclose(f2);
	return n;
}