Annotation of src/usr.bin/cksum/cksum.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: cksum.c,v 1.10 2003/06/03 02:56:06 millert 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.
1.10 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: static char copyright[] =
38: "@(#) Copyright (c) 1991, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: #if 0
44: static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95";
45: #endif
1.11 ! deraadt 46: static char rcsid[] = "$OpenBSD: cksum.c,v 1.10 2003/06/03 02:56:06 millert Exp $";
1.1 deraadt 47: #endif /* not lint */
48:
49: #include <sys/types.h>
50:
51: #include <err.h>
52: #include <errno.h>
53: #include <fcntl.h>
54: #include <stdio.h>
55: #include <stdlib.h>
56: #include <string.h>
57: #include <unistd.h>
1.4 grr 58: #include <locale.h>
1.1 deraadt 59:
60: #include "extern.h"
61:
1.8 millert 62: void usage(void);
1.1 deraadt 63:
1.5 aaron 64: extern char *__progname;
65:
1.1 deraadt 66: int
1.11 ! deraadt 67: main(int argc, char *argv[])
1.1 deraadt 68: {
1.7 mpech 69: int ch, fd, rval;
1.1 deraadt 70: u_int32_t len, val;
71: char *fn;
1.8 millert 72: int (*cfncn)(int, u_int32_t *, u_int32_t *);
73: void (*pfncn)(char *, u_int32_t, u_int32_t);
1.3 deraadt 74:
75: setlocale(LC_ALL, "");
1.1 deraadt 76:
77: if (!strcmp(__progname, "sum")) {
78: cfncn = csum1;
79: pfncn = psum1;
80: } else {
81: cfncn = crc;
82: pfncn = pcrc;
83: }
84:
85: while ((ch = getopt(argc, argv, "o:")) != -1)
86: switch(ch) {
87: case 'o':
88: if (!strcmp(optarg, "1")) {
89: cfncn = csum1;
90: pfncn = psum1;
91: } else if (!strcmp(optarg, "2")) {
92: cfncn = csum2;
93: pfncn = psum2;
94: } else {
95: warnx("illegal argument to -o option");
96: usage();
97: }
98: break;
99: case '?':
100: default:
101: usage();
102: }
103: argc -= optind;
104: argv += optind;
105:
106: fd = STDIN_FILENO;
107: fn = NULL;
108: rval = 0;
109: do {
110: if (*argv) {
111: fn = *argv++;
112: if ((fd = open(fn, O_RDONLY, 0)) < 0) {
113: warn("%s", fn);
114: rval = 1;
115: continue;
116: }
117: }
118: if (cfncn(fd, &val, &len)) {
119: warn("%s", fn ? fn : "stdin");
120: rval = 1;
121: } else
122: pfncn(fn, val, len);
123: (void)close(fd);
124: } while (*argv);
125: exit(rval);
126: }
127:
128: void
1.11 ! deraadt 129: usage(void)
1.1 deraadt 130: {
1.6 aaron 131: if (!strcmp(__progname, "cksum"))
132: (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
133: else
134: (void)fprintf(stderr, "usage: %s [file ...]\n", __progname);
1.1 deraadt 135: exit(1);
136: }