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