xref: /illumos-gate/usr/src/contrib/ast/src/lib/libcmd/cmp.c (revision b30d1939)
1*b30d1939SAndy Fiddaman /*
2*b30d1939SAndy Fiddaman  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3*b30d1939SAndy Fiddaman  */
4*b30d1939SAndy Fiddaman 
5*b30d1939SAndy Fiddaman /***********************************************************************
6*b30d1939SAndy Fiddaman *                                                                      *
7*b30d1939SAndy Fiddaman *               This software is part of the ast package               *
8*b30d1939SAndy Fiddaman *          Copyright (c) 1992-2012 AT&T Intellectual Property          *
9*b30d1939SAndy Fiddaman *                      and is licensed under the                       *
10*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
11*b30d1939SAndy Fiddaman *                    by AT&T Intellectual Property                     *
12*b30d1939SAndy Fiddaman *                                                                      *
13*b30d1939SAndy Fiddaman *                A copy of the License is available at                 *
14*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
15*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
16*b30d1939SAndy Fiddaman *                                                                      *
17*b30d1939SAndy Fiddaman *              Information and Software Systems Research               *
18*b30d1939SAndy Fiddaman *                            AT&T Research                             *
19*b30d1939SAndy Fiddaman *                           Florham Park NJ                            *
20*b30d1939SAndy Fiddaman *                                                                      *
21*b30d1939SAndy Fiddaman *                 Glenn Fowler <gsf@research.att.com>                  *
22*b30d1939SAndy Fiddaman *                  David Korn <dgk@research.att.com>                   *
23*b30d1939SAndy Fiddaman *                                                                      *
24*b30d1939SAndy Fiddaman ***********************************************************************/
25*b30d1939SAndy Fiddaman #pragma prototyped
26*b30d1939SAndy Fiddaman /*
27*b30d1939SAndy Fiddaman  * David Korn
28*b30d1939SAndy Fiddaman  * Glenn Fowler
29*b30d1939SAndy Fiddaman  * AT&T Bell Laboratories
30*b30d1939SAndy Fiddaman  *
31*b30d1939SAndy Fiddaman  * cmp
32*b30d1939SAndy Fiddaman  */
33*b30d1939SAndy Fiddaman 
34*b30d1939SAndy Fiddaman static const char usage[] =
35*b30d1939SAndy Fiddaman "[-?\n@(#)$Id: cmp (AT&T Research) 2010-04-11 $\n]"
36*b30d1939SAndy Fiddaman USAGE_LICENSE
37*b30d1939SAndy Fiddaman "[+NAME?cmp - compare two files]"
38*b30d1939SAndy Fiddaman "[+DESCRIPTION?\bcmp\b compares two files \afile1\a and \afile2\a. "
39*b30d1939SAndy Fiddaman     "\bcmp\b writes no output if the files are the same. By default, if the "
40*b30d1939SAndy Fiddaman     "files differ, the byte and line number at which the first difference "
41*b30d1939SAndy Fiddaman     "occurred are written to standard output. Bytes and lines are numbered "
42*b30d1939SAndy Fiddaman     "beginning with 1.]"
43*b30d1939SAndy Fiddaman "[+?If \askip1\a or \askip2\a are specified, or the \b-i\b option is "
44*b30d1939SAndy Fiddaman     "specified, initial bytes of the corresponding file are skipped before "
45*b30d1939SAndy Fiddaman     "beginning the compare. The skip values are in bytes or can have a "
46*b30d1939SAndy Fiddaman     "suffix of \bk\b for kilobytes or \bm\b for megabytes.]"
47*b30d1939SAndy Fiddaman "[+?If either \afile1\a or \afiles2\a is \b-\b, \bcmp\b uses standard "
48*b30d1939SAndy Fiddaman     "input starting at the current location.]"
49*b30d1939SAndy Fiddaman "[b:print-bytes?Print differing bytes as 3 digit octal values.]"
50*b30d1939SAndy Fiddaman "[c:print-chars?Print differing bytes as follows: non-space printable "
51*b30d1939SAndy Fiddaman     "characters as themselves; space and control characters as \b^\b "
52*b30d1939SAndy Fiddaman     "followed by a letter of the alphabet; and characters with the high bit "
53*b30d1939SAndy Fiddaman     "set as the lower 7 bit character prefixed by \bM^\b for 7 bit space and "
54*b30d1939SAndy Fiddaman     "non-printable characters and \bM-\b for all other characters. If the 7 "
55*b30d1939SAndy Fiddaman     "bit character encoding is not ASCII then the characters are converted "
56*b30d1939SAndy Fiddaman     "to ASCII to determine \ahigh bit set\a, and if set it is cleared and "
57*b30d1939SAndy Fiddaman     "converted back to the native encoding. Multibyte characters in the "
58*b30d1939SAndy Fiddaman     "current locale are treated as printable characters.]"
59*b30d1939SAndy Fiddaman "[d:differences?Print at most \adifferences\a differences using "
60*b30d1939SAndy Fiddaman     "\b--verbose\b output format. \b--differences=0\b is equivalent to "
61*b30d1939SAndy Fiddaman     "\b--silent\b.]#[differences]"
62*b30d1939SAndy Fiddaman "[i:ignore-initial|skip?Skip the the first \askip1\a bytes in \afile1\a "
63*b30d1939SAndy Fiddaman     "and the first \askip2\a bytes in \afile2\a. If \askip2\a is omitted "
64*b30d1939SAndy Fiddaman     "then \askip1\a is used.]:[skip1[::skip2]]:=0::0]"
65*b30d1939SAndy Fiddaman "[l:verbose?Write the decimal byte number and the differing bytes (in "
66*b30d1939SAndy Fiddaman     "octal) for each difference.]"
67*b30d1939SAndy Fiddaman "[n:count|bytes?Compare at most \acount\a bytes.]#[count]"
68*b30d1939SAndy Fiddaman "[s:quiet|silent?Write nothing for differing files; return non-zero exit "
69*b30d1939SAndy Fiddaman     "status only.]"
70*b30d1939SAndy Fiddaman "\n"
71*b30d1939SAndy Fiddaman "\nfile1 file2 [skip1 [skip2]]\n"
72*b30d1939SAndy Fiddaman "\n"
73*b30d1939SAndy Fiddaman "[+EXIT STATUS?]"
74*b30d1939SAndy Fiddaman     "{"
75*b30d1939SAndy Fiddaman         "[+0?The files or portions compared are identical.]"
76*b30d1939SAndy Fiddaman         "[+1?The files are different.]"
77*b30d1939SAndy Fiddaman         "[+>1?An error occurred.]"
78*b30d1939SAndy Fiddaman     "}"
79*b30d1939SAndy Fiddaman "[+SEE ALSO?\bcomm\b(1), \bdiff\b(1), \bcat\b(1)]"
80*b30d1939SAndy Fiddaman ;
81*b30d1939SAndy Fiddaman 
82*b30d1939SAndy Fiddaman #include <cmd.h>
83*b30d1939SAndy Fiddaman #include <ls.h>
84*b30d1939SAndy Fiddaman #include <ctype.h>
85*b30d1939SAndy Fiddaman #include <ccode.h>
86*b30d1939SAndy Fiddaman 
87*b30d1939SAndy Fiddaman #define CMP_VERBOSE	0x01
88*b30d1939SAndy Fiddaman #define CMP_SILENT	0x02
89*b30d1939SAndy Fiddaman #define CMP_CHARS	0x04
90*b30d1939SAndy Fiddaman #define CMP_BYTES	0x08
91*b30d1939SAndy Fiddaman 
92*b30d1939SAndy Fiddaman static void
pretty(Sfio_t * out,int o,int delim,int flags)93*b30d1939SAndy Fiddaman pretty(Sfio_t *out, int o, int delim, int flags)
94*b30d1939SAndy Fiddaman {
95*b30d1939SAndy Fiddaman 	int	c;
96*b30d1939SAndy Fiddaman 	int	m;
97*b30d1939SAndy Fiddaman 	char*	s;
98*b30d1939SAndy Fiddaman 	char	buf[10];
99*b30d1939SAndy Fiddaman 
100*b30d1939SAndy Fiddaman 	s = buf;
101*b30d1939SAndy Fiddaman 	if ((flags & CMP_BYTES) || !(flags & CMP_CHARS))
102*b30d1939SAndy Fiddaman 	{
103*b30d1939SAndy Fiddaman 		*s++ = ' ';
104*b30d1939SAndy Fiddaman 		if ((flags & CMP_CHARS) && delim != -1)
105*b30d1939SAndy Fiddaman 			*s++ = ' ';
106*b30d1939SAndy Fiddaman 		*s++ = '0' + ((o >> 6) & 07);
107*b30d1939SAndy Fiddaman 		*s++ = '0' + ((o >> 3) & 07);
108*b30d1939SAndy Fiddaman 		*s++ = '0' + (o & 07);
109*b30d1939SAndy Fiddaman 	}
110*b30d1939SAndy Fiddaman 	if (flags & CMP_CHARS)
111*b30d1939SAndy Fiddaman 	{
112*b30d1939SAndy Fiddaman 		*s++ = ' ';
113*b30d1939SAndy Fiddaman 		c = ccmapc(o, CC_NATIVE, CC_ASCII);
114*b30d1939SAndy Fiddaman 		if (c & 0x80)
115*b30d1939SAndy Fiddaman 		{
116*b30d1939SAndy Fiddaman 			m = 1;
117*b30d1939SAndy Fiddaman 			*s++ = 'M';
118*b30d1939SAndy Fiddaman 			c &= 0x7f;
119*b30d1939SAndy Fiddaman 			o = ccmapc(c, CC_ASCII, CC_NATIVE);
120*b30d1939SAndy Fiddaman 		}
121*b30d1939SAndy Fiddaman 		else
122*b30d1939SAndy Fiddaman 			m = 0;
123*b30d1939SAndy Fiddaman 		if (isspace(o) || !isprint(o))
124*b30d1939SAndy Fiddaman 		{
125*b30d1939SAndy Fiddaman 			if (!m)
126*b30d1939SAndy Fiddaman 				*s++ = ' ';
127*b30d1939SAndy Fiddaman 			*s++ = '^';
128*b30d1939SAndy Fiddaman 			c ^= 0x40;
129*b30d1939SAndy Fiddaman 			o = ccmapc(c, CC_ASCII, CC_NATIVE);
130*b30d1939SAndy Fiddaman 		}
131*b30d1939SAndy Fiddaman 		else if (m)
132*b30d1939SAndy Fiddaman 			*s++ = '-';
133*b30d1939SAndy Fiddaman 		else
134*b30d1939SAndy Fiddaman 		{
135*b30d1939SAndy Fiddaman 			*s++ = ' ';
136*b30d1939SAndy Fiddaman 			*s++ = ' ';
137*b30d1939SAndy Fiddaman 		}
138*b30d1939SAndy Fiddaman 		*s++ = o;
139*b30d1939SAndy Fiddaman 	}
140*b30d1939SAndy Fiddaman 	*s = 0;
141*b30d1939SAndy Fiddaman 	sfputr(out, buf, delim);
142*b30d1939SAndy Fiddaman }
143*b30d1939SAndy Fiddaman 
144*b30d1939SAndy Fiddaman /*
145*b30d1939SAndy Fiddaman  * compare two files
146*b30d1939SAndy Fiddaman  */
147*b30d1939SAndy Fiddaman 
148*b30d1939SAndy Fiddaman static int
cmp(const char * file1,Sfio_t * f1,const char * file2,Sfio_t * f2,int flags,Sfoff_t count,Sfoff_t differences)149*b30d1939SAndy Fiddaman cmp(const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags, Sfoff_t count, Sfoff_t differences)
150*b30d1939SAndy Fiddaman {
151*b30d1939SAndy Fiddaman 	register int		c1;
152*b30d1939SAndy Fiddaman 	register int		c2;
153*b30d1939SAndy Fiddaman 	register unsigned char*	p1 = 0;
154*b30d1939SAndy Fiddaman 	register unsigned char*	p2 = 0;
155*b30d1939SAndy Fiddaman 	register Sfoff_t	lines = 1;
156*b30d1939SAndy Fiddaman 	register unsigned char*	e1 = 0;
157*b30d1939SAndy Fiddaman 	register unsigned char*	e2 = 0;
158*b30d1939SAndy Fiddaman 	Sfoff_t			pos = 0;
159*b30d1939SAndy Fiddaman 	int			n1 = 0;
160*b30d1939SAndy Fiddaman 	int			ret = 0;
161*b30d1939SAndy Fiddaman 	unsigned char*		last;
162*b30d1939SAndy Fiddaman 
163*b30d1939SAndy Fiddaman 	for (;;)
164*b30d1939SAndy Fiddaman 	{
165*b30d1939SAndy Fiddaman 		if ((c1 = e1 - p1) <= 0)
166*b30d1939SAndy Fiddaman 		{
167*b30d1939SAndy Fiddaman 			if (count > 0 && !(count -= n1))
168*b30d1939SAndy Fiddaman 				return ret;
169*b30d1939SAndy Fiddaman 			if (!(p1 = (unsigned char*)sfreserve(f1, SF_UNBOUND, 0)) || (c1 = sfvalue(f1)) <= 0)
170*b30d1939SAndy Fiddaman 			{
171*b30d1939SAndy Fiddaman 				if (sferror(f1)) {
172*b30d1939SAndy Fiddaman 					error(ERROR_exit(2),
173*b30d1939SAndy Fiddaman 					    "read error on %s", file1);
174*b30d1939SAndy Fiddaman 				}
175*b30d1939SAndy Fiddaman 				if ((e2 - p2) > 0 || sfreserve(f2, SF_UNBOUND, 0) && sfvalue(f2) > 0)
176*b30d1939SAndy Fiddaman 				{
177*b30d1939SAndy Fiddaman 					ret = 1;
178*b30d1939SAndy Fiddaman 					if (!(flags & CMP_SILENT))
179*b30d1939SAndy Fiddaman 						error(ERROR_exit(1), "EOF on %s", file1);
180*b30d1939SAndy Fiddaman 				}
181*b30d1939SAndy Fiddaman 				if (sferror(f2)) {
182*b30d1939SAndy Fiddaman 					error(ERROR_exit(2),
183*b30d1939SAndy Fiddaman 					    "read error on %s", file2);
184*b30d1939SAndy Fiddaman 				}
185*b30d1939SAndy Fiddaman 				return ret;
186*b30d1939SAndy Fiddaman 			}
187*b30d1939SAndy Fiddaman 			if (count > 0 && c1 > count)
188*b30d1939SAndy Fiddaman 				c1 = (int)count;
189*b30d1939SAndy Fiddaman 			e1 = p1 + c1;
190*b30d1939SAndy Fiddaman 			n1 = c1;
191*b30d1939SAndy Fiddaman 		}
192*b30d1939SAndy Fiddaman 		if ((c2 = e2 - p2) <= 0)
193*b30d1939SAndy Fiddaman 		{
194*b30d1939SAndy Fiddaman 			if (!(p2 = (unsigned char*)sfreserve(f2, SF_UNBOUND, 0)) || (c2 = sfvalue(f2)) <= 0)
195*b30d1939SAndy Fiddaman 			{
196*b30d1939SAndy Fiddaman 				if (sferror(f2)) {
197*b30d1939SAndy Fiddaman 					error(ERROR_exit(2),
198*b30d1939SAndy Fiddaman 					    "read error on %s", file2);
199*b30d1939SAndy Fiddaman 				}
200*b30d1939SAndy Fiddaman 				if (!(flags & CMP_SILENT))
201*b30d1939SAndy Fiddaman 					error(ERROR_exit(1), "EOF on %s", file2);
202*b30d1939SAndy Fiddaman 				return 1;
203*b30d1939SAndy Fiddaman 			}
204*b30d1939SAndy Fiddaman 			e2 = p2 + c2;
205*b30d1939SAndy Fiddaman 		}
206*b30d1939SAndy Fiddaman 		if (c1 > c2)
207*b30d1939SAndy Fiddaman 			c1 = c2;
208*b30d1939SAndy Fiddaman 		pos += c1;
209*b30d1939SAndy Fiddaman 		if (flags & CMP_SILENT)
210*b30d1939SAndy Fiddaman 		{
211*b30d1939SAndy Fiddaman 			if (memcmp(p1, p2, c1))
212*b30d1939SAndy Fiddaman 				return 1;
213*b30d1939SAndy Fiddaman 			p1 += c1;
214*b30d1939SAndy Fiddaman 			p2 += c1;
215*b30d1939SAndy Fiddaman 		}
216*b30d1939SAndy Fiddaman 		else
217*b30d1939SAndy Fiddaman 		{
218*b30d1939SAndy Fiddaman 			last = p1 + c1;
219*b30d1939SAndy Fiddaman 			while (p1 < last)
220*b30d1939SAndy Fiddaman 			{
221*b30d1939SAndy Fiddaman 				if ((c1 = *p1++) != *p2++)
222*b30d1939SAndy Fiddaman 				{
223*b30d1939SAndy Fiddaman 					if (differences >= 0)
224*b30d1939SAndy Fiddaman 					{
225*b30d1939SAndy Fiddaman 						if (!differences)
226*b30d1939SAndy Fiddaman 							return 1;
227*b30d1939SAndy Fiddaman 						differences--;
228*b30d1939SAndy Fiddaman 					}
229*b30d1939SAndy Fiddaman #if 0
230*b30d1939SAndy Fiddaman 					if (!flags)
231*b30d1939SAndy Fiddaman 						sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u\n", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
232*b30d1939SAndy Fiddaman 					else
233*b30d1939SAndy Fiddaman 					{
234*b30d1939SAndy Fiddaman 						sfprintf(sfstdout, "%6I*d", sizeof(pos), pos - (last - p1));
235*b30d1939SAndy Fiddaman 						pretty(sfstdout, c1, -1, flags);
236*b30d1939SAndy Fiddaman 						pretty(sfstdout, *(p2-1), '\n', flags);
237*b30d1939SAndy Fiddaman 					}
238*b30d1939SAndy Fiddaman #else
239*b30d1939SAndy Fiddaman 					if (flags & CMP_VERBOSE)
240*b30d1939SAndy Fiddaman 						sfprintf(sfstdout, "%6I*d", sizeof(pos), pos - (last - p1));
241*b30d1939SAndy Fiddaman 					else
242*b30d1939SAndy Fiddaman 						sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
243*b30d1939SAndy Fiddaman 					if (flags & (CMP_BYTES|CMP_CHARS|CMP_VERBOSE))
244*b30d1939SAndy Fiddaman 					{
245*b30d1939SAndy Fiddaman 						sfputc(sfstdout, (flags & CMP_VERBOSE) ? ' ' : ',');
246*b30d1939SAndy Fiddaman 						pretty(sfstdout, c1, -1, flags);
247*b30d1939SAndy Fiddaman 						pretty(sfstdout, *(p2-1), '\n', flags);
248*b30d1939SAndy Fiddaman 					}
249*b30d1939SAndy Fiddaman 					else
250*b30d1939SAndy Fiddaman 						sfputc(sfstdout, '\n');
251*b30d1939SAndy Fiddaman #endif
252*b30d1939SAndy Fiddaman 					if (!differences || differences < 0 && !(flags & CMP_VERBOSE))
253*b30d1939SAndy Fiddaman 						return 1;
254*b30d1939SAndy Fiddaman 					ret = 1;
255*b30d1939SAndy Fiddaman 				}
256*b30d1939SAndy Fiddaman 				if (c1 == '\n')
257*b30d1939SAndy Fiddaman 					lines++;
258*b30d1939SAndy Fiddaman 			}
259*b30d1939SAndy Fiddaman 		}
260*b30d1939SAndy Fiddaman 	}
261*b30d1939SAndy Fiddaman }
262*b30d1939SAndy Fiddaman 
263*b30d1939SAndy Fiddaman int
b_cmp(int argc,register char ** argv,Shbltin_t * context)264*b30d1939SAndy Fiddaman b_cmp(int argc, register char** argv, Shbltin_t* context)
265*b30d1939SAndy Fiddaman {
266*b30d1939SAndy Fiddaman 	char*		s;
267*b30d1939SAndy Fiddaman 	char*		e;
268*b30d1939SAndy Fiddaman 	char*		file1;
269*b30d1939SAndy Fiddaman 	char*		file2;
270*b30d1939SAndy Fiddaman 	int		n;
271*b30d1939SAndy Fiddaman 	struct stat	s1;
272*b30d1939SAndy Fiddaman 	struct stat	s2;
273*b30d1939SAndy Fiddaman 
274*b30d1939SAndy Fiddaman 	Sfio_t*		f1 = 0;
275*b30d1939SAndy Fiddaman 	Sfio_t*		f2 = 0;
276*b30d1939SAndy Fiddaman 	Sfoff_t		o1 = 0;
277*b30d1939SAndy Fiddaman 	Sfoff_t		o2 = 0;
278*b30d1939SAndy Fiddaman 	Sfoff_t		count = -1;
279*b30d1939SAndy Fiddaman 	Sfoff_t		differences = -1;
280*b30d1939SAndy Fiddaman 	int		flags = 0;
281*b30d1939SAndy Fiddaman 
282*b30d1939SAndy Fiddaman 	NoP(argc);
283*b30d1939SAndy Fiddaman 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
284*b30d1939SAndy Fiddaman 	for (;;)
285*b30d1939SAndy Fiddaman 	{
286*b30d1939SAndy Fiddaman 		switch (optget(argv, usage))
287*b30d1939SAndy Fiddaman 		{
288*b30d1939SAndy Fiddaman 		case 'b':
289*b30d1939SAndy Fiddaman 			flags |= CMP_BYTES;
290*b30d1939SAndy Fiddaman 			continue;
291*b30d1939SAndy Fiddaman 		case 'c':
292*b30d1939SAndy Fiddaman 			flags |= CMP_CHARS;
293*b30d1939SAndy Fiddaman 			continue;
294*b30d1939SAndy Fiddaman 		case 'd':
295*b30d1939SAndy Fiddaman 			flags |= CMP_VERBOSE;
296*b30d1939SAndy Fiddaman 			differences = opt_info.number;
297*b30d1939SAndy Fiddaman 			continue;
298*b30d1939SAndy Fiddaman 		case 'i':
299*b30d1939SAndy Fiddaman 			o1 = strtoll(opt_info.arg, &e, 0);
300*b30d1939SAndy Fiddaman 			if (*e == ':')
301*b30d1939SAndy Fiddaman 				o2 = strtoll(e + 1, &e, 0);
302*b30d1939SAndy Fiddaman 			else
303*b30d1939SAndy Fiddaman 				o2 = o1;
304*b30d1939SAndy Fiddaman 			if (*e)
305*b30d1939SAndy Fiddaman 			{
306*b30d1939SAndy Fiddaman 				error(2, "%s: skip1:skip2 expected", opt_info.arg);
307*b30d1939SAndy Fiddaman 				break;
308*b30d1939SAndy Fiddaman 			}
309*b30d1939SAndy Fiddaman 			continue;
310*b30d1939SAndy Fiddaman 		case 'l':
311*b30d1939SAndy Fiddaman 			flags |= CMP_VERBOSE;
312*b30d1939SAndy Fiddaman 			continue;
313*b30d1939SAndy Fiddaman 		case 'n':
314*b30d1939SAndy Fiddaman 			count = opt_info.number;
315*b30d1939SAndy Fiddaman 			continue;
316*b30d1939SAndy Fiddaman 		case 's':
317*b30d1939SAndy Fiddaman 			flags |= CMP_SILENT;
318*b30d1939SAndy Fiddaman 			continue;
319*b30d1939SAndy Fiddaman 		case ':':
320*b30d1939SAndy Fiddaman 			error(2, "%s", opt_info.arg);
321*b30d1939SAndy Fiddaman 			break;
322*b30d1939SAndy Fiddaman 		case '?':
323*b30d1939SAndy Fiddaman 			error(ERROR_usage(2), "%s", opt_info.arg);
324*b30d1939SAndy Fiddaman 			break;
325*b30d1939SAndy Fiddaman 		}
326*b30d1939SAndy Fiddaman 		break;
327*b30d1939SAndy Fiddaman 	}
328*b30d1939SAndy Fiddaman 	argv += opt_info.index;
329*b30d1939SAndy Fiddaman 	if (error_info.errors || !(file1 = *argv++) || !(file2 = *argv++))
330*b30d1939SAndy Fiddaman 		error(ERROR_usage(2), "%s", optusage(NiL));
331*b30d1939SAndy Fiddaman 	n = 2;
332*b30d1939SAndy Fiddaman 	if (streq(file1, "-"))
333*b30d1939SAndy Fiddaman 		f1 = sfstdin;
334*b30d1939SAndy Fiddaman 	else if (!(f1 = sfopen(NiL, file1, "r")))
335*b30d1939SAndy Fiddaman 	{
336*b30d1939SAndy Fiddaman 		if (!(flags & CMP_SILENT))
337*b30d1939SAndy Fiddaman 			error(ERROR_system(0), "%s: cannot open", file1);
338*b30d1939SAndy Fiddaman 		goto done;
339*b30d1939SAndy Fiddaman 	}
340*b30d1939SAndy Fiddaman 	if (streq(file2, "-"))
341*b30d1939SAndy Fiddaman 		f2 = sfstdin;
342*b30d1939SAndy Fiddaman 	else if (!(f2 = sfopen(NiL, file2, "r")))
343*b30d1939SAndy Fiddaman 	{
344*b30d1939SAndy Fiddaman 		if (!(flags & CMP_SILENT))
345*b30d1939SAndy Fiddaman 			error(ERROR_system(0), "%s: cannot open", file2);
346*b30d1939SAndy Fiddaman 		goto done;
347*b30d1939SAndy Fiddaman 	}
348*b30d1939SAndy Fiddaman 	if (s = *argv++)
349*b30d1939SAndy Fiddaman 	{
350*b30d1939SAndy Fiddaman 		o1 = strtoll(s, &e, 0);
351*b30d1939SAndy Fiddaman 		if (*e)
352*b30d1939SAndy Fiddaman 		{
353*b30d1939SAndy Fiddaman 			error(ERROR_exit(0), "%s: %s: invalid skip", file1, s);
354*b30d1939SAndy Fiddaman 			goto done;
355*b30d1939SAndy Fiddaman 		}
356*b30d1939SAndy Fiddaman 		if (s = *argv++)
357*b30d1939SAndy Fiddaman 		{
358*b30d1939SAndy Fiddaman 			o2 = strtoll(s, &e, 0);
359*b30d1939SAndy Fiddaman 			if (*e)
360*b30d1939SAndy Fiddaman 			{
361*b30d1939SAndy Fiddaman 				error(ERROR_exit(0), "%s: %s: invalid skip", file2, s);
362*b30d1939SAndy Fiddaman 				goto done;
363*b30d1939SAndy Fiddaman 			}
364*b30d1939SAndy Fiddaman 		}
365*b30d1939SAndy Fiddaman 		if (*argv)
366*b30d1939SAndy Fiddaman 		{
367*b30d1939SAndy Fiddaman 			error(ERROR_usage(0), "%s", optusage(NiL));
368*b30d1939SAndy Fiddaman 			goto done;
369*b30d1939SAndy Fiddaman 		}
370*b30d1939SAndy Fiddaman 	}
371*b30d1939SAndy Fiddaman 	if (o1 && sfseek(f1, o1, SEEK_SET) != o1)
372*b30d1939SAndy Fiddaman 	{
373*b30d1939SAndy Fiddaman 		if (!(flags & CMP_SILENT))
374*b30d1939SAndy Fiddaman 			error(ERROR_exit(0), "EOF on %s", file1);
375*b30d1939SAndy Fiddaman 		n = 1;
376*b30d1939SAndy Fiddaman 		goto done;
377*b30d1939SAndy Fiddaman 	}
378*b30d1939SAndy Fiddaman 	if (o2 && sfseek(f2, o2, SEEK_SET) != o2)
379*b30d1939SAndy Fiddaman 	{
380*b30d1939SAndy Fiddaman 		if (!(flags & CMP_SILENT))
381*b30d1939SAndy Fiddaman 			error(ERROR_exit(0), "EOF on %s", file2);
382*b30d1939SAndy Fiddaman 		n = 1;
383*b30d1939SAndy Fiddaman 		goto done;
384*b30d1939SAndy Fiddaman 	}
385*b30d1939SAndy Fiddaman 	if (fstat(sffileno(f1), &s1))
386*b30d1939SAndy Fiddaman 		error(ERROR_system(0), "%s: cannot stat", file1);
387*b30d1939SAndy Fiddaman 	else if (fstat(sffileno(f2), &s2))
388*b30d1939SAndy Fiddaman 		error(ERROR_system(0), "%s: cannot stat", file1);
389*b30d1939SAndy Fiddaman 	else if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev && o1 == o2)