cmp.c revision 1.2 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.2 mycroft static char rcsid[] = "$Id: cmp.c,v 1.2 1993/08/01 18:17:37 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.1 cgd #include <ctype.h>
50 1.1 cgd #include <errno.h>
51 1.1 cgd
52 1.1 cgd #define EXITNODIFF 0
53 1.1 cgd #define EXITDIFF 1
54 1.1 cgd #define EXITERR 2
55 1.1 cgd
56 1.1 cgd int all, fd1, fd2, silent;
57 1.1 cgd u_char buf1[MAXBSIZE], buf2[MAXBSIZE];
58 1.1 cgd char *file1, *file2;
59 1.1 cgd
60 1.1 cgd main(argc, argv)
61 1.1 cgd int argc;
62 1.1 cgd char *argv[];
63 1.1 cgd {
64 1.1 cgd extern char *optarg;
65 1.1 cgd extern int optind;
66 1.1 cgd int ch;
67 1.1 cgd u_long otoi();
68 1.1 cgd
69 1.1 cgd while ((ch = getopt(argc, argv, "-ls")) != EOF)
70 1.1 cgd switch (ch) {
71 1.1 cgd case 'l': /* print all differences */
72 1.1 cgd all = 1;
73 1.1 cgd break;
74 1.1 cgd case 's': /* silent run */
75 1.1 cgd silent = 1;
76 1.1 cgd break;
77 1.1 cgd case '-': /* must be after any flags */
78 1.1 cgd --optind;
79 1.1 cgd goto endargs;
80 1.1 cgd case '?':
81 1.1 cgd default:
82 1.1 cgd usage();
83 1.1 cgd }
84 1.1 cgd endargs:
85 1.1 cgd argv += optind;
86 1.1 cgd argc -= optind;
87 1.1 cgd
88 1.1 cgd if (argc < 2 || argc > 4)
89 1.1 cgd usage();
90 1.1 cgd
91 1.1 cgd if (all && silent) {
92 1.1 cgd fprintf(stderr,
93 1.1 cgd "cmp: only one of -l and -s may be specified.\n");
94 1.1 cgd exit(EXITERR);
95 1.1 cgd }
96 1.1 cgd if (strcmp(file1 = argv[0], "-") == 0)
97 1.1 cgd fd1 = 0;
98 1.1 cgd else if ((fd1 = open(file1, O_RDONLY, 0)) < 0)
99 1.1 cgd error(file1);
100 1.1 cgd if (strcmp(file2 = argv[1], "-") == 0)
101 1.1 cgd fd2 = 0;
102 1.1 cgd else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
103 1.1 cgd error(file2);
104 1.1 cgd if (fd1 == fd2) {
105 1.1 cgd fprintf(stderr,
106 1.1 cgd "cmp: standard input may only be specified once.\n");
107 1.1 cgd exit(EXITERR);
108 1.1 cgd }
109 1.1 cgd
110 1.1 cgd /* handle skip arguments */
111 1.1 cgd if (argc > 2) {
112 1.1 cgd skip(otoi(argv[2]), fd1, file1);
113 1.1 cgd if (argc == 4)
114 1.1 cgd skip(otoi(argv[3]), fd2, file2);
115 1.1 cgd }
116 1.1 cgd cmp();
117 1.1 cgd /*NOTREACHED*/
118 1.1 cgd }
119 1.1 cgd
120 1.1 cgd /*
121 1.1 cgd * skip --
122 1.1 cgd * skip first part of file
123 1.1 cgd */
124 1.1 cgd skip(dist, fd, fname)
125 1.1 cgd register u_long dist;
126 1.1 cgd register int fd;
127 1.1 cgd char *fname;
128 1.1 cgd {
129 1.1 cgd register int rlen, nread;
130 1.1 cgd
131 1.1 cgd for (; dist; dist -= rlen) {
132 1.1 cgd rlen = MIN(dist, sizeof(buf1));
133 1.1 cgd if ((nread = read(fd, buf1, rlen)) != rlen) {
134 1.1 cgd if (nread < 0)
135 1.1 cgd error(fname);
136 1.1 cgd else
137 1.1 cgd endoffile(fname);
138 1.1 cgd }
139 1.1 cgd }
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd cmp()
143 1.1 cgd {
144 1.1 cgd register u_char *p1, *p2;
145 1.1 cgd register int cnt, len1, len2;
146 1.1 cgd register long byte, line;
147 1.1 cgd int dfound = 0;
148 1.1 cgd
149 1.1 cgd for (byte = 0, line = 1; ; ) {
150 1.1 cgd switch (len1 = read(fd1, buf1, MAXBSIZE)) {
151 1.1 cgd case -1:
152 1.1 cgd error(file1);
153 1.1 cgd case 0:
154 1.1 cgd /*
155 1.1 cgd * read of file 1 just failed, find out
156 1.1 cgd * if there's anything left in file 2
157 1.1 cgd */
158 1.1 cgd switch (read(fd2, buf2, 1)) {
159 1.1 cgd case -1:
160 1.1 cgd error(file2);
161 1.1 cgd /* NOTREACHED */
162 1.1 cgd case 0:
163 1.1 cgd exit(dfound ? EXITDIFF : EXITNODIFF);
164 1.1 cgd /* NOTREACHED */
165 1.1 cgd default:
166 1.1 cgd endoffile(file1);
167 1.1 cgd break;
168 1.1 cgd }
169 1.1 cgd }
170 1.1 cgd /*
171 1.1 cgd * file1 might be stdio, which means that a read of less than
172 1.1 cgd * MAXBSIZE might not mean an EOF. So, read whatever we read
173 1.1 cgd * from file1 from file2.
174 1.1 cgd */
175 1.1 cgd if ((len2 = read(fd2, buf2, len1)) == -1)
176 1.1 cgd error(file2);
177 1.1 cgd if (bcmp(buf1, buf2, len2)) {
178 1.1 cgd if (silent)
179 1.1 cgd exit(EXITDIFF);
180 1.1 cgd if (all) {
181 1.1 cgd dfound = 1;
182 1.1 cgd for (p1 = buf1, p2 = buf2, cnt = len2; cnt--;
183 1.1 cgd ++p1, ++p2) {
184 1.1 cgd ++byte;
185 1.1 cgd if (*p1 != *p2)
186 1.1 cgd printf("%6ld %3o %3o\n",
187 1.1 cgd byte, *p1, *p2);
188 1.1 cgd }
189 1.1 cgd } else for (p1 = buf1, p2 = buf2; ; ++p1, ++p2) {
190 1.1 cgd ++byte;
191 1.1 cgd if (*p1 != *p2) {
192 1.1 cgd printf("%s %s differ: char %ld, line %ld\n", file1, file2, byte, line);
193 1.1 cgd exit(EXITDIFF);
194 1.1 cgd }
195 1.1 cgd if (*p1 == '\n')
196 1.1 cgd ++line;
197 1.1 cgd }
198 1.1 cgd } else {
199 1.1 cgd byte += len2;
200 1.1 cgd /*
201 1.1 cgd * here's the real performance problem, we've got to
202 1.1 cgd * count the stupid lines, which means that -l is a
203 1.1 cgd * *much* faster version, i.e., unless you really
204 1.1 cgd * *want* to know the line number, run -s or -l.
205 1.1 cgd */
206 1.1 cgd if (!silent && !all)
207 1.1 cgd for (p1 = buf1, cnt = len2; cnt--; )
208 1.1 cgd if (*p1++ == '\n')
209 1.1 cgd ++line;
210 1.1 cgd }
211 1.1 cgd /*
212 1.1 cgd * couldn't read as much from file2 as from file1; checked
213 1.1 cgd * here because there might be a difference before we got
214 1.1 cgd * to this point, which would have precedence.
215 1.1 cgd */
216 1.1 cgd if (len2 < len1)
217 1.1 cgd endoffile(file2);
218 1.1 cgd }
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd /*
222 1.1 cgd * otoi --
223 1.1 cgd * octal/decimal string to u_long
224 1.1 cgd */
225 1.1 cgd u_long
226 1.1 cgd otoi(s)
227 1.1 cgd register char *s;
228 1.1 cgd {
229 1.1 cgd register u_long val;
230 1.1 cgd register int base;
231 1.1 cgd
232 1.1 cgd base = (*s == '0') ? 8 : 10;
233 1.1 cgd for (val = 0; isdigit(*s); ++s)
234 1.1 cgd val = val * base + *s - '0';
235 1.1 cgd return (val);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * error --
240 1.1 cgd * print I/O error message and die
241 1.1 cgd */
242 1.1 cgd error(filename)
243 1.1 cgd char *filename;
244 1.1 cgd {
245 1.1 cgd extern int errno;
246 1.1 cgd char *strerror();
247 1.1 cgd
248 1.1 cgd if (!silent)
249 1.1 cgd (void) fprintf(stderr, "cmp: %s: %s\n",
250 1.1 cgd filename, strerror(errno));
251 1.1 cgd exit(EXITERR);
252 1.1 cgd }
253 1.1 cgd
254 1.1 cgd /*
255 1.1 cgd * endoffile --
256 1.1 cgd * print end-of-file message and exit indicating the files were different
257 1.1 cgd */
258 1.1 cgd endoffile(filename)
259 1.1 cgd char *filename;
260 1.1 cgd {
261 1.1 cgd /* 32V put this message on stdout, S5 does it on stderr. */
262 1.1 cgd /* POSIX.2 currently does it on stdout-- Hooray! */
263 1.1 cgd if (!silent)
264 1.1 cgd (void) printf("cmp: EOF on %s\n", filename);
265 1.1 cgd exit(EXITDIFF);
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * usage --
270 1.1 cgd * print usage and die
271 1.1 cgd */
272 1.1 cgd usage()
273 1.1 cgd {
274 1.1 cgd fputs("usage: cmp [-ls] file1 file2 [skip1] [skip2]\n", stderr);
275 1.1 cgd exit(EXITERR);
276 1.1 cgd }
277