VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/cmp.c@ 1264

Last change on this file since 1264 was 1246, checked in by bird, 17 years ago

Solaris build fixes.

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
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
40static 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#include "getopt.h"
68
69#ifndef O_BINARY
70# define O_BINARY 0
71#endif
72
73/*#include "extern.h"*/
74
75#include "kmkbuiltin.h"
76
77
78static int lflag, sflag;
79
80static struct option long_options[] =
81{
82 { "help", no_argument, 0, 261 },
83 { "version", no_argument, 0, 262 },
84 { 0, 0, 0, 0 },
85};
86
87
88/* this is kind of ugly but its the simplest way to avoid namespace mess. */
89#include "cmp_misc.c"
90#include "cmp_special.c"
91#if defined(__FreeBSD__) || defined(__NetBSD__) /** @todo more mmap capable OSes. */
92#include "cmp_regular.c"
93#else
94#include "cmp_regular_std.c"
95#endif
96
97static int usage(FILE *);
98
99int
100kmk_builtin_cmp(int argc, char *argv[], char **envp)
101{
102 struct stat sb1, sb2;
103 off_t skip1 = 0, skip2 = 0;
104 int ch, fd1, fd2, special;
105 char *file1, *file2;
106 int rc;
107
108#ifdef kmk_builtin_cmp
109 setlocale(LC_ALL, "");
110#endif
111 /* init globals */
112 lflag = sflag = 0;
113
114 /* reset getopt and set progname. */
115 g_progname = argv[0];
116 opterr = 1;
117 optarg = NULL;
118 optopt = 0;
119 optind = 0; /* init */
120
121 while ((ch = getopt_long(argc, argv, "ls", long_options, NULL)) != -1)
122 switch (ch) {
123 case 'l': /* print all differences */
124 lflag = 1;
125 break;
126 case 's': /* silent run */
127 sflag = 1;
128 break;
129 case 261:
130 usage(stdout);
131 return 0;
132 case 262:
133 return kbuild_version(argv[0]);
134 case '?':
135 default:
136 return usage(stderr);
137 }
138 argv += optind;
139 argc -= optind;
140
141 if (lflag && sflag)
142 return errx(ERR_EXIT, "only one of -l and -s may be specified");
143
144 if (argc < 2 || argc > 4)
145 return usage(stderr);
146
147 /* Backward compatibility -- handle "-" meaning stdin. */
148 special = 0;
149 if (strcmp(file1 = argv[0], "-") == 0) {
150 special = 1;
151 fd1 = 0;
152 file1 = "stdin";
153 }
154 else if ((fd1 = open(file1, O_RDONLY | O_BINARY, 0)) < 0) {
155 if (!sflag)
156 warn("%s", file1);
157 return(ERR_EXIT);
158 }
159 if (strcmp(file2 = argv[1], "-") == 0) {
160 if (special)
161 return errx(ERR_EXIT,
162 "standard input may only be specified once");
163 special = 1;
164 fd2 = 0;
165 file2 = "stdin";
166 }
167 else if ((fd2 = open(file2, O_RDONLY | O_BINARY, 0)) < 0) {
168 if (!sflag)
169 warn("%s", file2);
170 if (fd1 != 0) close(fd1);
171 return(ERR_EXIT);
172 }
173
174 if (argc > 2) {
175 char *ep;
176
177 errno = 0;
178 skip1 = strtoll(argv[2], &ep, 0);
179 if (errno || ep == argv[2]) {
180 rc = usage(stderr);
181 goto l_exit;
182 }
183
184 if (argc == 4) {
185 skip2 = strtoll(argv[3], &ep, 0);
186 if (errno || ep == argv[3]) {
187 rc = usage(stderr);
188 goto l_exit;
189 }
190 }
191 }
192
193 if (!special) {
194 if (fstat(fd1, &sb1)) {
195 rc = err(ERR_EXIT, "%s", file1);
196 goto l_exit;
197 }
198 if (!S_ISREG(sb1.st_mode))
199 special = 1;
200 else {
201 if (fstat(fd2, &sb2)) {
202 rc = err(ERR_EXIT, "%s", file2);
203 goto l_exit;
204 }
205 if (!S_ISREG(sb2.st_mode))
206 special = 1;
207 }
208 }
209
210 if (special)
211 rc = c_special(fd1, file1, skip1, fd2, file2, skip2);
212 else
213 rc = c_regular(fd1, file1, skip1, sb1.st_size,
214 fd2, file2, skip2, sb2.st_size);
215l_exit:
216 if (fd1 != 0) close(fd1);
217 if (fd2 != 0) close(fd2);
218 return rc;
219}
220
221static int
222usage(FILE *fp)
223{
224 fprintf(fp, "usage: %s [-l | -s] file1 file2 [skip1 [skip2]]\n"
225 " or: %s --help\n"
226 " or: %s --version\n",
227 g_progname, g_progname, g_progname);
228 return(ERR_EXIT);
229}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette