cksum.c revision 1.9 1 /* $NetBSD: cksum.c,v 1.9 1997/06/26 23:24:01 kleink 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.9 1997/06/26 23:24:01 kleink 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 <locale.h>
60 #include <md5.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "extern.h"
67
68 int md5_digest_file __P((char *));
69 void requiremd5 __P((const char *));
70 void usage __P((void));
71
72 int
73 main(argc, argv)
74 int argc;
75 char **argv;
76 {
77 register int ch, fd, rval, domd5, dosum, pflag, nomd5stdin;
78 u_int32_t len, val;
79 char *fn;
80 int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
81 void (*pfncn) __P((char *, u_int32_t, u_int32_t));
82 extern char *__progname;
83
84 dosum = domd5 = pflag = nomd5stdin = 0;
85
86 setlocale(LC_ALL, "");
87
88 if (!strcmp(__progname, "md5"))
89 domd5 = 1;
90 else if (!strcmp(__progname, "sum")) {
91 dosum = 1;
92 cfncn = csum1;
93 pfncn = psum1;
94 } else {
95 cfncn = crc;
96 pfncn = pcrc;
97 }
98
99 while ((ch = getopt(argc, argv, "mo:ps:tx")) != -1)
100 switch(ch) {
101 case 'm':
102 if (dosum) {
103 warnx("sum mutually exclusive with md5");
104 usage();
105 }
106 domd5 = 1;
107 break;
108 case 'o':
109 if (domd5) {
110 warnx("md5 mutually exclusive with sum");
111 usage();
112 }
113 if (!strcmp(optarg, "1")) {
114 cfncn = csum1;
115 pfncn = psum1;
116 } else if (!strcmp(optarg, "2")) {
117 cfncn = csum2;
118 pfncn = psum2;
119 } else {
120 warnx("illegal argument to -o option");
121 usage();
122 }
123 break;
124 case 'p':
125 if (!domd5)
126 requiremd5("-p");
127 pflag = 1;
128 break;
129 case 's':
130 if (!domd5)
131 requiremd5("-s");
132 nomd5stdin = 1;
133 MDString(optarg);
134 break;
135 case 't':
136 if (!domd5)
137 requiremd5("-t");
138 MDTimeTrial();
139 nomd5stdin = 1;
140 break;
141 case 'x':
142 if (!domd5)
143 requiremd5("-x");
144 MDTestSuite();
145 nomd5stdin = 1;
146 break;
147 case '?':
148 default:
149 usage();
150 }
151 argc -= optind;
152 argv += optind;
153
154 fd = STDIN_FILENO;
155 fn = NULL;
156 rval = 0;
157 do {
158 if (*argv) {
159 fn = *argv++;
160 if (domd5) {
161 if (md5_digest_file(fn)) {
162 warn("%s", fn);
163 rval = 1;
164 }
165 continue;
166 }
167 if ((fd = open(fn, O_RDONLY, 0)) < 0) {
168 warn("%s", fn);
169 rval = 1;
170 continue;
171 }
172 } else if (domd5 && !nomd5stdin)
173 MDFilter(pflag);
174
175 if (!domd5) {
176 if (cfncn(fd, &val, &len)) {
177 warn("%s", fn ? fn : "stdin");
178 rval = 1;
179 } else
180 pfncn(fn, val, len);
181 (void)close(fd);
182 }
183 } while (*argv);
184 exit(rval);
185 }
186
187 int
188 md5_digest_file(fn)
189 char *fn;
190 {
191 char buf[33], *cp;
192
193 cp = MD5File(fn, buf);
194 if (cp == NULL)
195 return (1);
196
197 printf("MD5 (%s) = %s\n", fn, cp);
198 return (0);
199 }
200
201 void
202 requiremd5(flg)
203 const char *flg;
204 {
205 warnx("%s flag requires `md5' or -m", flg);
206 usage();
207 }
208
209 void
210 usage()
211 {
212
213 (void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
214 (void)fprintf(stderr, " sum [file ...]\n");
215 (void)fprintf(stderr,
216 " md5 [-p | -t | -x | -s string] [file ...]\n");
217 exit(1);
218 }
219