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