1 /* 2 * Copyright (c) 1987 Regents of the University of California. 3 * All rights reserved. 4 * 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. 32 */ 33 34 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1987, 1990 Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)cmp.c 5.3 (Berkeley) 6/1/90"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <stdio.h> 48 #include <ctype.h> 49 #include <errno.h> 50 51 #define EXITNODIFF 0 52 #define EXITDIFF 1 53 #define EXITERR 2 54 55 int all, fd1, fd2, silent; 56 u_char buf1[MAXBSIZE], buf2[MAXBSIZE]; 57 char *file1, *file2; 58 59 main(argc, argv) 60 int argc; 61 char *argv[]; 62 { 63 extern char *optarg; 64 extern int optind; 65 int ch; 66 u_long otoi(); 67 68 while ((ch = getopt(argc, argv, "-ls")) != EOF) 69 switch (ch) { 70 case 'l': /* print all differences */ 71 all = 1; 72 break; 73 case 's': /* silent run */ 74 silent = 1; 75 break; 76 case '-': /* must be after any flags */ 77 --optind; 78 goto endargs; 79 case '?': 80 default: 81 usage(); 82 } 83 endargs: 84 argv += optind; 85 argc -= optind; 86 87 if (argc < 2 || argc > 4) 88 usage(); 89 90 if (all && silent) { 91 fprintf(stderr, 92 "cmp: only one of -l and -s may be specified.\n"); 93 exit(EXITERR); 94 } 95 if (strcmp(file1 = argv[0], "-") == 0) 96 fd1 = 0; 97 else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) 98 error(file1); 99 if (strcmp(file2 = argv[1], "-") == 0) 100 fd2 = 0; 101 else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) 102 error(file2); 103 if (fd1 == fd2) { 104 fprintf(stderr, 105 "cmp: standard input may only be specified once.\n"); 106 exit(EXITERR); 107 } 108 109 /* handle skip arguments */ 110 if (argc > 2) { 111 skip(otoi(argv[2]), fd1, file1); 112 if (argc == 4) 113 skip(otoi(argv[3]), fd2, file2); 114 } 115 cmp(); 116 /*NOTREACHED*/ 117 } 118 119 /* 120 * skip -- 121 * skip first part of file 122 */ 123 skip(dist, fd, fname) 124 register u_long dist; 125 register int fd; 126 char *fname; 127 { 128 register int rlen, nread; 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); 137 } 138 } 139 } 140 141 cmp() 142 { 143 register u_char *p1, *p2; 144 register int cnt, len1, len2; 145 register long byte, line; 146 int dfound = 0; 147 148 for (byte = 0, line = 1; ; ) { 149 switch (len1 = read(fd1, buf1, MAXBSIZE)) { 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 */ 157 switch (read(fd2, buf2, 1)) { 158 case -1: 159 error(file2); 160 /* NOTREACHED */ 161 case 0: 162 exit(dfound ? EXITDIFF : EXITNODIFF); 163 /* NOTREACHED */ 164 default: 165 endoffile(file1); 166 break; 167 } 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 */ 174 if ((len2 = read(fd2, buf2, len1)) == -1) 175 error(file2); 176 if (bcmp(buf1, buf2, len2)) { 177 if (silent) 178 exit(EXITDIFF); 179 if (all) { 180 dfound = 1; 181 for (p1 = buf1, p2 = buf2, cnt = len2; cnt--; 182 ++p1, ++p2) { 183 ++byte; 184 if (*p1 != *p2) 185 printf("%6ld %3o %3o\n", 186 byte, *p1, *p2); 187 } 188 } else for (p1 = buf1, p2 = buf2; ; ++p1, ++p2) { 189 ++byte; 190 if (*p1 != *p2) { 191 printf("%s %s differ: char %ld, line %ld\n", file1, file2, byte, line); 192 exit(EXITDIFF); 193 } 194 if (*p1 == '\n') 195 ++line; 196 } 197 } else { 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 203 * *want* to know the line number, run -s or -l. 204 */ 205 if (!silent && !all) 206 for (p1 = buf1, cnt = len2; cnt--; ) 207 if (*p1++ == '\n') 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 */ 215 if (len2 < len1) 216 endoffile(file2); 217 } 218 } 219 220 /* 221 * otoi -- 222 * octal/decimal string to u_long 223 */ 224 u_long 225 otoi(s) 226 register char *s; 227 { 228 register u_long val; 229 register int base; 230 231 base = (*s == '0') ? 8 : 10; 232 for (val = 0; isdigit(*s); ++s) 233 val = val * base + *s - '0'; 234 return (val); 235 } 236 237 /* 238 * error -- 239 * print I/O error message and die 240 */ 241 error(filename) 242 char *filename; 243 { 244 extern int errno; 245 char *strerror(); 246 247 if (!silent) 248 (void) fprintf(stderr, "cmp: %s: %s\n", 249 filename, strerror(errno)); 250 exit(EXITERR); 251 } 252 253 /* 254 * endoffile -- 255 * print end-of-file message and exit indicating the files were different 256 */ 257 endoffile(filename) 258 char *filename; 259 { 260 /* 32V put this message on stdout, S5 does it on stderr. */ 261 /* POSIX.2 currently does it on stdout-- Hooray! */ 262 if (!silent) 263 (void) printf("cmp: EOF on %s\n", filename); 264 exit(EXITDIFF); 265 } 266 267 /* 268 * usage -- 269 * print usage and die 270 */ 271 usage() 272 { 273 fputs("usage: cmp [-ls] file1 file2 [skip1] [skip2]\n", stderr); 274 exit(EXITERR); 275 } 276