Ignore:
Timestamp:
Dec 2, 2007, 5:53:40 AM (18 years ago)
Author:
bird
Message:

combined the bulk of the cmp stuff into cmp_util.c. implemented cp --changed.

File:
1 edited

Legend:

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

    r1246 r1309  
    3030 */
    3131
    32 /*#include <sys/cdefs.h>*/
    33 #ifndef lint
    3432/*__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
     33        The Regents of the University of California.  All rights reserved.\n");
    4034static 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 */
     35__RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $"); */
    4536
    4637#include <sys/types.h>
    47 #include <sys/stat.h>
    48 
    4938#include "err.h"
    5039#include <errno.h>
    51 #include <fcntl.h>
    5240#include <stdio.h>
    5341#include <stdlib.h>
    5442#include <string.h>
     43#include <locale.h>
    5544#ifndef _MSC_VER
    5645# include <unistd.h>
    5746#else
     47# define MSC_DO_64_BIT_IO /* for correct off_t */
    5848# 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
    6549#endif
    66 #include <locale.h>
    6750#include "getopt.h"
    6851
    69 #ifndef O_BINARY
    70 # define O_BINARY 0
    71 #endif
    72 
    73 /*#include "extern.h"*/
    74 
    7552#include "kmkbuiltin.h"
     53#include "cmp_extern.h"
    7654
    7755
    78 static int      lflag, sflag;
    79 
    80 static struct option long_options[] =
     56static const struct option long_options[] =
    8157{
    8258    { "help",                                           no_argument, 0, 261 },
     
    8662
    8763
    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 
    9764static int usage(FILE *);
    9865
     
    10067kmk_builtin_cmp(int argc, char *argv[], char **envp)
    10168{
    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;
     69    off_t skip1 = 0, skip2 = 0;
     70    int lflag = 0, sflag = 0;
     71    int ch;
     72    char *file1, *file2;
    10773
    10874#ifdef kmk_builtin_cmp
    109         setlocale(LC_ALL, "");
     75    setlocale(LC_ALL, "");
    11076#endif
    111         /* init globals */
    112         lflag = sflag = 0;
    11377
    114         /* reset getopt and set progname. */
    115         g_progname = argv[0];
    116         opterr = 1;
    117         optarg = NULL;
    118         optopt = 0;
    119         optind = 0; /* init */
     78    /* reset getopt and set progname. */
     79    g_progname = argv[0];
     80    opterr = 1;
     81    optarg = NULL;
     82    optopt = 0;
     83    optind = 0; /* init */
    12084
    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;
     85    while ((ch = getopt_long(argc, argv, "ls", long_options, NULL)) != -1)
     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:
     96                usage(stdout);
     97                return 0;
     98            case 262:
     99                return kbuild_version(argv[0]);
     100            case '?':
     101            default:
     102                return usage(stderr);
     103        }
     104    }
     105    argv += optind;
     106    argc -= optind;
    140107
    141         if (lflag && sflag)
    142                 return errx(ERR_EXIT, "only one of -l and -s may be specified");
     108    if (argc < 2 || argc > 4)
     109        return usage(stderr);
    143110
    144         if (argc < 2 || argc > 4)
    145                 return usage(stderr);
     111    file1 = argv[0];
     112    file2 = argv[1];
    146113
    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";
     114    if (argc > 2)
     115    {
     116        char *ep;
     117
     118        errno = 0;
     119        skip1 = strtoll(argv[2], &ep, 0);
     120        if (errno || ep == argv[2])
     121            return errx(ERR_EXIT, "strtoll(%s,,) failed", argv[2]);
     122
     123        if (argc == 4)
     124        {
     125            skip2 = strtoll(argv[3], &ep, 0);
     126            if (errno || ep == argv[3])
     127                return errx(ERR_EXIT, "strtoll(%s,,) failed", argv[3]);
    153128        }
    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         }
     129    }
    173130
    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);
    215 l_exit:
    216         if (fd1 != 0) close(fd1);
    217         if (fd2 != 0) close(fd2);
    218         return rc;
     131    return cmp_file_and_file_ex(file1, skip1, file2, skip2, sflag, lflag, 0);
    219132}
    220133
     
    223136{
    224137    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);
     138                "   or: %s --help\n"
     139                "   or: %s --version\n",
     140            g_progname, g_progname, g_progname);
     141    return ERR_EXIT;
    229142}
Note: See TracChangeset for help on using the changeset viewer.