xref: /csrg-svn/usr.bin/cksum/cksum.c (revision 51819)
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
651066Sbostic  * James W. Williams of NASA Goddard Space Flight Center.
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*51819Sbostic static char sccsid[] = "@(#)cksum.c	5.5 (Berkeley) 11/27/91";
1947169Sbostic #endif /* not lint */
2047169Sbostic 
2147801Sbostic #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>
2947801Sbostic #include "extern.h"
3047169Sbostic 
31*51819Sbostic void usage __P((void));
32*51819Sbostic 
33*51819Sbostic int
3447169Sbostic main(argc, argv)
3547169Sbostic 	int argc;
3647169Sbostic 	char **argv;
3747169Sbostic {
3847169Sbostic 	extern int optind;
3947721Sbostic 	u_long len, val;
4047169Sbostic 	register int ch, fd, rval;
4147169Sbostic 	char *fn;
4247801Sbostic 	int (*cfncn) __P((int, unsigned long *, unsigned long *));
4347801Sbostic 	void (*pfncn) __P((char *, unsigned long, unsigned long));
4447169Sbostic 
4547801Sbostic 	cfncn = crc;
4647801Sbostic 	pfncn = pcrc;
4747801Sbostic 	while ((ch = getopt(argc, argv, "o:")) != EOF)
4847169Sbostic 		switch(ch) {
4947801Sbostic 		case 'o':
5047801Sbostic 			if (*optarg == '1') {
5147801Sbostic 				cfncn = csum1;
5247801Sbostic 				pfncn = psum1;
5347801Sbostic 			} else if (*optarg == '2') {
5447801Sbostic 				cfncn = csum2;
5547801Sbostic 				pfncn = psum2;
5647801Sbostic 			} else {
5747801Sbostic 				(void)fprintf(stderr,
5847801Sbostic 				    "cksum: illegal argument to -o option\n");
5947801Sbostic 				usage();
6047801Sbostic 			}
6147801Sbostic 			break;
6247169Sbostic 		case '?':
6347169Sbostic 		default:
6447169Sbostic 			usage();
6547169Sbostic 		}
6647169Sbostic 	argc -= optind;
6747169Sbostic 	argv += optind;
6847169Sbostic 
6947169Sbostic 	fd = STDIN_FILENO;
7047169Sbostic 	fn = "stdin";
7147169Sbostic 	rval = 0;
7247169Sbostic 	do {
7347169Sbostic 		if (*argv) {
7447169Sbostic 			fn = *argv++;
7547169Sbostic 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
7647169Sbostic 				(void)fprintf(stderr,
7747169Sbostic 				    "cksum: %s: %s\n", fn, strerror(errno));
7847169Sbostic 				rval = 1;
7947169Sbostic 				continue;
8047169Sbostic 			}
8147169Sbostic 		}
8247801Sbostic 		if (cfncn(fd, &val, &len)) {
8347721Sbostic 			(void)fprintf(stderr,
8447721Sbostic 			    "cksum: %s: %s\n", fn, strerror(errno));
8547721Sbostic 			rval = 1;
8647721Sbostic 		} else
8747801Sbostic 			pfncn(fn, val, len);
8847169Sbostic 		(void)close(fd);
8947169Sbostic 	} while (*argv);
9047169Sbostic 	exit(rval);
9147169Sbostic }
9247169Sbostic 
93*51819Sbostic void
9447169Sbostic usage()
9547169Sbostic {
9647801Sbostic 	(void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
9747169Sbostic 	exit(1);
9847169Sbostic }
99