xref: /openbsd-src/usr.bin/cmp/cmp.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*      $OpenBSD: cmp.c,v 1.8 1999/08/03 16:02:44 mickey Exp $      */
2 /*      $NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $      */
3 
4 /*
5  * Copyright (c) 1987, 1990, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)cmp.c	8.3 (Berkeley) 4/2/94";
46 #else
47 static char rcsid[] = "$OpenBSD: cmp.c,v 1.8 1999/08/03 16:02:44 mickey Exp $";
48 #endif
49 #endif /* not lint */
50 
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 
54 #include <err.h>
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <locale.h>
61 
62 #include "extern.h"
63 
64 int	lflag, sflag;
65 
66 static void usage __P((void));
67 
68 int
69 main(argc, argv)
70 	int argc;
71 	char *argv[];
72 {
73 	struct stat sb1, sb2;
74 	off_t skip1, skip2;
75 	int ch, fd1, fd2, special;
76 	char *file1, *file2;
77 
78 	setlocale(LC_ALL, "");
79 
80 	while ((ch = getopt(argc, argv, "ls")) != -1)
81 		switch (ch) {
82 		case 'l':		/* print all differences */
83 			lflag = 1;
84 			break;
85 		case 's':		/* silent run */
86 			sflag = 1;
87 			break;
88 		case '?':
89 		default:
90 			usage();
91 		}
92 
93 	argv += optind;
94 	argc -= optind;
95 
96 	if (lflag && sflag)
97 		errx(ERR_EXIT, "only one of -l and -s may be specified");
98 
99 	if (argc < 2 || argc > 4)
100 		usage();
101 
102 	/* Backward compatibility -- handle "-" meaning stdin. */
103 	special = 0;
104 	if (strcmp(file1 = argv[0], "-") == 0) {
105 		special = 1;
106 		fd1 = 0;
107 		file1 = "stdin";
108 	}
109 	else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
110 		if (sflag)
111 			exit(ERR_EXIT);
112 		else
113 			err(ERR_EXIT, "%s", file1);
114 	}
115 	if (strcmp(file2 = argv[1], "-") == 0) {
116 		if (special) {
117 			if (sflag)
118 				exit(ERR_EXIT);
119 			else
120 				errx(ERR_EXIT,
121 					"standard input may only be specified once");
122 		}
123 		special = 1;
124 		fd2 = 0;
125 		file2 = "stdin";
126 	}
127 	else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
128 		if (sflag)
129 			exit(ERR_EXIT);
130 		else
131 			err(ERR_EXIT, "%s", file2);
132 	}
133 
134 	skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
135 	skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
136 
137 	if (!special) {
138 		if (fstat(fd1, &sb1)) {
139 			if (sflag)
140 				exit(ERR_EXIT);
141 			else
142 				err(ERR_EXIT, "%s", file1);
143 		}
144 		if (!S_ISREG(sb1.st_mode))
145 			special = 1;
146 		else {
147 			if (fstat(fd2, &sb2)) {
148 				if (sflag)
149 					exit(ERR_EXIT);
150 				else
151 					err(ERR_EXIT, "%s", file2);
152 			}
153 			if (!S_ISREG(sb2.st_mode))
154 				special = 1;
155 		}
156 	}
157 
158 	if (special)
159 		c_special(fd1, file1, skip1, fd2, file2, skip2);
160 	else
161 		c_regular(fd1, file1, skip1, sb1.st_size,
162 		    fd2, file2, skip2, sb2.st_size);
163 	return 0;
164 }
165 
166 static void
167 usage()
168 {
169 
170 	(void)fprintf(stderr,
171 	    "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
172 	exit(ERR_EXIT);
173 }
174