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