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