Changeset 1309 for trunk/src/kmk/kmkbuiltin/cmp.c
- Timestamp:
- Dec 2, 2007, 5:53:40 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/cmp.c
r1246 r1309 30 30 */ 31 31 32 /*#include <sys/cdefs.h>*/33 #ifndef lint34 32 /*__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"); 40 34 static 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 $"); */ 45 36 46 37 #include <sys/types.h> 47 #include <sys/stat.h>48 49 38 #include "err.h" 50 39 #include <errno.h> 51 #include <fcntl.h>52 40 #include <stdio.h> 53 41 #include <stdlib.h> 54 42 #include <string.h> 43 #include <locale.h> 55 44 #ifndef _MSC_VER 56 45 # include <unistd.h> 57 46 #else 47 # define MSC_DO_64_BIT_IO /* for correct off_t */ 58 48 # include "mscfakes.h" 59 # if _MSC_VER >= 1400 /* We want 64-bit file lengths here when possible. */60 # define off_t __int6461 # define stat _stat6462 # define fstat _fstat6463 # define lseek _lseeki6464 # endif65 49 #endif 66 #include <locale.h>67 50 #include "getopt.h" 68 51 69 #ifndef O_BINARY70 # define O_BINARY 071 #endif72 73 /*#include "extern.h"*/74 75 52 #include "kmkbuiltin.h" 53 #include "cmp_extern.h" 76 54 77 55 78 static int lflag, sflag; 79 80 static struct option long_options[] = 56 static const struct option long_options[] = 81 57 { 82 58 { "help", no_argument, 0, 261 }, … … 86 62 87 63 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 #else94 #include "cmp_regular_std.c"95 #endif96 97 64 static int usage(FILE *); 98 65 … … 100 67 kmk_builtin_cmp(int argc, char *argv[], char **envp) 101 68 { 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; 107 73 108 74 #ifdef kmk_builtin_cmp 109 75 setlocale(LC_ALL, ""); 110 76 #endif 111 /* init globals */112 lflag = sflag = 0;113 77 114 115 116 117 118 119 78 /* reset getopt and set progname. */ 79 g_progname = argv[0]; 80 opterr = 1; 81 optarg = NULL; 82 optopt = 0; 83 optind = 0; /* init */ 120 84 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; 140 107 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); 143 110 144 if (argc < 2 || argc > 4) 145 return usage(stderr);111 file1 = argv[0]; 112 file2 = argv[1]; 146 113 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]); 153 128 } 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 } 173 130 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); 219 132 } 220 133 … … 223 136 { 224 137 fprintf(fp, "usage: %s [-l | -s] file1 file2 [skip1 [skip2]]\n" 225 226 227 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; 229 142 }
Note:
See TracChangeset
for help on using the changeset viewer.