xref: /freebsd/usr.bin/cksum/cksum.c (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * James W. Williams of NASA Goddard Space Flight Center.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "extern.h"
45 
46 static void usage(void) __dead2;
47 
48 int
49 main(int argc, char **argv)
50 {
51 	uint32_t val;
52 	int ch, fd, rval;
53 	off_t len;
54 	char *fn, *p;
55 	int (*cfncn)(int, uint32_t *, off_t *);
56 	void (*pfncn)(char *, uint32_t, off_t);
57 
58 	if ((p = strrchr(argv[0], '/')) == NULL)
59 		p = argv[0];
60 	else
61 		++p;
62 	if (!strcmp(p, "sum")) {
63 		cfncn = csum1;
64 		pfncn = psum1;
65 		++argv;
66 	} else {
67 		cfncn = crc;
68 		pfncn = pcrc;
69 
70 		while ((ch = getopt(argc, argv, "o:")) != -1)
71 			switch (ch) {
72 			case 'o':
73 				if (!strcmp(optarg, "1")) {
74 					cfncn = csum1;
75 					pfncn = psum1;
76 				} else if (!strcmp(optarg, "2")) {
77 					cfncn = csum2;
78 					pfncn = psum2;
79 				} else if (!strcmp(optarg, "3")) {
80 					cfncn = crc32;
81 					pfncn = pcrc;
82 				} else {
83 					warnx("illegal argument to -o option");
84 					usage();
85 				}
86 				break;
87 			case '?':
88 			default:
89 				usage();
90 			}
91 		argc -= optind;
92 		argv += optind;
93 	}
94 
95 	fd = STDIN_FILENO;
96 	fn = NULL;
97 	rval = 0;
98 	do {
99 		if (*argv) {
100 			fn = *argv++;
101 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
102 				warn("%s", fn);
103 				rval = 1;
104 				continue;
105 			}
106 		}
107 		if (cfncn(fd, &val, &len)) {
108 			warn("%s", fn ? fn : "stdin");
109 			rval = 1;
110 		} else
111 			pfncn(fn, val, len);
112 		(void)close(fd);
113 	} while (*argv);
114 	exit(rval);
115 }
116 
117 static void
118 usage(void)
119 {
120 	(void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n");
121 	(void)fprintf(stderr, "       sum [file ...]\n");
122 	exit(1);
123 }
124