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