VirtualBox

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

Last change on this file since 3192 was 3192, checked in by bird, 7 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • Property svn:eol-style set to native
File size: 4.5 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/*__COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n");
34static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94";
35__RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $"); */
36
37#ifdef _MSC_VER
38# define MSC_DO_64_BIT_IO /* for correct off_t */
39#endif
40#include "config.h"
41#include <sys/types.h>
42#include "err.h"
43#include <errno.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <locale.h>
48#ifndef _MSC_VER
49# include <unistd.h>
50#else
51# include "mscfakes.h"
52#endif
53#include "getopt.h"
54#include "kmkbuiltin.h"
55#include "cmp_extern.h"
56
57
58static const struct option long_options[] =
59{
60 { "help", no_argument, 0, 261 },
61 { "version", no_argument, 0, 262 },
62 { 0, 0, 0, 0 },
63};
64
65
66static int usage(PKMKBUILTINCTX pCtx, int is_err);
67
68int
69kmk_builtin_cmp(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
70{
71 off_t skip1 = 0, skip2 = 0;
72 int lflag = 0, sflag = 0;
73 int ch;
74 char *file1, *file2;
75
76#ifdef KMK_BUILTIN_STANDALONE
77 setlocale(LC_ALL, "");
78#endif
79
80 /* reset getopt and set progname. */
81 opterr = 1;
82 optarg = NULL;
83 optopt = 0;
84 optind = 0; /* init */
85
86 while ((ch = getopt_long(argc, argv, "ls", long_options, NULL)) != -1)
87 {
88 switch (ch)
89 {
90 case 'l': /* print all differences */
91 lflag = 1;
92 break;
93 case 's': /* silent run */
94 sflag = 1;
95 break;
96 case 261:
97 usage(pCtx, 0);
98 return 0;
99 case 262:
100 return kbuild_version(argv[0]);
101 case '?':
102 default:
103 return usage(pCtx, 1);
104 }
105 }
106 argv += optind;
107 argc -= optind;
108
109 if (argc < 2 || argc > 4)
110 return usage(pCtx, 1);
111
112 file1 = argv[0];
113 file2 = argv[1];
114
115 if (argc > 2)
116 {
117 char *ep;
118
119 errno = 0;
120 skip1 = strtoll(argv[2], &ep, 0);
121 if (errno || ep == argv[2])
122 return errx(pCtx, ERR_EXIT, "strtoll(%s,,) failed", argv[2]);
123
124 if (argc == 4)
125 {
126 skip2 = strtoll(argv[3], &ep, 0);
127 if (errno || ep == argv[3])
128 return errx(pCtx, ERR_EXIT, "strtoll(%s,,) failed", argv[3]);
129 }
130 }
131
132 return cmp_file_and_file_ex(pCtx, file1, skip1, file2, skip2, sflag, lflag, 0);
133}
134
135static int
136usage(PKMKBUILTINCTX pCtx, int is_err)
137{
138 kmk_builtin_ctx_printf(pCtx, is_err,
139 "usage: %s [-l | -s] file1 file2 [skip1 [skip2]]\n"
140 " or: %s --help\n"
141 " or: %s --version\n",
142 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
143 return ERR_EXIT;
144}
145
146#ifdef KMK_BUILTIN_STANDALONE
147int main(int argc, char **argv, char **envp)
148{
149 KMKBUILTINCTX Ctx = { "kmk_cmp", NULL };
150 return kmk_builtin_cmp(argc, argv, envp, &Ctx);
151}
152#endif
153
Note: See TracBrowser for help on using the repository browser.

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