cksum.c revision 31054
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1991, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * James W. Williams of NASA Goddard Space Flight Center.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3827223Sbdestatic const char copyright[] =
391590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
401590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411590Srgrimes#endif /* not lint */
421590Srgrimes
431590Srgrimes#ifndef lint
4427223Sbde#if 0
4527222Sbdestatic char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
4627223Sbde#endif
4727223Sbdestatic const char rcsid[] =
4831054Sobrien	"$Id: cksum.c,v 1.6 1997/09/26 08:02:17 phk Exp $";
491590Srgrimes#endif /* not lint */
501590Srgrimes
511590Srgrimes#include <sys/cdefs.h>
521590Srgrimes#include <sys/types.h>
5327222Sbde
5426922Scharnier#include <err.h>
5526922Scharnier#include <errno.h>
561590Srgrimes#include <fcntl.h>
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <stdlib.h>
591590Srgrimes#include <string.h>
6026922Scharnier#include <unistd.h>
6127222Sbde
621590Srgrimes#include "extern.h"
631590Srgrimes
6426922Scharnierstatic void usage __P((void));
651590Srgrimes
661590Srgrimesint
671590Srgrimesmain(argc, argv)
681590Srgrimes	int argc;
691590Srgrimes	char **argv;
701590Srgrimes{
7127222Sbde	register int ch, fd, rval;
721590Srgrimes	u_long len, val;
7327222Sbde	char *fn, *p;
741590Srgrimes	int (*cfncn) __P((int, unsigned long *, unsigned long *));
751590Srgrimes	void (*pfncn) __P((char *, unsigned long, unsigned long));
761590Srgrimes
7727222Sbde	if ((p = rindex(argv[0], '/')) == NULL)
7827222Sbde		p = argv[0];
7927222Sbde	else
8027222Sbde		++p;
8127223Sbde	if (!strcmp(p, "sum")) {
8227223Sbde		cfncn = csum1;
8327223Sbde		pfncn = psum1;
8427223Sbde	} else {
8527222Sbde		cfncn = crc;
8627222Sbde		pfncn = pcrc;
8727222Sbde
8831054Sobrien		while ((ch = getopt(argc, argv, "o:")) != -1)
8931054Sobrien			switch (ch) {
9031054Sobrien			case 'o':
9131054Sobrien				if (!strcmp(optarg, "1")) {
9231054Sobrien					cfncn = csum1;
9331054Sobrien					pfncn = psum1;
9431054Sobrien				} else if (!strcmp(optarg, "2")) {
9531054Sobrien					cfncn = csum2;
9631054Sobrien					pfncn = psum2;
9731054Sobrien				} else if (*optarg == '3') {
9831054Sobrien					cfncn = crc32;
9931054Sobrien					pfncn = pcrc;
10031054Sobrien				} else {
10131054Sobrien					warnx("illegal argument to -o option");
10231054Sobrien					usage();
10331054Sobrien				}
10431054Sobrien				break;
10531054Sobrien			case '?':
10631054Sobrien			default:
1071590Srgrimes				usage();
1081590Srgrimes			}
10931054Sobrien		argc -= optind;
11031054Sobrien		argv += optind;
11131054Sobrien	}
1121590Srgrimes
1131590Srgrimes	fd = STDIN_FILENO;
1141590Srgrimes	fn = NULL;
1151590Srgrimes	rval = 0;
1161590Srgrimes	do {
1171590Srgrimes		if (*argv) {
1181590Srgrimes			fn = *argv++;
1191590Srgrimes			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
12026922Scharnier				warn("%s", fn);
1211590Srgrimes				rval = 1;
1221590Srgrimes				continue;
1231590Srgrimes			}
1241590Srgrimes		}
1251590Srgrimes		if (cfncn(fd, &val, &len)) {
12626922Scharnier			warn("%s", fn ? fn : "stdin");
1271590Srgrimes			rval = 1;
1281590Srgrimes		} else
1291590Srgrimes			pfncn(fn, val, len);
1301590Srgrimes		(void)close(fd);
1311590Srgrimes	} while (*argv);
1321590Srgrimes	exit(rval);
1331590Srgrimes}
1341590Srgrimes
13526922Scharnierstatic void
1361590Srgrimesusage()
1371590Srgrimes{
1381590Srgrimes	(void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
13927223Sbde	(void)fprintf(stderr, "       sum [file ...]\n");
1401590Srgrimes	exit(1);
1411590Srgrimes}
142