147169Sbostic /*-
261940Sbostic * Copyright (c) 1991, 1993
361940Sbostic * The Regents of the University of California. 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
1261940Sbostic static char copyright[] =
1361940Sbostic "@(#) Copyright (c) 1991, 1993\n\
1461940Sbostic The Regents of the University of California. All rights reserved.\n";
1547169Sbostic #endif /* not lint */
1647169Sbostic
1747169Sbostic #ifndef lint
18*69082Sbostic static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 04/28/95";
1947169Sbostic #endif /* not lint */
2047169Sbostic
2147801Sbostic #include <sys/cdefs.h>
2247169Sbostic #include <sys/types.h>
23*69082Sbostic
24*69082Sbostic #include <err.h>
25*69082Sbostic #include <errno.h>
2647169Sbostic #include <fcntl.h>
2747169Sbostic #include <stdio.h>
2847169Sbostic #include <stdlib.h>
2947169Sbostic #include <string.h>
30*69082Sbostic #include <unistd.h>
31*69082Sbostic
3247801Sbostic #include "extern.h"
3347169Sbostic
3451819Sbostic void usage __P((void));
3551819Sbostic
3651819Sbostic int
main(argc,argv)3747169Sbostic main(argc, argv)
3847169Sbostic int argc;
3947169Sbostic char **argv;
4047169Sbostic {
41*69082Sbostic register int ch, fd, rval;
4247721Sbostic u_long len, val;
43*69082Sbostic char *fn, *p;
4447801Sbostic int (*cfncn) __P((int, unsigned long *, unsigned long *));
4547801Sbostic void (*pfncn) __P((char *, unsigned long, unsigned long));
4647169Sbostic
47*69082Sbostic if ((p = rindex(argv[0], '/')) == NULL)
48*69082Sbostic p = argv[0];
49*69082Sbostic else
50*69082Sbostic ++p;
51*69082Sbostic if (*p == 'c') {
52*69082Sbostic cfncn = crc;
53*69082Sbostic pfncn = pcrc;
54*69082Sbostic } else {
55*69082Sbostic cfncn = csum1;
56*69082Sbostic pfncn = psum1;
57*69082Sbostic }
58*69082Sbostic
59*69082Sbostic while ((ch = getopt(argc, argv, "o:")) != -1)
60*69082Sbostic switch (ch) {
6147801Sbostic case 'o':
62*69082Sbostic if (!strcmp(optarg, "1")) {
6347801Sbostic cfncn = csum1;
6447801Sbostic pfncn = psum1;
65*69082Sbostic } else if (!strcmp(optarg, "2")) {
6647801Sbostic cfncn = csum2;
6747801Sbostic pfncn = psum2;
6847801Sbostic } else {
69*69082Sbostic warnx("illegal argument to -o option");
7047801Sbostic usage();
7147801Sbostic }
7247801Sbostic break;
7347169Sbostic case '?':
7447169Sbostic default:
7547169Sbostic usage();
7647169Sbostic }
7747169Sbostic argc -= optind;
7847169Sbostic argv += optind;
7947169Sbostic
8047169Sbostic fd = STDIN_FILENO;
8151820Sbostic fn = NULL;
8247169Sbostic rval = 0;
8347169Sbostic do {
8447169Sbostic if (*argv) {
8547169Sbostic fn = *argv++;
8647169Sbostic if ((fd = open(fn, O_RDONLY, 0)) < 0) {
87*69082Sbostic warn("%s", fn);
8847169Sbostic rval = 1;
8947169Sbostic continue;
9047169Sbostic }
9147169Sbostic }
9247801Sbostic if (cfncn(fd, &val, &len)) {
93*69082Sbostic warn("%s", fn ? fn : "stdin");
9447721Sbostic rval = 1;
9547721Sbostic } else
9647801Sbostic pfncn(fn, val, len);
9747169Sbostic (void)close(fd);
9847169Sbostic } while (*argv);
9947169Sbostic exit(rval);
10047169Sbostic }
10147169Sbostic
10251819Sbostic void
usage()10347169Sbostic usage()
10447169Sbostic {
10547801Sbostic (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
10647169Sbostic exit(1);
10747169Sbostic }
108