Changeset 1117 for trunk/src/kmk/kmkbuiltin/cmp.c
- Timestamp:
- Sep 25, 2007, 7:03:31 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/cmp.c
-
Property svn:eol-style
set to
native
r1115 r1117 30 30 */ 31 31 32 #include <sys/cdefs.h> 32 /*#include <sys/cdefs.h>*/ 33 33 #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");*/ 36 36 #endif /* not lint */ 37 37 38 38 #ifndef lint 39 #if 039 /*#if 0 40 40 static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94"; 41 41 #else 42 42 __RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $"); 43 #endif 43 #endif*/ 44 44 #endif /* not lint */ 45 45 … … 47 47 #include <sys/stat.h> 48 48 49 #include <err.h>49 #include "err.h" 50 50 #include <errno.h> 51 51 #include <fcntl.h> … … 53 53 #include <stdlib.h> 54 54 #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 56 66 #include <locale.h> 57 67 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 74 static 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 85 static int usage(void); 63 86 64 87 int 65 main(int argc, char *argv[])88 kmk_builtin_cmp(int argc, char *argv[]) 66 89 { 67 90 struct stat sb1, sb2; … … 69 92 int ch, fd1, fd2, special; 70 93 char *file1, *file2; 71 94 int rc; 95 96 #ifdef kmk_builtin_cmp 72 97 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 */ 73 108 74 109 while ((ch = getopt(argc, argv, "ls")) != -1) … … 82 117 case '?': 83 118 default: 84 usage();119 return usage(); 85 120 } 86 121 argv += optind; … … 88 123 89 124 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"); 91 126 92 127 if (argc < 2 || argc > 4) 93 usage();128 return usage(); 94 129 95 130 /* Backward compatibility -- handle "-" meaning stdin. */ … … 100 135 file1 = "stdin"; 101 136 } 102 else if ((fd1 = open(file1, O_RDONLY , 0)) < 0) {137 else if ((fd1 = open(file1, O_RDONLY | O_BINARY, 0)) < 0) { 103 138 if (!sflag) 104 139 warn("%s", file1); 105 exit(ERR_EXIT);140 return(ERR_EXIT); 106 141 } 107 142 if (strcmp(file2 = argv[1], "-") == 0) { 108 143 if (special) 109 errx(ERR_EXIT,144 return errx(ERR_EXIT, 110 145 "standard input may only be specified once"); 111 146 special = 1; … … 113 148 file2 = "stdin"; 114 149 } 115 else if ((fd2 = open(file2, O_RDONLY , 0)) < 0) {150 else if ((fd2 = open(file2, O_RDONLY | O_BINARY, 0)) < 0) { 116 151 if (!sflag) 117 152 warn("%s", file2); 118 exit(ERR_EXIT); 153 if (fd1 != 0) close(fd1); 154 return(ERR_EXIT); 119 155 } 120 156 … … 123 159 124 160 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 } 128 166 129 167 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 } 133 173 } 134 174 } 135 175 136 176 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 } 139 181 if (!S_ISREG(sb1.st_mode)) 140 182 special = 1; 141 183 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 } 144 188 if (!S_ISREG(sb2.st_mode)) 145 189 special = 1; … … 148 192 149 193 if (special) 150 c_special(fd1, file1, skip1, fd2, file2, skip2);194 rc = c_special(fd1, file1, skip1, fd2, file2, skip2); 151 195 else 152 c_regular(fd1, file1, skip1, sb1.st_size,196 rc = c_regular(fd1, file1, skip1, sb1.st_size, 153 197 fd2, file2, skip2, sb2.st_size); 154 exit(0); 198 l_exit: 199 if (fd1 != 0) close(fd1); 200 if (fd2 != 0) close(fd2); 201 return rc; 155 202 } 156 203 157 static void204 static int 158 205 usage(void) 159 206 { … … 161 208 (void)fprintf(stderr, 162 209 "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n"); 163 exit(ERR_EXIT);210 return(ERR_EXIT); 164 211 } -
Property svn:eol-style
set to
Note:
See TracChangeset
for help on using the changeset viewer.