xref: /csrg-svn/usr.bin/cksum/cksum.c (revision 47801)
147169Sbostic /*-
247169Sbostic  * Copyright (c) 1991 The Regents of the University of California.
347169Sbostic  * All rights reserved.
447169Sbostic  *
547169Sbostic  * This code is derived from software contributed to Berkeley by
647169Sbostic  * James W. Williams of the University of Maryland.
747169Sbostic  *
847169Sbostic  * %sccs.include.redist.c%
947169Sbostic  */
1047169Sbostic 
1147169Sbostic #ifndef lint
1247169Sbostic char copyright[] =
1347169Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
1447169Sbostic  All rights reserved.\n";
1547169Sbostic #endif /* not lint */
1647169Sbostic 
1747169Sbostic #ifndef lint
18*47801Sbostic static char sccsid[] = "@(#)cksum.c	5.3 (Berkeley) 04/04/91";
1947169Sbostic #endif /* not lint */
2047169Sbostic 
21*47801Sbostic #include <sys/cdefs.h>
2247169Sbostic #include <sys/types.h>
2347169Sbostic #include <fcntl.h>
2447721Sbostic #include <unistd.h>
2547169Sbostic #include <stdio.h>
2647721Sbostic #include <errno.h>
2747169Sbostic #include <stdlib.h>
2847169Sbostic #include <string.h>
29*47801Sbostic #include "extern.h"
3047169Sbostic 
3147169Sbostic main(argc, argv)
3247169Sbostic 	int argc;
3347169Sbostic 	char **argv;
3447169Sbostic {
3547169Sbostic 	extern int optind;
3647721Sbostic 	u_long len, val;
3747169Sbostic 	register int ch, fd, rval;
3847169Sbostic 	char *fn;
39*47801Sbostic 	int (*cfncn) __P((int, unsigned long *, unsigned long *));
40*47801Sbostic 	void (*pfncn) __P((char *, unsigned long, unsigned long));
4147169Sbostic 
42*47801Sbostic 	cfncn = crc;
43*47801Sbostic 	pfncn = pcrc;
44*47801Sbostic 	while ((ch = getopt(argc, argv, "o:")) != EOF)
4547169Sbostic 		switch(ch) {
46*47801Sbostic 		case 'o':
47*47801Sbostic 			if (*optarg == '1') {
48*47801Sbostic 				cfncn = csum1;
49*47801Sbostic 				pfncn = psum1;
50*47801Sbostic 			} else if (*optarg == '2') {
51*47801Sbostic 				cfncn = csum2;
52*47801Sbostic 				pfncn = psum2;
53*47801Sbostic 			} else {
54*47801Sbostic 				(void)fprintf(stderr,
55*47801Sbostic 				    "cksum: illegal argument to -o option\n");
56*47801Sbostic 				usage();
57*47801Sbostic 			}
58*47801Sbostic 			break;
5947169Sbostic 		case '?':
6047169Sbostic 		default:
6147169Sbostic 			usage();
6247169Sbostic 		}
6347169Sbostic 	argc -= optind;
6447169Sbostic 	argv += optind;
6547169Sbostic 
6647169Sbostic 	fd = STDIN_FILENO;
6747169Sbostic 	fn = "stdin";
6847169Sbostic 	rval = 0;
6947169Sbostic 	do {
7047169Sbostic 		if (*argv) {
7147169Sbostic 			fn = *argv++;
7247169Sbostic 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
7347169Sbostic 				(void)fprintf(stderr,
7447169Sbostic 				    "cksum: %s: %s\n", fn, strerror(errno));
7547169Sbostic 				rval = 1;
7647169Sbostic 				continue;
7747169Sbostic 			}
7847169Sbostic 		}
79*47801Sbostic 		if (cfncn(fd, &val, &len)) {
8047721Sbostic 			(void)fprintf(stderr,
8147721Sbostic 			    "cksum: %s: %s\n", fn, strerror(errno));
8247721Sbostic 			rval = 1;
8347721Sbostic 		} else
84*47801Sbostic 			pfncn(fn, val, len);
8547169Sbostic 		(void)close(fd);
8647169Sbostic 	} while (*argv);
8747169Sbostic 	exit(rval);
8847169Sbostic }
8947169Sbostic 
9047169Sbostic usage()
9147169Sbostic {
92*47801Sbostic 	(void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
9347169Sbostic 	exit(1);
9447169Sbostic }
95