xref: /freebsd/usr.bin/cmp/cmp.c (revision 1727cb4cae100feb1b38a21042b75352a30c2b8a)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1987, 1990, 1993, 1994
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
32e03983a3SPoul-Henning Kamp  *
33e03983a3SPoul-Henning Kamp  * $FreeBSD$
34e03983a3SPoul-Henning Kamp  *
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
38fa146c53SArchie Cobbs static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
44fa146c53SArchie Cobbs static const char sccsid[] = "@(#)cmp.c	8.3 (Berkeley) 4/2/94";
459b50d902SRodney W. Grimes #endif /* not lint */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/types.h>
489b50d902SRodney W. Grimes #include <sys/stat.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <err.h>
519b50d902SRodney W. Grimes #include <fcntl.h>
529b50d902SRodney W. Grimes #include <stdio.h>
539b50d902SRodney W. Grimes #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
559b50d902SRodney W. Grimes #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes #include "extern.h"
589b50d902SRodney W. Grimes 
593e6902efSBrian Somers int	lflag, sflag, xflag, zflag;
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes static void usage __P((void));
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes int
649b50d902SRodney W. Grimes main(argc, argv)
659b50d902SRodney W. Grimes 	int argc;
669b50d902SRodney W. Grimes 	char *argv[];
679b50d902SRodney W. Grimes {
689b50d902SRodney W. Grimes 	struct stat sb1, sb2;
699b50d902SRodney W. Grimes 	off_t skip1, skip2;
709b50d902SRodney W. Grimes 	int ch, fd1, fd2, special;
719b50d902SRodney W. Grimes 	char *file1, *file2;
729b50d902SRodney W. Grimes 
733e6902efSBrian Somers 	while ((ch = getopt(argc, argv, "-lsxz")) != -1)
749b50d902SRodney W. Grimes 		switch (ch) {
759b50d902SRodney W. Grimes 		case 'l':		/* print all differences */
769b50d902SRodney W. Grimes 			lflag = 1;
779b50d902SRodney W. Grimes 			break;
789b50d902SRodney W. Grimes 		case 's':		/* silent run */
799b50d902SRodney W. Grimes 			sflag = 1;
803e6902efSBrian Somers 			zflag = 1;
819b50d902SRodney W. Grimes 			break;
82e03983a3SPoul-Henning Kamp 		case 'x':		/* hex output */
83e03983a3SPoul-Henning Kamp 			lflag = 1;
84e03983a3SPoul-Henning Kamp 			xflag = 1;
85e03983a3SPoul-Henning Kamp 			break;
863e6902efSBrian Somers 		case 'z':		/* compare size first */
873e6902efSBrian Somers 			zflag = 1;
883e6902efSBrian Somers 			break;
899b50d902SRodney W. Grimes 		case '-':		/* stdin (must be after options) */
909b50d902SRodney W. Grimes 			--optind;
919b50d902SRodney W. Grimes 			goto endargs;
929b50d902SRodney W. Grimes 		case '?':
939b50d902SRodney W. Grimes 		default:
949b50d902SRodney W. Grimes 			usage();
959b50d902SRodney W. Grimes 		}
969b50d902SRodney W. Grimes endargs:
979b50d902SRodney W. Grimes 	argv += optind;
989b50d902SRodney W. Grimes 	argc -= optind;
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes 	if (lflag && sflag)
1013e6902efSBrian Somers 		errx(ERR_EXIT, "specifying -s with -l or -x is not permitted");
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	if (argc < 2 || argc > 4)
1049b50d902SRodney W. Grimes 		usage();
1059b50d902SRodney W. Grimes 
1069b50d902SRodney W. Grimes 	/* Backward compatibility -- handle "-" meaning stdin. */
1079b50d902SRodney W. Grimes 	special = 0;
1089b50d902SRodney W. Grimes 	if (strcmp(file1 = argv[0], "-") == 0) {
1099b50d902SRodney W. Grimes 		special = 1;
1109b50d902SRodney W. Grimes 		fd1 = 0;
1119b50d902SRodney W. Grimes 		file1 = "stdin";
1129b50d902SRodney W. Grimes 	}
113e5266259SJordan K. Hubbard 	else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
114e5266259SJordan K. Hubbard 		if (!sflag)
1159b50d902SRodney W. Grimes 			err(ERR_EXIT, "%s", file1);
116e5266259SJordan K. Hubbard 		else
117e5266259SJordan K. Hubbard 			exit(1);
118e5266259SJordan K. Hubbard 	}
1199b50d902SRodney W. Grimes 	if (strcmp(file2 = argv[1], "-") == 0) {
1209b50d902SRodney W. Grimes 		if (special)
1219b50d902SRodney W. Grimes 			errx(ERR_EXIT,
1229b50d902SRodney W. Grimes 				"standard input may only be specified once");
1239b50d902SRodney W. Grimes 		special = 1;
1249b50d902SRodney W. Grimes 		fd2 = 0;
1259b50d902SRodney W. Grimes 		file2 = "stdin";
1269b50d902SRodney W. Grimes 	}
127e5266259SJordan K. Hubbard 	else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
128e5266259SJordan K. Hubbard 		if (!sflag)
1299b50d902SRodney W. Grimes 			err(ERR_EXIT, "%s", file2);
130e5266259SJordan K. Hubbard 		else
131e5266259SJordan K. Hubbard 			exit(1);
132e5266259SJordan K. Hubbard 	}
1339b50d902SRodney W. Grimes 
1341e17b945SJonathan Lemon 	skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0;
1351e17b945SJonathan Lemon 	skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0;
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes 	if (!special) {
138e5266259SJordan K. Hubbard 		if (fstat(fd1, &sb1)) {
139e5266259SJordan K. Hubbard 			if (!sflag)
1409b50d902SRodney W. Grimes 				err(ERR_EXIT, "%s", file1);
141e5266259SJordan K. Hubbard 			else
142e5266259SJordan K. Hubbard 				exit(1);
143e5266259SJordan K. Hubbard 		}
1449b50d902SRodney W. Grimes 		if (!S_ISREG(sb1.st_mode))
1459b50d902SRodney W. Grimes 			special = 1;
1469b50d902SRodney W. Grimes 		else {
147e5266259SJordan K. Hubbard 			if (fstat(fd2, &sb2)) {
148e5266259SJordan K. Hubbard 				if (!sflag)
1499b50d902SRodney W. Grimes 					err(ERR_EXIT, "%s", file2);
150e5266259SJordan K. Hubbard 				else
151e5266259SJordan K. Hubbard 					exit(1);
152e5266259SJordan K. Hubbard 			}
1539b50d902SRodney W. Grimes 			if (!S_ISREG(sb2.st_mode))
1549b50d902SRodney W. Grimes 				special = 1;
1559b50d902SRodney W. Grimes 		}
1569b50d902SRodney W. Grimes 	}
1579b50d902SRodney W. Grimes 
1589b50d902SRodney W. Grimes 	if (special)
1599b50d902SRodney W. Grimes 		c_special(fd1, file1, skip1, fd2, file2, skip2);
1601727cb4cSSheldon Hearn 	else {
1613e6902efSBrian Somers 		if (zflag && sb1.st_size != sb2.st_size) {
1623e6902efSBrian Somers 			if (!sflag)
1633e6902efSBrian Somers 				(void) printf("%s %s differ: size\n",
1643e6902efSBrian Somers 				    file1, file2);
1653e6902efSBrian Somers 			exit(DIFF_EXIT);
1663e6902efSBrian Somers 		}
1679b50d902SRodney W. Grimes 		c_regular(fd1, file1, skip1, sb1.st_size,
1689b50d902SRodney W. Grimes 		    fd2, file2, skip2, sb2.st_size);
1691727cb4cSSheldon Hearn 	}
1709b50d902SRodney W. Grimes 	exit(0);
1719b50d902SRodney W. Grimes }
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes static void
1749b50d902SRodney W. Grimes usage()
1759b50d902SRodney W. Grimes {
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes 	(void)fprintf(stderr,
1783e6902efSBrian Somers 	    "usage: cmp [-l | -s | -x] [-z] file1 file2 [skip1 [skip2]]\n");
1799b50d902SRodney W. Grimes 	exit(ERR_EXIT);
1809b50d902SRodney W. Grimes }
181