Annotation of src/usr.bin/cksum/cksum.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: cksum.c,v 1.9 2002/12/13 15:28:34 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.10 ! millert 46: static char rcsid[] = "$OpenBSD: cksum.c,v 1.9 2002/12/13 15:28:34 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
67: main(argc, argv)
68: int argc;
69: char **argv;
70: {
1.7 mpech 71: int ch, fd, rval;
1.1 deraadt 72: u_int32_t len, val;
73: char *fn;
1.8 millert 74: int (*cfncn)(int, u_int32_t *, u_int32_t *);
75: void (*pfncn)(char *, u_int32_t, u_int32_t);
1.3 deraadt 76:
77: setlocale(LC_ALL, "");
1.1 deraadt 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: {
1.6 aaron 133: if (!strcmp(__progname, "cksum"))
134: (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
135: else
136: (void)fprintf(stderr, "usage: %s [file ...]\n", __progname);
1.1 deraadt 137: exit(1);
138: }