1 | /* $NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled 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. Neither the name of the University nor the names of its contributors
|
---|
16 | * may be used to endorse or promote products derived from this software
|
---|
17 | * without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
29 | * SUCH DAMAGE.
|
---|
30 | */
|
---|
31 |
|
---|
32 | /*#include <sys/cdefs.h>*/
|
---|
33 | #ifndef lint
|
---|
34 | /*__COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
|
---|
35 | The Regents of the University of California. All rights reserved.\n");*/
|
---|
36 | #endif /* not lint */
|
---|
37 |
|
---|
38 | #ifndef lint
|
---|
39 | /*#if 0
|
---|
40 | static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94";
|
---|
41 | #else
|
---|
42 | __RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $");
|
---|
43 | #endif*/
|
---|
44 | #endif /* not lint */
|
---|
45 |
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <sys/stat.h>
|
---|
48 |
|
---|
49 | #include "err.h"
|
---|
50 | #include <errno.h>
|
---|
51 | #include <fcntl.h>
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <string.h>
|
---|
55 | #ifndef _MSC_VER
|
---|
56 | # include <unistd.h>
|
---|
57 | #else
|
---|
58 | # include "mscfakes.h"
|
---|
59 | # if _MSC_VER >= 1400 /* We want 64-bit file lengths here when possible. */
|
---|
60 | # define off_t __int64
|
---|
61 | # define stat _stat64
|
---|
62 | # define fstat _fstat64
|
---|
63 | # define lseek _lseeki64
|
---|
64 | # endif
|
---|
65 | #endif
|
---|
66 | #include <locale.h>
|
---|
67 |
|
---|
68 | #ifndef O_BINARY
|
---|
69 | # define O_BINARY 0
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | /*#include "extern.h"*/
|
---|
73 |
|
---|
74 | static int lflag, sflag;
|
---|
75 |
|
---|
76 | /* this is kind of ugly but its the simplest way to avoid namespace mess. */
|
---|
77 | #include "cmp_misc.c"
|
---|
78 | #include "cmp_special.c"
|
---|
79 | #if defined(__FreeBSD__) || defined(__NetBSD__) /** @todo more mmap capable OSes. */
|
---|
80 | #include "cmp_regular.c"
|
---|
81 | #else
|
---|
82 | #include "cmp_regular_std.c"
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | static int usage(void);
|
---|
86 |
|
---|
87 | int
|
---|
88 | kmk_builtin_cmp(int argc, char *argv[])
|
---|
89 | {
|
---|
90 | struct stat sb1, sb2;
|
---|
91 | off_t skip1 = 0, skip2 = 0;
|
---|
92 | int ch, fd1, fd2, special;
|
---|
93 | char *file1, *file2;
|
---|
94 | int rc;
|
---|
95 |
|
---|
96 | #ifdef kmk_builtin_cmp
|
---|
97 | setlocale(LC_ALL, "");
|
---|
98 | #endif
|
---|
99 | /* init globals */
|
---|
100 | lflag = sflag = 0;
|
---|
101 |
|
---|
102 | /* reset getopt and set progname. */
|
---|
103 | g_progname = argv[0];
|
---|
104 | opterr = 1;
|
---|
105 | optarg = NULL;
|
---|
106 | optopt = 0;
|
---|
107 | optind = 0; /* init */
|
---|
108 |
|
---|
109 | while ((ch = getopt(argc, argv, "ls")) != -1)
|
---|
110 | switch (ch) {
|
---|
111 | case 'l': /* print all differences */
|
---|
112 | lflag = 1;
|
---|
113 | break;
|
---|
114 | case 's': /* silent run */
|
---|
115 | sflag = 1;
|
---|
116 | break;
|
---|
117 | case '?':
|
---|
118 | default:
|
---|
119 | return usage();
|
---|
120 | }
|
---|
121 | argv += optind;
|
---|
122 | argc -= optind;
|
---|
123 |
|
---|
124 | if (lflag && sflag)
|
---|
125 | return errx(ERR_EXIT, "only one of -l and -s may be specified");
|
---|
126 |
|
---|
127 | if (argc < 2 || argc > 4)
|
---|
128 | return usage();
|
---|
129 |
|
---|
130 | /* Backward compatibility -- handle "-" meaning stdin. */
|
---|
131 | special = 0;
|
---|
132 | if (strcmp(file1 = argv[0], "-") == 0) {
|
---|
133 | special = 1;
|
---|
134 | fd1 = 0;
|
---|
135 | file1 = "stdin";
|
---|
136 | }
|
---|
137 | else if ((fd1 = open(file1, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
138 | if (!sflag)
|
---|
139 | warn("%s", file1);
|
---|
140 | return(ERR_EXIT);
|
---|
141 | }
|
---|
142 | if (strcmp(file2 = argv[1], "-") == 0) {
|
---|
143 | if (special)
|
---|
144 | return errx(ERR_EXIT,
|
---|
145 | "standard input may only be specified once");
|
---|
146 | special = 1;
|
---|
147 | fd2 = 0;
|
---|
148 | file2 = "stdin";
|
---|
149 | }
|
---|
150 | else if ((fd2 = open(file2, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
151 | if (!sflag)
|
---|
152 | warn("%s", file2);
|
---|
153 | if (fd1 != 0) close(fd1);
|
---|
154 | return(ERR_EXIT);
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (argc > 2) {
|
---|
158 | char *ep;
|
---|
159 |
|
---|
160 | errno = 0;
|
---|
161 | skip1 = strtoll(argv[2], &ep, 0);
|
---|
162 | if (errno || ep == argv[2]) {
|
---|
163 | rc = usage();
|
---|
164 | goto l_exit;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (argc == 4) {
|
---|
168 | skip2 = strtoll(argv[3], &ep, 0);
|
---|
169 | if (errno || ep == argv[3]) {
|
---|
170 | rc = usage();
|
---|
171 | goto l_exit;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (!special) {
|
---|
177 | if (fstat(fd1, &sb1)) {
|
---|
178 | rc = err(ERR_EXIT, "%s", file1);
|
---|
179 | goto l_exit;
|
---|
180 | }
|
---|
181 | if (!S_ISREG(sb1.st_mode))
|
---|
182 | special = 1;
|
---|
183 | else {
|
---|
184 | if (fstat(fd2, &sb2)) {
|
---|
185 | rc = err(ERR_EXIT, "%s", file2);
|
---|
186 | goto l_exit;
|
---|
187 | }
|
---|
188 | if (!S_ISREG(sb2.st_mode))
|
---|
189 | special = 1;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (special)
|
---|
194 | rc = c_special(fd1, file1, skip1, fd2, file2, skip2);
|
---|
195 | else
|
---|
196 | rc = c_regular(fd1, file1, skip1, sb1.st_size,
|
---|
197 | fd2, file2, skip2, sb2.st_size);
|
---|
198 | l_exit:
|
---|
199 | if (fd1 != 0) close(fd1);
|
---|
200 | if (fd2 != 0) close(fd2);
|
---|
201 | return rc;
|
---|
202 | }
|
---|
203 |
|
---|
204 | static int
|
---|
205 | usage(void)
|
---|
206 | {
|
---|
207 |
|
---|
208 | (void)fprintf(stderr,
|
---|
209 | "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
|
---|
210 | return(ERR_EXIT);
|
---|
211 | }
|
---|