xref: /freebsd-11-stable/usr.bin/cmp/cmp.c (revision a69447bab852853af0c486e1180e61fa621ee06d)
1 /*
2  * Copyright (c) 1987, 1990, 1993, 1994
3  *	The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #endif
35 
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)cmp.c	8.3 (Berkeley) 4/2/94";
39 #endif
40 #endif
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <getopt.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "extern.h"
58 
59 int	lflag, sflag, xflag, zflag;
60 
61 static const struct option long_opts[] =
62 {
63 	{"verbose",	no_argument,		NULL, 'l'},
64 	{"silent",	no_argument,		NULL, 's'},
65 	{"quiet",	no_argument,		NULL, 's'},
66 	{NULL,		no_argument,		NULL, 0}
67 };
68 
69 static void usage(void);
70 
71 int
main(int argc,char * argv[])72 main(int argc, char *argv[])
73 {
74 	struct stat sb1, sb2;
75 	off_t skip1, skip2;
76 	int ch, fd1, fd2, oflag, special;
77 	const char *file1, *file2;
78 
79 	oflag = O_RDONLY;
80 	while ((ch = getopt_long(argc, argv, "+hlsxz", long_opts, NULL)) != -1)
81 		switch (ch) {
82 		case 'h':		/* Don't follow symlinks */
83 			oflag |= O_NOFOLLOW;
84 			break;
85 		case 'l':		/* print all differences */
86 			lflag = 1;
87 			break;
88 		case 's':		/* silent run */
89 			sflag = 1;
90 			zflag = 1;
91 			break;
92 		case 'x':		/* hex output */
93 			lflag = 1;
94 			xflag = 1;
95 			break;
96 		case 'z':		/* compare size first */
97 			zflag = 1;
98 			break;
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	argv += optind;
104 	argc -= optind;
105 
106 	if (lflag && sflag)
107 		errx(ERR_EXIT, "specifying -s with -l or -x is not permitted");
108 
109 	if (argc < 2 || argc > 4)
110 		usage();
111 
112 	/* Backward compatibility -- handle "-" meaning stdin. */
113 	special = 0;
114 	if (strcmp(file1 = argv[0], "-") == 0) {
115 		special = 1;
116 		fd1 = 0;
117 		file1 = "stdin";
118 	}
119 	else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) {
120 		if (!sflag)
121 			err(ERR_EXIT, "%s", file1);
122 		else
123 			exit(ERR_EXIT);
124 	}
125 	if (strcmp(file2 = argv[1], "-") == 0) {
126 		if (special)
127 			errx(ERR_EXIT,
128 				"standard input may only be specified once");
129 		special = 1;
130 		fd2 = 0;
131 		file2 = "stdin";
132 	}
133 	else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) {
134 		if (!sflag)
135 			err(ERR_EXIT, "%s", file2);
136 		else
137 			exit(ERR_EXIT);
138 	}
139 
140 	skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0;
141 	skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0;
142 
143 	if (fd1 == -1) {
144 		if (fd2 == -1) {
145 			c_link(file1, skip1, file2, skip2);
146 			exit(0);
147 		} else if (!sflag)
148 			errx(ERR_EXIT, "%s: Not a symbolic link", file2);
149 		else
150 			exit(ERR_EXIT);
151 	} else if (fd2 == -1) {
152 		if (!sflag)
153 			errx(ERR_EXIT, "%s: Not a symbolic link", file1);
154 		else
155 			exit(ERR_EXIT);
156 	}
157 
158 	if (!special) {
159 		if (fstat(fd1, &sb1)) {
160 			if (!sflag)
161 				err(ERR_EXIT, "%s", file1);
162 			else
163 				exit(ERR_EXIT);
164 		}
165 		if (!S_ISREG(sb1.st_mode))
166 			special = 1;
167 		else {
168 			if (fstat(fd2, &sb2)) {
169 				if (!sflag)
170 					err(ERR_EXIT, "%s", file2);
171 				else
172 					exit(ERR_EXIT);
173 			}
174 			if (!S_ISREG(sb2.st_mode))
175 				special = 1;
176 		}
177 	}
178 
179 	if (special)
180 		c_special(fd1, file1, skip1, fd2, file2, skip2);
181 	else {
182 		if (zflag && sb1.st_size != sb2.st_size) {
183 			if (!sflag)
184 				(void) printf("%s %s differ: size\n",
185 				    file1, file2);
186 			exit(DIFF_EXIT);
187 		}
188 		c_regular(fd1, file1, skip1, sb1.st_size,
189 		    fd2, file2, skip2, sb2.st_size);
190 	}
191 	exit(0);
192 }
193 
194 static void
usage(void)195 usage(void)
196 {
197 
198 	(void)fprintf(stderr,
199 	    "usage: cmp [-l | -s | -x] [-hz] file1 file2 [skip1 [skip2]]\n");
200 	exit(ERR_EXIT);
201 }
202