1 /* $NetBSD: cksum.c,v 1.8 1997/01/30 01:10:34 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * James W. Williams of NASA Goddard Space Flight Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #ifndef lint 41 static char copyright[] = 42 "@(#) Copyright (c) 1991, 1993\n\ 43 The Regents of the University of California. All rights reserved.\n"; 44 #endif /* not lint */ 45 46 #ifndef lint 47 #if 0 48 static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95"; 49 #endif 50 static char rcsid[] = "$NetBSD: cksum.c,v 1.8 1997/01/30 01:10:34 thorpej Exp $"; 51 #endif /* not lint */ 52 53 #include <sys/cdefs.h> 54 #include <sys/types.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <md5.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "extern.h" 66 67 int md5_digest_file __P((char *)); 68 void requiremd5 __P((const char *)); 69 void usage __P((void)); 70 71 int 72 main(argc, argv) 73 int argc; 74 char **argv; 75 { 76 register int ch, fd, rval, domd5, dosum, pflag, nomd5stdin; 77 u_int32_t len, val; 78 char *fn; 79 int (*cfncn) __P((int, u_int32_t *, u_int32_t *)); 80 void (*pfncn) __P((char *, u_int32_t, u_int32_t)); 81 extern char *__progname; 82 83 dosum = domd5 = pflag = nomd5stdin = 0; 84 85 if (!strcmp(__progname, "md5")) 86 domd5 = 1; 87 else if (!strcmp(__progname, "sum")) { 88 dosum = 1; 89 cfncn = csum1; 90 pfncn = psum1; 91 } else { 92 cfncn = crc; 93 pfncn = pcrc; 94 } 95 96 while ((ch = getopt(argc, argv, "mo:ps:tx")) != -1) 97 switch(ch) { 98 case 'm': 99 if (dosum) { 100 warnx("sum mutually exclusive with md5"); 101 usage(); 102 } 103 domd5 = 1; 104 break; 105 case 'o': 106 if (domd5) { 107 warnx("md5 mutually exclusive with sum"); 108 usage(); 109 } 110 if (!strcmp(optarg, "1")) { 111 cfncn = csum1; 112 pfncn = psum1; 113 } else if (!strcmp(optarg, "2")) { 114 cfncn = csum2; 115 pfncn = psum2; 116 } else { 117 warnx("illegal argument to -o option"); 118 usage(); 119 } 120 break; 121 case 'p': 122 if (!domd5) 123 requiremd5("-p"); 124 pflag = 1; 125 break; 126 case 's': 127 if (!domd5) 128 requiremd5("-s"); 129 nomd5stdin = 1; 130 MDString(optarg); 131 break; 132 case 't': 133 if (!domd5) 134 requiremd5("-t"); 135 MDTimeTrial(); 136 nomd5stdin = 1; 137 break; 138 case 'x': 139 if (!domd5) 140 requiremd5("-x"); 141 MDTestSuite(); 142 nomd5stdin = 1; 143 break; 144 case '?': 145 default: 146 usage(); 147 } 148 argc -= optind; 149 argv += optind; 150 151 fd = STDIN_FILENO; 152 fn = NULL; 153 rval = 0; 154 do { 155 if (*argv) { 156 fn = *argv++; 157 if (domd5) { 158 if (md5_digest_file(fn)) { 159 warn("%s", fn); 160 rval = 1; 161 } 162 continue; 163 } 164 if ((fd = open(fn, O_RDONLY, 0)) < 0) { 165 warn("%s", fn); 166 rval = 1; 167 continue; 168 } 169 } else if (domd5 && !nomd5stdin) 170 MDFilter(pflag); 171 172 if (!domd5) { 173 if (cfncn(fd, &val, &len)) { 174 warn("%s", fn ? fn : "stdin"); 175 rval = 1; 176 } else 177 pfncn(fn, val, len); 178 (void)close(fd); 179 } 180 } while (*argv); 181 exit(rval); 182 } 183 184 int 185 md5_digest_file(fn) 186 char *fn; 187 { 188 char buf[33], *cp; 189 190 cp = MD5File(fn, buf); 191 if (cp == NULL) 192 return (1); 193 194 printf("MD5 (%s) = %s\n", fn, cp); 195 return (0); 196 } 197 198 void 199 requiremd5(flg) 200 const char *flg; 201 { 202 warnx("%s flag requires `md5' or -m", flg); 203 usage(); 204 } 205 206 void 207 usage() 208 { 209 210 (void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n"); 211 (void)fprintf(stderr, " sum [file ...]\n"); 212 (void)fprintf(stderr, 213 " md5 [-p | -t | -x | -s string] [file ...]\n"); 214 exit(1); 215 } 216