1 /* $OpenBSD: cmp.c,v 1.11 2003/06/10 22:20:45 deraadt 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n");
36 __SCCSID("@(#)cmp.c 8.3 (Berkeley) 4/2/94");
37 __RCSID("$MirOS: src/usr.bin/cmp/cmp.c,v 1.2 2007/07/05 23:09:38 tg Exp $");
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <locale.h>
49
50 #include "extern.h"
51
52 int lflag, sflag;
53
54 static void usage(void);
55
56 int
main(int argc,char * argv[])57 main(int argc, char *argv[])
58 {
59 struct stat sb1, sb2;
60 off_t skip1, skip2;
61 int ch, fd1, fd2, special;
62 char *file1, *file2;
63
64 #ifndef __MirBSD__
65 setlocale(LC_ALL, "");
66 #endif
67
68 while ((ch = getopt(argc, argv, "ls")) != -1)
69 switch (ch) {
70 case 'l': /* print all differences */
71 lflag = 1;
72 break;
73 case 's': /* silent run */
74 sflag = 1;
75 break;
76 case '?':
77 default:
78 usage();
79 }
80
81 argv += optind;
82 argc -= optind;
83
84 if (lflag && sflag)
85 errx(ERR_EXIT, "only one of -l and -s may be specified");
86
87 if (argc < 2 || argc > 4)
88 usage();
89
90 /* Backward compatibility -- handle "-" meaning stdin. */
91 special = 0;
92 if (strcmp(file1 = argv[0], "-") == 0) {
93 special = 1;
94 fd1 = 0;
95 file1 = "stdin";
96 } else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
97 if (sflag)
98 exit(ERR_EXIT);
99 else
100 err(ERR_EXIT, "%s", file1);
101 }
102 if (strcmp(file2 = argv[1], "-") == 0) {
103 if (special) {
104 if (sflag)
105 exit(ERR_EXIT);
106 else
107 errx(ERR_EXIT,
108 "standard input may only be specified once");
109 }
110 special = 1;
111 fd2 = 0;
112 file2 = "stdin";
113 } else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
114 if (sflag)
115 exit(ERR_EXIT);
116 else
117 err(ERR_EXIT, "%s", file2);
118 }
119
120 skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
121 skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
122
123 if (!special) {
124 if (fstat(fd1, &sb1)) {
125 if (sflag)
126 exit(ERR_EXIT);
127 else
128 err(ERR_EXIT, "%s", file1);
129 }
130 if (!S_ISREG(sb1.st_mode))
131 special = 1;
132 else {
133 if (fstat(fd2, &sb2)) {
134 if (sflag)
135 exit(ERR_EXIT);
136 else
137 err(ERR_EXIT, "%s", file2);
138 }
139 if (!S_ISREG(sb2.st_mode))
140 special = 1;
141 }
142 }
143
144 if (special)
145 c_special(fd1, file1, skip1, fd2, file2, skip2);
146 else
147 c_regular(fd1, file1, skip1, sb1.st_size,
148 fd2, file2, skip2, sb2.st_size);
149 return 0;
150 }
151
152 static void
usage(void)153 usage(void)
154 {
155
156 (void)fprintf(stderr,
157 "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
158 exit(ERR_EXIT);
159 }
160