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