Ignore:
Timestamp:
Sep 25, 2007, 7:03:31 AM (18 years ago)
Author:
bird
Message:

kmk_builtin_cmp.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/cmp.c

    • Property svn:eol-style set to native
    r1115 r1117  
    3030 */
    3131
    32 #include <sys/cdefs.h>
     32/*#include <sys/cdefs.h>*/
    3333#ifndef lint
    34 __COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
    35         The Regents of the University of California.  All rights reserved.\n");
     34/*__COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
     35        The Regents of the University of California.  All rights reserved.\n");*/
    3636#endif /* not lint */
    3737
    3838#ifndef lint
    39 #if 0
     39/*#if 0
    4040static char sccsid[] = "@(#)cmp.c       8.3 (Berkeley) 4/2/94";
    4141#else
    4242__RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $");
    43 #endif
     43#endif*/
    4444#endif /* not lint */
    4545
     
    4747#include <sys/stat.h>
    4848
    49 #include <err.h>
     49#include "err.h"
    5050#include <errno.h>
    5151#include <fcntl.h>
     
    5353#include <stdlib.h>
    5454#include <string.h>
    55 #include <unistd.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
    5666#include <locale.h>
    5767
    58 #include "extern.h"
    59 
    60 int     lflag, sflag;
    61 
    62 static void usage(void);
     68#ifndef O_BINARY
     69# define O_BINARY 0
     70#endif
     71
     72/*#include "extern.h"*/
     73
     74static 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
     85static int usage(void);
    6386
    6487int
    65 main(int argc, char *argv[])
     88kmk_builtin_cmp(int argc, char *argv[])
    6689{
    6790        struct stat sb1, sb2;
     
    6992        int ch, fd1, fd2, special;
    7093        char *file1, *file2;
    71 
     94        int rc;
     95
     96#ifdef kmk_builtin_cmp
    7297        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 */
    73108
    74109        while ((ch = getopt(argc, argv, "ls")) != -1)
     
    82117                case '?':
    83118                default:
    84                         usage();
     119                        return usage();
    85120                }
    86121        argv += optind;
     
    88123
    89124        if (lflag && sflag)
    90                 errx(ERR_EXIT, "only one of -l and -s may be specified");
     125                return errx(ERR_EXIT, "only one of -l and -s may be specified");
    91126
    92127        if (argc < 2 || argc > 4)
    93                 usage();
     128                return usage();
    94129
    95130        /* Backward compatibility -- handle "-" meaning stdin. */
     
    100135                file1 = "stdin";
    101136        }
    102         else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
     137        else if ((fd1 = open(file1, O_RDONLY | O_BINARY, 0)) < 0) {
    103138                if (!sflag)
    104139                        warn("%s", file1);
    105                 exit(ERR_EXIT);
     140                return(ERR_EXIT);
    106141        }
    107142        if (strcmp(file2 = argv[1], "-") == 0) {
    108143                if (special)
    109                         errx(ERR_EXIT,
     144                        return errx(ERR_EXIT,
    110145                                "standard input may only be specified once");
    111146                special = 1;
     
    113148                file2 = "stdin";
    114149        }
    115         else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
     150        else if ((fd2 = open(file2, O_RDONLY | O_BINARY, 0)) < 0) {
    116151                if (!sflag)
    117152                        warn("%s", file2);
    118                 exit(ERR_EXIT);
     153                if (fd1 != 0) close(fd1);
     154                return(ERR_EXIT);
    119155        }
    120156
     
    123159
    124160                errno = 0;
    125                 skip1 = strtoq(argv[2], &ep, 0);
    126                 if (errno || ep == argv[2])
    127                         usage();
     161                skip1 = strtoll(argv[2], &ep, 0);
     162                if (errno || ep == argv[2]) {
     163                        rc = usage();
     164                        goto l_exit;
     165                }
    128166
    129167                if (argc == 4) {
    130                         skip2 = strtoq(argv[3], &ep, 0);
    131                         if (errno || ep == argv[3])
    132                                 usage();
     168                        skip2 = strtoll(argv[3], &ep, 0);
     169                        if (errno || ep == argv[3]) {
     170                                rc = usage();
     171                                goto l_exit;
     172                        }
    133173                }
    134174        }
    135175
    136176        if (!special) {
    137                 if (fstat(fd1, &sb1))
    138                         err(ERR_EXIT, "%s", file1);
     177                if (fstat(fd1, &sb1)) {
     178                        rc = err(ERR_EXIT, "%s", file1);
     179                        goto l_exit;
     180                }
    139181                if (!S_ISREG(sb1.st_mode))
    140182                        special = 1;
    141183                else {
    142                         if (fstat(fd2, &sb2))
    143                                 err(ERR_EXIT, "%s", file2);
     184                        if (fstat(fd2, &sb2)) {
     185                                rc = err(ERR_EXIT, "%s", file2);
     186                                goto l_exit;
     187                        }
    144188                        if (!S_ISREG(sb2.st_mode))
    145189                                special = 1;
     
    148192
    149193        if (special)
    150                 c_special(fd1, file1, skip1, fd2, file2, skip2);
     194                rc = c_special(fd1, file1, skip1, fd2, file2, skip2);
    151195        else
    152                 c_regular(fd1, file1, skip1, sb1.st_size,
     196                rc = c_regular(fd1, file1, skip1, sb1.st_size,
    153197                    fd2, file2, skip2, sb2.st_size);
    154         exit(0);
     198l_exit:
     199        if (fd1 != 0) close(fd1);
     200        if (fd2 != 0) close(fd2);
     201        return rc;
    155202}
    156203
    157 static void
     204static int
    158205usage(void)
    159206{
     
    161208        (void)fprintf(stderr,
    162209            "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
    163         exit(ERR_EXIT);
     210        return(ERR_EXIT);
    164211}
Note: See TracChangeset for help on using the changeset viewer.