| 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"); | 
|---|
| 34 | static 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 | #include <sys/types.h> | 
|---|
| 38 | #include "err.h" | 
|---|
| 39 | #include <errno.h> | 
|---|
| 40 | #include <stdio.h> | 
|---|
| 41 | #include <stdlib.h> | 
|---|
| 42 | #include <string.h> | 
|---|
| 43 | #include <locale.h> | 
|---|
| 44 | #ifndef _MSC_VER | 
|---|
| 45 | # include <unistd.h> | 
|---|
| 46 | #else | 
|---|
| 47 | # define MSC_DO_64_BIT_IO /* for correct off_t */ | 
|---|
| 48 | # include "mscfakes.h" | 
|---|
| 49 | #endif | 
|---|
| 50 | #include "getopt.h" | 
|---|
| 51 | #include "kmkbuiltin.h" | 
|---|
| 52 | #include "cmp_extern.h" | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | static const struct option long_options[] = | 
|---|
| 56 | { | 
|---|
| 57 | { "help",                                           no_argument, 0, 261 }, | 
|---|
| 58 | { "version",                                        no_argument, 0, 262 }, | 
|---|
| 59 | { 0, 0,     0, 0 }, | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | static int usage(FILE *); | 
|---|
| 64 |  | 
|---|
| 65 | int | 
|---|
| 66 | kmk_builtin_cmp(int argc, char *argv[], char **envp) | 
|---|
| 67 | { | 
|---|
| 68 | off_t skip1 = 0, skip2 = 0; | 
|---|
| 69 | int lflag = 0, sflag = 0; | 
|---|
| 70 | int ch; | 
|---|
| 71 | char *file1, *file2; | 
|---|
| 72 |  | 
|---|
| 73 | #ifdef kmk_builtin_cmp | 
|---|
| 74 | setlocale(LC_ALL, ""); | 
|---|
| 75 | #endif | 
|---|
| 76 |  | 
|---|
| 77 | /* reset getopt and set progname. */ | 
|---|
| 78 | g_progname = argv[0]; | 
|---|
| 79 | opterr = 1; | 
|---|
| 80 | optarg = NULL; | 
|---|
| 81 | optopt = 0; | 
|---|
| 82 | optind = 0; /* init */ | 
|---|
| 83 |  | 
|---|
| 84 | while ((ch = getopt_long(argc, argv, "ls", long_options, NULL)) != -1) | 
|---|
| 85 | { | 
|---|
| 86 | switch (ch) | 
|---|
| 87 | { | 
|---|
| 88 | case 'l':           /* print all differences */ | 
|---|
| 89 | lflag = 1; | 
|---|
| 90 | break; | 
|---|
| 91 | case 's':           /* silent run */ | 
|---|
| 92 | sflag = 1; | 
|---|
| 93 | break; | 
|---|
| 94 | case 261: | 
|---|
| 95 | usage(stdout); | 
|---|
| 96 | return 0; | 
|---|
| 97 | case 262: | 
|---|
| 98 | return kbuild_version(argv[0]); | 
|---|
| 99 | case '?': | 
|---|
| 100 | default: | 
|---|
| 101 | return usage(stderr); | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 | argv += optind; | 
|---|
| 105 | argc -= optind; | 
|---|
| 106 |  | 
|---|
| 107 | if (argc < 2 || argc > 4) | 
|---|
| 108 | return usage(stderr); | 
|---|
| 109 |  | 
|---|
| 110 | file1 = argv[0]; | 
|---|
| 111 | file2 = argv[1]; | 
|---|
| 112 |  | 
|---|
| 113 | if (argc > 2) | 
|---|
| 114 | { | 
|---|
| 115 | char *ep; | 
|---|
| 116 |  | 
|---|
| 117 | errno = 0; | 
|---|
| 118 | skip1 = strtoll(argv[2], &ep, 0); | 
|---|
| 119 | if (errno || ep == argv[2]) | 
|---|
| 120 | return errx(ERR_EXIT, "strtoll(%s,,) failed", argv[2]); | 
|---|
| 121 |  | 
|---|
| 122 | if (argc == 4) | 
|---|
| 123 | { | 
|---|
| 124 | skip2 = strtoll(argv[3], &ep, 0); | 
|---|
| 125 | if (errno || ep == argv[3]) | 
|---|
| 126 | return errx(ERR_EXIT, "strtoll(%s,,) failed", argv[3]); | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | return cmp_file_and_file_ex(file1, skip1, file2, skip2, sflag, lflag, 0); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | static int | 
|---|
| 134 | usage(FILE *fp) | 
|---|
| 135 | { | 
|---|
| 136 | fprintf(fp, "usage: %s [-l | -s] file1 file2 [skip1 [skip2]]\n" | 
|---|
| 137 | "   or: %s --help\n" | 
|---|
| 138 | "   or: %s --version\n", | 
|---|
| 139 | g_progname, g_progname, g_progname); | 
|---|
| 140 | return ERR_EXIT; | 
|---|
| 141 | } | 
|---|