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