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