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