Annotation of src/usr.bin/cksum/cksum.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: cksum.c,v 1.2 1996/06/26 05:32:01 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: cksum.c,v 1.7 1995/09/02 05:45:18 jtc Exp $ */
3:
4: /*-
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
1.3 ! deraadt 50: static char rcsid[] = "$OpenBSD: cksum.c,v 1.2 1996/06/26 05:32:01 deraadt Exp $";
1.1 deraadt 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 <stdio.h>
60: #include <stdlib.h>
61: #include <string.h>
62: #include <unistd.h>
63:
64: #include "extern.h"
65:
66: void usage __P((void));
67:
68: int
69: main(argc, argv)
70: int argc;
71: char **argv;
72: {
73: register int ch, fd, rval;
74: u_int32_t len, val;
75: char *fn;
76: int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
77: void (*pfncn) __P((char *, u_int32_t, u_int32_t));
78: extern char *__progname;
1.3 ! deraadt 79:
! 80: setlocale(LC_ALL, "");
1.1 deraadt 81:
82: if (!strcmp(__progname, "sum")) {
83: cfncn = csum1;
84: pfncn = psum1;
85: } else {
86: cfncn = crc;
87: pfncn = pcrc;
88: }
89:
90: while ((ch = getopt(argc, argv, "o:")) != -1)
91: switch(ch) {
92: case 'o':
93: if (!strcmp(optarg, "1")) {
94: cfncn = csum1;
95: pfncn = psum1;
96: } else if (!strcmp(optarg, "2")) {
97: cfncn = csum2;
98: pfncn = psum2;
99: } else {
100: warnx("illegal argument to -o option");
101: usage();
102: }
103: break;
104: case '?':
105: default:
106: usage();
107: }
108: argc -= optind;
109: argv += optind;
110:
111: fd = STDIN_FILENO;
112: fn = NULL;
113: rval = 0;
114: do {
115: if (*argv) {
116: fn = *argv++;
117: if ((fd = open(fn, O_RDONLY, 0)) < 0) {
118: warn("%s", fn);
119: rval = 1;
120: continue;
121: }
122: }
123: if (cfncn(fd, &val, &len)) {
124: warn("%s", fn ? fn : "stdin");
125: rval = 1;
126: } else
127: pfncn(fn, val, len);
128: (void)close(fd);
129: } while (*argv);
130: exit(rval);
131: }
132:
133: void
134: usage()
135: {
136:
137: (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
138: exit(1);
139: }