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