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