cmp.c revision 1.6 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1987 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1987, 1990 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.2 mycroft /*static char sccsid[] = "from: @(#)cmp.c 5.3 (Berkeley) 6/1/90";*/
42 1.6 mycroft static char rcsid[] = "$Id: cmp.c,v 1.6 1994/03/25 17:07:02 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/file.h>
47 1.1 cgd #include <sys/stat.h>
48 1.1 cgd #include <stdio.h>
49 1.4 jtc #include <stdlib.h>
50 1.4 jtc #include <string.h>
51 1.1 cgd #include <ctype.h>
52 1.1 cgd #include <errno.h>
53 1.4 jtc #include <locale.h>
54 1.4 jtc #include <unistd.h>
55 1.1 cgd
56 1.1 cgd #define EXITNODIFF 0
57 1.1 cgd #define EXITDIFF 1
58 1.1 cgd #define EXITERR 2
59 1.1 cgd
60 1.4 jtc void skip __P(());
61 1.4 jtc __dead void cmp __P(());
62 1.4 jtc __dead void error __P(());
63 1.4 jtc __dead void endoffile __P(());
64 1.4 jtc __dead void usage __P(());
65 1.4 jtc
66 1.1 cgd int all, fd1, fd2, silent;
67 1.5 mycroft u_char buffer1[MAXBSIZE], buffer2[MAXBSIZE];
68 1.1 cgd char *file1, *file2;
69 1.1 cgd
70 1.4 jtc int
71 1.1 cgd main(argc, argv)
72 1.1 cgd int argc;
73 1.3 jtc char **argv;
74 1.1 cgd {
75 1.1 cgd int ch;
76 1.4 jtc
77 1.4 jtc setlocale(LC_ALL, "");
78 1.1 cgd
79 1.3 jtc while ((ch = getopt(argc, argv, "ls")) != -1)
80 1.1 cgd switch (ch) {
81 1.1 cgd case 'l': /* print all differences */
82 1.1 cgd all = 1;
83 1.1 cgd break;
84 1.1 cgd case 's': /* silent run */
85 1.1 cgd silent = 1;
86 1.1 cgd break;
87 1.1 cgd case '?':
88 1.1 cgd default:
89 1.1 cgd usage();
90 1.1 cgd }
91 1.1 cgd argv += optind;
92 1.1 cgd argc -= optind;
93 1.1 cgd
94 1.1 cgd if (argc < 2 || argc > 4)
95 1.1 cgd usage();
96 1.1 cgd
97 1.3 jtc if (all && silent)
98 1.3 jtc usage ();
99 1.3 jtc
100 1.1 cgd if (strcmp(file1 = argv[0], "-") == 0)
101 1.1 cgd fd1 = 0;
102 1.1 cgd else if ((fd1 = open(file1, O_RDONLY, 0)) < 0)
103 1.1 cgd error(file1);
104 1.1 cgd if (strcmp(file2 = argv[1], "-") == 0)
105 1.1 cgd fd2 = 0;
106 1.1 cgd else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
107 1.1 cgd error(file2);
108 1.1 cgd if (fd1 == fd2) {
109 1.1 cgd fprintf(stderr,
110 1.1 cgd "cmp: standard input may only be specified once.\n");
111 1.1 cgd exit(EXITERR);
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd /* handle skip arguments */
115 1.1 cgd if (argc > 2) {
116 1.4 jtc skip(strtoul(argv[2], NULL, 0), fd1, file1);
117 1.1 cgd if (argc == 4)
118 1.4 jtc skip(strtoul(argv[3], NULL, 0), fd2, file2);
119 1.1 cgd }
120 1.1 cgd cmp();
121 1.1 cgd /*NOTREACHED*/
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd /*
125 1.1 cgd * skip --
126 1.1 cgd * skip first part of file
127 1.1 cgd */
128 1.4 jtc void
129 1.1 cgd skip(dist, fd, fname)
130 1.1 cgd register u_long dist;
131 1.1 cgd register int fd;
132 1.1 cgd char *fname;
133 1.1 cgd {
134 1.1 cgd register int rlen, nread;
135 1.1 cgd
136 1.1 cgd for (; dist; dist -= rlen) {
137 1.5 mycroft rlen = MIN(dist, sizeof(buffer1));
138 1.5 mycroft if ((nread = read(fd, buffer1, rlen)) != rlen) {
139 1.1 cgd if (nread < 0)
140 1.1 cgd error(fname);
141 1.1 cgd else
142 1.1 cgd endoffile(fname);
143 1.1 cgd }
144 1.1 cgd }
145 1.1 cgd }
146 1.1 cgd
147 1.4 jtc void
148 1.1 cgd cmp()
149 1.1 cgd {
150 1.1 cgd register u_char *p1, *p2;
151 1.5 mycroft u_char *buf1, *buf2;
152 1.5 mycroft register int cnt;
153 1.5 mycroft int len = 0, len1 = 0, len2 = 0;
154 1.1 cgd register long byte, line;
155 1.1 cgd int dfound = 0;
156 1.1 cgd
157 1.1 cgd for (byte = 0, line = 1; ; ) {
158 1.5 mycroft len1 -= len;
159 1.5 mycroft len2 -= len;
160 1.5 mycroft if (len1)
161 1.6 mycroft buf1 += len;
162 1.5 mycroft else
163 1.5 mycroft switch (len1 = read(fd1, buf1 = buffer1, MAXBSIZE)) {
164 1.5 mycroft case -1:
165 1.5 mycroft error(file1);
166 1.5 mycroft case 0:
167 1.5 mycroft if (len2)
168 1.5 mycroft endoffile(file1);
169 1.5 mycroft /*
170 1.5 mycroft * read of file 1 just failed, find out
171 1.5 mycroft * if there's anything left in file 2
172 1.5 mycroft */
173 1.5 mycroft switch (read(fd2, buf2 = buffer2, 1)) {
174 1.1 cgd case -1:
175 1.1 cgd error(file2);
176 1.1 cgd /* NOTREACHED */
177 1.1 cgd case 0:
178 1.1 cgd exit(dfound ? EXITDIFF : EXITNODIFF);
179 1.1 cgd /* NOTREACHED */
180 1.1 cgd default:
181 1.1 cgd endoffile(file1);
182 1.1 cgd break;
183 1.5 mycroft }
184 1.5 mycroft }
185 1.5 mycroft if (len2)
186 1.6 mycroft buf2 += len;
187 1.5 mycroft else
188 1.5 mycroft switch (len2 = read(fd2, buf2 = buffer2, MAXBSIZE)) {
189 1.5 mycroft case -1:
190 1.5 mycroft error(file2);
191 1.5 mycroft case 0:
192 1.5 mycroft /*
193 1.5 mycroft * read of file 2 just failed; we know there is
194 1.5 mycroft * data left in file 1 if we got this far
195 1.5 mycroft */
196 1.5 mycroft endoffile(file2);
197 1.5 mycroft break;
198 1.1 cgd }
199 1.1 cgd /*
200 1.5 mycroft * Either file might be stdio. We compare only the minimum
201 1.5 mycroft * number of bytes we know are common, then loop back to the
202 1.5 mycroft * top. This avoids blocking on input if a difference is
203 1.5 mycroft * found early.
204 1.1 cgd */
205 1.5 mycroft if (len1 < len2)
206 1.5 mycroft len = len1;
207 1.5 mycroft else
208 1.5 mycroft len = len2;
209 1.5 mycroft if (memcmp(buf1, buf2, len)) {
210 1.1 cgd if (silent)
211 1.1 cgd exit(EXITDIFF);
212 1.1 cgd if (all) {
213 1.1 cgd dfound = 1;
214 1.5 mycroft for (p1 = buf1, p2 = buf2, cnt = len; cnt--;
215 1.1 cgd ++p1, ++p2) {
216 1.1 cgd ++byte;
217 1.1 cgd if (*p1 != *p2)
218 1.1 cgd printf("%6ld %3o %3o\n",
219 1.1 cgd byte, *p1, *p2);
220 1.1 cgd }
221 1.1 cgd } else for (p1 = buf1, p2 = buf2; ; ++p1, ++p2) {
222 1.1 cgd ++byte;
223 1.1 cgd if (*p1 != *p2) {
224 1.1 cgd printf("%s %s differ: char %ld, line %ld\n", file1, file2, byte, line);
225 1.1 cgd exit(EXITDIFF);
226 1.1 cgd }
227 1.1 cgd if (*p1 == '\n')
228 1.1 cgd ++line;
229 1.1 cgd }
230 1.1 cgd } else {
231 1.5 mycroft byte += len;
232 1.1 cgd /*
233 1.1 cgd * here's the real performance problem, we've got to
234 1.1 cgd * count the stupid lines, which means that -l is a
235 1.1 cgd * *much* faster version, i.e., unless you really
236 1.1 cgd * *want* to know the line number, run -s or -l.
237 1.1 cgd */
238 1.1 cgd if (!silent && !all)
239 1.5 mycroft for (p1 = buf1, cnt = len; cnt--; )
240 1.1 cgd if (*p1++ == '\n')
241 1.1 cgd ++line;
242 1.1 cgd }
243 1.1 cgd }
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /*
247 1.1 cgd * error --
248 1.1 cgd * print I/O error message and die
249 1.1 cgd */
250 1.4 jtc void
251 1.1 cgd error(filename)
252 1.1 cgd char *filename;
253 1.1 cgd {
254 1.1 cgd extern int errno;
255 1.1 cgd char *strerror();
256 1.1 cgd
257 1.1 cgd if (!silent)
258 1.1 cgd (void) fprintf(stderr, "cmp: %s: %s\n",
259 1.1 cgd filename, strerror(errno));
260 1.1 cgd exit(EXITERR);
261 1.1 cgd }
262 1.1 cgd
263 1.1 cgd /*
264 1.1 cgd * endoffile --
265 1.1 cgd * print end-of-file message and exit indicating the files were different
266 1.1 cgd */
267 1.4 jtc void
268 1.1 cgd endoffile(filename)
269 1.1 cgd char *filename;
270 1.1 cgd {
271 1.1 cgd if (!silent)
272 1.3 jtc (void) fprintf(stderr, "cmp: EOF on %s\n", filename);
273 1.1 cgd exit(EXITDIFF);
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd /*
277 1.1 cgd * usage --
278 1.1 cgd * print usage and die
279 1.1 cgd */
280 1.4 jtc void
281 1.1 cgd usage()
282 1.1 cgd {
283 1.3 jtc fputs("usage: cmp [-l | -s] file1 file2 [skip1] [skip2]\n", stderr);
284 1.1 cgd exit(EXITERR);
285 1.1 cgd }
286