cksum.c revision 92920
1116742Ssam/*-
2116904Ssam * Copyright (c) 1991, 1993
3116742Ssam *	The Regents of the University of California.  All rights reserved.
4116742Ssam *
5116742Ssam * This code is derived from software contributed to Berkeley by
6116742Ssam * James W. Williams of NASA Goddard Space Flight Center.
7116742Ssam *
8116742Ssam * Redistribution and use in source and binary forms, with or without
9116742Ssam * modification, are permitted provided that the following conditions
10116742Ssam * are met:
11116742Ssam * 1. Redistributions of source code must retain the above copyright
12116742Ssam *    notice, this list of conditions and the following disclaimer.
13116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
14116904Ssam *    notice, this list of conditions and the following disclaimer in the
15116904Ssam *    documentation and/or other materials provided with the distribution.
16116742Ssam * 3. All advertising materials mentioning features or use of this software
17116904Ssam *    must display the following acknowledgement:
18116904Ssam *	This product includes software developed by the University of
19116904Ssam *	California, Berkeley and its contributors.
20116742Ssam * 4. Neither the name of the University nor the names of its contributors
21116904Ssam *    may be used to endorse or promote products derived from this software
22116904Ssam *    without specific prior written permission.
23116904Ssam *
24116904Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25116904Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26116904Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27116904Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28116904Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29116904Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30116904Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31116904Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32116742Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33116742Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34116742Ssam * SUCH DAMAGE.
35116742Ssam */
36116742Ssam
37116742Ssam#ifndef lint
38116742Ssamstatic const char copyright[] =
39116742Ssam"@(#) Copyright (c) 1991, 1993\n\
40116742Ssam	The Regents of the University of California.  All rights reserved.\n";
41116742Ssam#endif /* not lint */
42116742Ssam
43116742Ssam#ifndef lint
44116742Ssam#if 0
45116742Ssamstatic char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
46116742Ssam#endif
47116742Ssamstatic const char rcsid[] =
48116742Ssam  "$FreeBSD: head/usr.bin/cksum/cksum.c 92920 2002-03-22 01:22:50Z imp $";
49116742Ssam#endif /* not lint */
50116742Ssam
51116742Ssam#include <sys/types.h>
52116742Ssam
53116742Ssam#include <err.h>
54116742Ssam#include <fcntl.h>
55116742Ssam#include <stdio.h>
56116742Ssam#include <stdlib.h>
57116742Ssam#include <string.h>
58116742Ssam#include <unistd.h>
59116742Ssam
60116742Ssam#include "extern.h"
61116742Ssam
62116742Ssamstatic void usage(void);
63116742Ssam
64116742Ssamint
65116742Ssammain(argc, argv)
66116742Ssam	int argc;
67116742Ssam	char **argv;
68116742Ssam{
69116742Ssam	int ch, fd, rval;
70116742Ssam	u_int32_t len, val;
71116742Ssam	char *fn, *p;
72116742Ssam	int (*cfncn)(int, u_int32_t *, u_int32_t *);
73117817Ssam	void (*pfncn)(char *, u_int32_t, u_int32_t);
74117817Ssam
75116742Ssam	if ((p = rindex(argv[0], '/')) == NULL)
76116742Ssam		p = argv[0];
77116742Ssam	else
78116742Ssam		++p;
79116742Ssam	if (!strcmp(p, "sum")) {
80116742Ssam		cfncn = csum1;
81116742Ssam		pfncn = psum1;
82116742Ssam		++argv;
83116742Ssam	} else {
84116742Ssam		cfncn = crc;
85116742Ssam		pfncn = pcrc;
86116742Ssam
87116742Ssam		while ((ch = getopt(argc, argv, "o:")) != -1)
88116742Ssam			switch (ch) {
89116742Ssam			case 'o':
90116742Ssam				if (!strcmp(optarg, "1")) {
91116742Ssam					cfncn = csum1;
92116742Ssam					pfncn = psum1;
93116742Ssam				} else if (!strcmp(optarg, "2")) {
94116742Ssam					cfncn = csum2;
95116742Ssam					pfncn = psum2;
96116742Ssam				} else if (!strcmp(optarg, "3")) {
97116742Ssam					cfncn = crc32;
98116742Ssam					pfncn = pcrc;
99116742Ssam				} else {
100116742Ssam					warnx("illegal argument to -o option");
101116742Ssam					usage();
102116742Ssam				}
103116742Ssam				break;
104116742Ssam			case '?':
105116742Ssam			default:
106116742Ssam				usage();
107116742Ssam			}
108116742Ssam		argc -= optind;
109116742Ssam		argv += optind;
110116742Ssam	}
111116742Ssam
112116742Ssam	fd = STDIN_FILENO;
113116742Ssam	fn = NULL;
114116742Ssam	rval = 0;
115116742Ssam	do {
116116742Ssam		if (*argv) {
117116742Ssam			fn = *argv++;
118116742Ssam			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
119116742Ssam				warn("%s", fn);
120116742Ssam				rval = 1;
121116742Ssam				continue;
122116742Ssam			}
123116742Ssam		}
124116742Ssam		if (cfncn(fd, &val, &len)) {
125116742Ssam			warn("%s", fn ? fn : "stdin");
126116742Ssam			rval = 1;
127116742Ssam		} else
128116742Ssam			pfncn(fn, val, len);
129116742Ssam		(void)close(fd);
130116742Ssam	} while (*argv);
131116742Ssam	exit(rval);
132116742Ssam}
133116742Ssam
134116742Ssamstatic void
135116742Ssamusage()
136116742Ssam{
137116742Ssam	(void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n");
138119150Ssam	(void)fprintf(stderr, "       sum [file ...]\n");
139119150Ssam	exit(1);
140116742Ssam}
141116742Ssam