cksum.c revision 1.7 1 /* $NetBSD: cksum.c,v 1.7 1995/09/02 05:45:18 jtc Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * James W. Williams of NASA Goddard Space Flight Center.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1991, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95";
48 #endif
49 static char rcsid[] = "$NetBSD: cksum.c,v 1.7 1995/09/02 05:45:18 jtc Exp $";
50 #endif /* not lint */
51
52 #include <sys/cdefs.h>
53 #include <sys/types.h>
54
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "extern.h"
64
65 void usage __P((void));
66
67 int
68 main(argc, argv)
69 int argc;
70 char **argv;
71 {
72 register int ch, fd, rval;
73 u_int32_t len, val;
74 char *fn;
75 int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
76 void (*pfncn) __P((char *, u_int32_t, u_int32_t));
77 extern char *__progname;
78
79 if (!strcmp(__progname, "sum")) {
80 cfncn = csum1;
81 pfncn = psum1;
82 } else {
83 cfncn = crc;
84 pfncn = pcrc;
85 }
86
87 while ((ch = getopt(argc, argv, "o:")) != -1)
88 switch(ch) {
89 case 'o':
90 if (!strcmp(optarg, "1")) {
91 cfncn = csum1;
92 pfncn = psum1;
93 } else if (!strcmp(optarg, "2")) {
94 cfncn = csum2;
95 pfncn = psum2;
96 } else {
97 warnx("illegal argument to -o option");
98 usage();
99 }
100 break;
101 case '?':
102 default:
103 usage();
104 }
105 argc -= optind;
106 argv += optind;
107
108 fd = STDIN_FILENO;
109 fn = NULL;
110 rval = 0;
111 do {
112 if (*argv) {
113 fn = *argv++;
114 if ((fd = open(fn, O_RDONLY, 0)) < 0) {
115 warn("%s", fn);
116 rval = 1;
117 continue;
118 }
119 }
120 if (cfncn(fd, &val, &len)) {
121 warn("%s", fn ? fn : "stdin");
122 rval = 1;
123 } else
124 pfncn(fn, val, len);
125 (void)close(fd);
126 } while (*argv);
127 exit(rval);
128 }
129
130 void
131 usage()
132 {
133
134 (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
135 exit(1);
136 }
137