xref: /original-bsd/usr.bin/cksum/cksum.c (revision 5c2ace9f)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * James W. Williams of the University of Maryland.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
14  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)cksum.c	5.3 (Berkeley) 04/04/91";
19 #endif /* not lint */
20 
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "extern.h"
30 
31 main(argc, argv)
32 	int argc;
33 	char **argv;
34 {
35 	extern int optind;
36 	u_long len, val;
37 	register int ch, fd, rval;
38 	char *fn;
39 	int (*cfncn) __P((int, unsigned long *, unsigned long *));
40 	void (*pfncn) __P((char *, unsigned long, unsigned long));
41 
42 	cfncn = crc;
43 	pfncn = pcrc;
44 	while ((ch = getopt(argc, argv, "o:")) != EOF)
45 		switch(ch) {
46 		case 'o':
47 			if (*optarg == '1') {
48 				cfncn = csum1;
49 				pfncn = psum1;
50 			} else if (*optarg == '2') {
51 				cfncn = csum2;
52 				pfncn = psum2;
53 			} else {
54 				(void)fprintf(stderr,
55 				    "cksum: illegal argument to -o option\n");
56 				usage();
57 			}
58 			break;
59 		case '?':
60 		default:
61 			usage();
62 		}
63 	argc -= optind;
64 	argv += optind;
65 
66 	fd = STDIN_FILENO;
67 	fn = "stdin";
68 	rval = 0;
69 	do {
70 		if (*argv) {
71 			fn = *argv++;
72 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
73 				(void)fprintf(stderr,
74 				    "cksum: %s: %s\n", fn, strerror(errno));
75 				rval = 1;
76 				continue;
77 			}
78 		}
79 		if (cfncn(fd, &val, &len)) {
80 			(void)fprintf(stderr,
81 			    "cksum: %s: %s\n", fn, strerror(errno));
82 			rval = 1;
83 		} else
84 			pfncn(fn, val, len);
85 		(void)close(fd);
86 	} while (*argv);
87 	exit(rval);
88 }
89 
90 usage()
91 {
92 	(void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
93 	exit(1);
94 }
95