Changeset 1117 in kBuild
- Timestamp:
- Sep 25, 2007 5:03:31 AM (17 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 10 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/Makefile.kmk
r1109 r1117 158 158 kmkbuiltin/append.c \ 159 159 kmkbuiltin/cat.c \ 160 kmkbuiltin/cmp.c \ 160 161 kmkbuiltin/cp.c \ 161 162 kmkbuiltin/cp_utils.c \ … … 174 175 # Standalone kmkbuiltin commands. 175 176 # 176 ## @todo should probably put all those 177 PROGRAMS += kmk_append kmk_cat kmk_cp kmk_echo kmk_md5sum kmk_mkdir kmk_mv kmk_install kmk_ln kmk_printf kmk_rm kmk_rmdir 177 PROGRAMS += kmk_append kmk_cat kmk_cp kmk_cmp kmk_echo kmk_md5sum kmk_mkdir kmk_mv kmk_install kmk_ln kmk_printf kmk_rm kmk_rmdir 178 178 179 179 kmk_append_TEMPLATE = BIN-KMK … … 187 187 kmk_cat_SOURCES = \ 188 188 kmkbuiltin/cat.c 189 190 kmk_cmp_TEMPLATE = BIN-KMK 191 kmk_cmp_DEFS = kmk_builtin_cmp=main 192 kmk_cmp_SOURCES = \ 193 kmkbuiltin/cmp.c 189 194 190 195 kmk_cp_TEMPLATE = BIN-KMK -
trunk/src/kmk/kmkbuiltin.c
r1101 r1117 207 207 else if (!strcmp(pszCmd, "md5sum")) 208 208 rc = kmk_builtin_md5sum(argc, argv, environ); 209 else if (!strcmp(pszCmd, "cmp")) 210 rc = kmk_builtin_cmp(argc, argv, environ); 209 211 else if (!strcmp(pszCmd, "cat")) 210 212 rc = kmk_builtin_cat(argc, argv, environ); -
trunk/src/kmk/kmkbuiltin.h
r1101 r1117 31 31 extern int kmk_builtin_cp(int argc, char **argv, char **envp); 32 32 extern int kmk_builtin_cat(int argc, char **argv, char **envp); 33 extern int kmk_builtin_cmp(int argc, char **argv, char **envp); 33 34 extern int kmk_builtin_echo(int argc, char **argv, char **envp); 34 35 extern int kmk_builtin_install(int argc, char **argv, char **envp); -
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
-
trunk/src/kmk/kmkbuiltin/cmp_extern.h
r1116 r1117 36 36 #define ERR_EXIT 2 /* error exit code */ 37 37 38 voidc_regular(int, char *, off_t, off_t, int, char *, off_t, off_t);39 voidc_special(int, char *, off_t, int, char *, off_t);40 voiddiffmsg(char *, char *, off_t, off_t);41 voideofmsg(char *, off_t, off_t);42 voiderrmsg(char *, off_t, off_t);38 static int c_regular(int, char *, off_t, off_t, int, char *, off_t, off_t); 39 static int c_special(int, char *, off_t, int, char *, off_t); 40 static int diffmsg(char *, char *, off_t, off_t); 41 static int eofmsg(char *, off_t, off_t); 42 static int errmsg(char *, off_t, off_t); 43 43 44 extern int lflag, sflag; 44 /*extern int lflag, sflag;*/ -
trunk/src/kmk/kmkbuiltin/cmp_misc.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 #if 034 /*#if 0 35 35 static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; 36 36 #else 37 37 __RCSID("$NetBSD: misc.c,v 1.11 2007/08/22 16:59:19 christos Exp $"); 38 #endif 38 #endif*/ 39 39 #endif /* not lint */ 40 40 41 41 #include <sys/types.h> 42 42 43 #include <err.h>43 #include "err.h" 44 44 #include <stdio.h> 45 45 #include <stdlib.h> 46 46 47 #include " extern.h"47 #include "cmp_extern.h" 48 48 49 void 49 static int 50 50 errmsg(char *file, off_t byte, off_t line) 51 51 { 52 52 if (lflag) 53 err(ERR_EXIT, "%s: char %lld, line %lld", file,53 return err(ERR_EXIT, "%s: char %lld, line %lld", file, 54 54 (long long)byte, (long long)line); 55 55 else 56 err(ERR_EXIT, "%s", file);56 return err(ERR_EXIT, "%s", file); 57 57 } 58 58 59 void 59 static int 60 60 eofmsg(char *file, off_t byte, off_t line) 61 61 { … … 72 72 } 73 73 } 74 exit(DIFF_EXIT);74 return(DIFF_EXIT); 75 75 } 76 76 77 void 77 static int 78 78 diffmsg(char *file1, char *file2, off_t byte, off_t line) 79 79 { … … 81 81 (void)printf("%s %s differ: char %lld, line %lld\n", 82 82 file1, file2, (long long)byte, (long long)line); 83 exit(DIFF_EXIT);83 return(DIFF_EXIT); 84 84 } -
Property svn:eol-style
set to
-
trunk/src/kmk/kmkbuiltin/cmp_regular.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 #if 034 /*#if 0 35 35 static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94"; 36 36 #else 37 37 __RCSID("$NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $"); 38 #endif 38 #endif*/ 39 39 #endif /* not lint */ 40 40 … … 43 43 #include <sys/stat.h> 44 44 45 #include <err.h>45 #include "err.h" 46 46 #include <limits.h> 47 47 #include <stdlib.h> … … 49 49 #include <string.h> 50 50 51 #include " extern.h"51 #include "cmp_extern.h" 52 52 53 void 53 static int 54 54 c_regular(int fd1, char *file1, off_t skip1, off_t len1, 55 55 int fd2, char *file2, off_t skip2, off_t len2) 56 56 { 57 u_char ch, *p1, *p2 ;57 u_char ch, *p1, *p2, *b1, *b2; 58 58 off_t byte, length, line; 59 59 int dfound; … … 61 61 62 62 if (sflag && len1 != len2) 63 exit(1);63 return(1); 64 64 65 65 if (skip1 > len1) 66 eofmsg(file1, len1 + 1, 0);66 return eofmsg(file1, len1 + 1, 0); 67 67 len1 -= skip1; 68 68 if (skip2 > len2) 69 eofmsg(file2, len2 + 1, 0);69 return eofmsg(file2, len2 + 1, 0); 70 70 len2 -= skip2; 71 71 … … 76 76 if (blk_sz > length) 77 77 blk_sz = length; 78 p1 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED,78 b1 = p1 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED, 79 79 fd1, skip1); 80 80 if (p1 == MAP_FAILED) 81 81 goto mmap_failed; 82 82 83 p2 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED,83 b2 = p2 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED, 84 84 fd2, skip2); 85 85 if (p2 == MAP_FAILED) { … … 92 92 if ((ch = *p1) != *p2) { 93 93 if (!lflag) { 94 diffmsg(file1, file2, byte, line); 95 /* NOTREACHED */ 94 munmap(b1, blk_sz); 95 munmap(b2, blk_sz); 96 return diffmsg(file1, file2, byte, line); 96 97 } 97 98 dfound = 1; … … 109 110 110 111 if (len1 != len2) 111 eofmsg(len1 > len2 ? file2 : file1, byte, line);112 return eofmsg(len1 > len2 ? file2 : file1, byte, line); 112 113 if (dfound) 113 exit(DIFF_EXIT);114 return ;114 return(DIFF_EXIT); 115 return(0); 115 116 116 117 mmap_failed: 117 c_special(fd1, file1, skip1, fd2, file2, skip2);118 return c_special(fd1, file1, skip1, fd2, file2, skip2); 118 119 } 120 -
Property svn:eol-style
set to
-
trunk/src/kmk/kmkbuiltin/cmp_regular_std.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 #if 034 /*#if 0 35 35 static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94"; 36 36 #else 37 37 __RCSID("$NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $"); 38 #endif 38 #endif*/ 39 39 #endif /* not lint */ 40 40 41 #include <sys/param.h>42 #include <sys/mman.h>43 41 #include <sys/stat.h> 44 42 45 #include <err.h>43 #include "err.h" 46 44 #include <limits.h> 47 45 #include <stdlib.h> … … 49 47 #include <string.h> 50 48 51 #include " extern.h"49 #include "cmp_extern.h" 52 50 53 void 51 static int 54 52 c_regular(int fd1, char *file1, off_t skip1, off_t len1, 55 53 int fd2, char *file2, off_t skip2, off_t len2) 56 54 { 57 u _char ch, *p1, *p2;58 off_t byte, length, line ;55 unsigned char ch, *p1, *p2, *b1 = 0, *b2 = 0; 56 off_t byte, length, line, bytes_read; 59 57 int dfound; 60 58 size_t blk_sz, blk_cnt; 61 59 62 60 if (sflag && len1 != len2) 63 exit(1);61 return(1); 64 62 65 63 if (skip1 > len1) 66 eofmsg(file1, len1 + 1, 0);64 return eofmsg(file1, len1 + 1, 0); 67 65 len1 -= skip1; 68 66 if (skip2 > len2) 69 eofmsg(file2, len2 + 1, 0);67 return eofmsg(file2, len2 + 1, 0); 70 68 len2 -= skip2; 69 70 if (skip1 && lseek(fd1, skip1, SEEK_SET) < 0) 71 goto l_special; 72 if (skip2 && lseek(fd2, skip2, SEEK_SET) < 0) { 73 if (skip1 && lseek(fd1, 0, SEEK_SET) < 0) 74 return err(1, "seek failed"); 75 goto l_special; 76 } 77 78 #define CMP_BUF_SIZE (128*1024) 79 80 b1 = malloc(CMP_BUF_SIZE); 81 b2 = malloc(CMP_BUF_SIZE); 82 if (!b1 || !b2) 83 goto l_malloc_failed; 71 84 72 85 byte = line = 1; 73 86 dfound = 0; 74 length = MIN(len1, len2); 75 for (blk_sz = 1024 * 1024; length != 0; length -= blk_sz) { 76 if (blk_sz > length) 87 length = len1; 88 if (length > len2) 89 length = len2; 90 for (blk_sz = CMP_BUF_SIZE; length != 0; length -= blk_sz) { 91 if ((off_t)blk_sz > length) 77 92 blk_sz = length; 78 p1 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED,79 fd1, skip1);80 if (p1 == MAP_FAILED)81 goto mmap_failed;82 93 83 p2 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED, 84 fd2, skip2); 85 if (p2 == MAP_FAILED) { 86 munmap(p1, blk_sz); 87 goto mmap_failed; 88 } 94 bytes_read = read(fd1, b1, blk_sz); 95 if (bytes_read != blk_sz) 96 goto l_read_error; 97 98 bytes_read = read(fd2, b2, blk_sz); 99 if (bytes_read != blk_sz) 100 goto l_read_error; 89 101 90 102 blk_cnt = blk_sz; 103 p1 = b1; 104 p2 = b2; 91 105 for (; blk_cnt--; ++p1, ++p2, ++byte) { 92 106 if ((ch = *p1) != *p2) { 93 107 if (!lflag) { 94 diffmsg(file1, file2, byte, line); 95 /* NOTREACHED */ 108 free(b1); 109 free(b2); 110 return diffmsg(file1, file2, byte, line); 96 111 } 97 112 dfound = 1; … … 102 117 ++line; 103 118 } 104 munmap(p1 - blk_sz, blk_sz);105 munmap(p2 - blk_sz, blk_sz);106 119 skip1 += blk_sz; 107 120 skip2 += blk_sz; … … 109 122 110 123 if (len1 != len2) 111 eofmsg(len1 > len2 ? file2 : file1, byte, line);124 return eofmsg(len1 > len2 ? file2 : file1, byte, line); 112 125 if (dfound) 113 exit(DIFF_EXIT);114 return ;126 return(DIFF_EXIT); 127 return(0); 115 128 116 mmap_failed: 117 c_special(fd1, file1, skip1, fd2, file2, skip2); 129 l_read_error: 130 if ( lseek(fd1, 0, SEEK_SET) < 0 131 || lseek(fd2, 0, SEEK_SET) < 0) { 132 err(1, "seek failed"); 133 free(b1); 134 free(b2); 135 return 1; 136 } 137 l_malloc_failed: 138 free(b1); 139 free(b2); 140 l_special: 141 return c_special(fd1, file1, skip1, fd2, file2, skip2); 118 142 } 143 -
Property svn:eol-style
set to
-
trunk/src/kmk/kmkbuiltin/cmp_special.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 #if 034 /*#if 0 35 35 static char sccsid[] = "@(#)special.c 8.3 (Berkeley) 4/2/94"; 36 36 #else 37 37 __RCSID("$NetBSD: special.c,v 1.12 2007/08/21 14:09:54 christos Exp $"); 38 #endif 38 #endif*/ 39 39 #endif /* not lint */ 40 40 41 41 #include <sys/types.h> 42 42 43 #include <err.h>43 #include "err.h" 44 44 #include <stdlib.h> 45 45 #include <stdio.h> 46 46 #include <string.h> 47 47 48 #include " extern.h"48 #include "cmp_extern.h" 49 49 50 void 50 static int 51 51 c_special(int fd1, char *file1, off_t skip1, int fd2, char *file2, off_t skip2) 52 52 { … … 58 58 dfound = 0; 59 59 if ((fp1 = fdopen(fd1, "r")) == NULL) 60 err(ERR_EXIT, "%s", file1);60 return err(ERR_EXIT, "%s", file1); 61 61 if ((fp2 = fdopen(fd2, "r")) == NULL) 62 err(ERR_EXIT, "%s", file2);62 return err(ERR_EXIT, "%s", file2); 63 63 64 64 for (byte = line = 1; skip1--; byte++) { … … 88 88 ch1, ch2); 89 89 } else 90 diffmsg(file1, file2, byte, line); 91 /* NOTREACHED */ 90 return diffmsg(file1, file2, byte, line); 92 91 } 93 92 if (ch1 == '\n') … … 97 96 eof: 98 97 if (ferror(fp1)) 99 errmsg(file1, byte, line);98 return errmsg(file1, byte, line); 100 99 if (ferror(fp2)) 101 errmsg(file2, byte, line);100 return errmsg(file2, byte, line); 102 101 if (feof(fp1)) { 103 102 if (!feof(fp2)) 104 eofmsg(file1, byte, line);103 return eofmsg(file1, byte, line); 105 104 } else 106 105 if (feof(fp2)) 107 eofmsg(file2, byte, line);106 return eofmsg(file2, byte, line); 108 107 (void)fclose(fp1); 109 108 (void)fclose(fp2); 110 109 if (dfound) 111 exit(DIFF_EXIT); 110 return(DIFF_EXIT); 111 return(0); 112 112 } -
Property svn:eol-style
set to
-
trunk/src/kmk/kmkbuiltin/mscfakes.h
r936 r1117 126 126 intmax_t strtoimax(const char *nptr, char **endptr, int base); 127 127 uintmax_t strtoumax(const char *nptr, char **endptr, int base); 128 #define strtoll(a,b,c) strtoimax(a,b,c) 129 #define strtoull(a,b,c) strtoumax(a,b,c) 128 130 int asprintf(char **strp, const char *fmt, ...); 129 131 int vasprintf(char **strp, const char *fmt, va_list ap); -
trunk/src/kmk/variable.c
r1109 r1117 1095 1095 #ifdef CONFIG_WITH_KMK_BUILTIN 1096 1096 /* The supported kMk Builtin commands. */ 1097 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp echo install ln md5sum mkdir mv printf rm rmdir", o_default, 0);1097 (void) define_variable ("KMK_BUILTIN", 11, "append cat cp cmp echo install ln md5sum mkdir mv printf rm rmdir", o_default, 0); 1098 1098 #endif 1099 1099
Note:
See TracChangeset
for help on using the changeset viewer.