xref: /csrg-svn/usr.bin/cksum/cksum.c (revision 47721)
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*47721Sbostic static char sccsid[] = "@(#)cksum.c	5.2 (Berkeley) 04/01/91";
1947169Sbostic #endif /* not lint */
2047169Sbostic 
2147169Sbostic #include <sys/types.h>
2247169Sbostic #include <fcntl.h>
23*47721Sbostic #include <unistd.h>
2447169Sbostic #include <stdio.h>
25*47721Sbostic #include <errno.h>
2647169Sbostic #include <stdlib.h>
2747169Sbostic #include <string.h>
2847169Sbostic 
2947169Sbostic main(argc, argv)
3047169Sbostic 	int argc;
3147169Sbostic 	char **argv;
3247169Sbostic {
3347169Sbostic 	extern int optind;
34*47721Sbostic 	u_long len, val;
3547169Sbostic 	register int ch, fd, rval;
3647169Sbostic 	char *fn;
3747169Sbostic 
3847169Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3947169Sbostic 		switch(ch) {
4047169Sbostic 		case '?':
4147169Sbostic 		default:
4247169Sbostic 			usage();
4347169Sbostic 		}
4447169Sbostic 	argc -= optind;
4547169Sbostic 	argv += optind;
4647169Sbostic 
4747169Sbostic 	fd = STDIN_FILENO;
4847169Sbostic 	fn = "stdin";
4947169Sbostic 	rval = 0;
5047169Sbostic 	do {
5147169Sbostic 		if (*argv) {
5247169Sbostic 			fn = *argv++;
5347169Sbostic 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
5447169Sbostic 				(void)fprintf(stderr,
5547169Sbostic 				    "cksum: %s: %s\n", fn, strerror(errno));
5647169Sbostic 				rval = 1;
5747169Sbostic 				continue;
5847169Sbostic 			}
5947169Sbostic 		}
60*47721Sbostic 		if (crc(fd, &val, &len)) {
61*47721Sbostic 			(void)fprintf(stderr,
62*47721Sbostic 			    "cksum: %s: %s\n", fn, strerror(errno));
63*47721Sbostic 			rval = 1;
64*47721Sbostic 		} else
65*47721Sbostic 			(void)printf("%lu %lu %s\n", val, len, fn);
6647169Sbostic 		(void)close(fd);
6747169Sbostic 	} while (*argv);
6847169Sbostic 	exit(rval);
6947169Sbostic }
7047169Sbostic 
7147169Sbostic usage()
7247169Sbostic {
7347169Sbostic 	(void)fprintf(stderr, "usage: cksum [file ...]\n");
7447169Sbostic 	exit(1);
7547169Sbostic }
76