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