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