cksum.c revision 26922
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.
3526922Scharnier *
3626922Scharnier *	$Id$
371590Srgrimes */
381590Srgrimes
391590Srgrimes#ifndef lint
401590Srgrimesstatic char copyright[] =
411590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
421590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
431590Srgrimes#endif /* not lint */
441590Srgrimes
451590Srgrimes#ifndef lint
461590Srgrimesstatic char sccsid[] = "@(#)cksum.c	8.1 (Berkeley) 6/6/93";
471590Srgrimes#endif /* not lint */
481590Srgrimes
491590Srgrimes#include <sys/cdefs.h>
501590Srgrimes#include <sys/types.h>
5126922Scharnier#include <err.h>
5226922Scharnier#include <errno.h>
531590Srgrimes#include <fcntl.h>
541590Srgrimes#include <stdio.h>
551590Srgrimes#include <stdlib.h>
561590Srgrimes#include <string.h>
5726922Scharnier#include <unistd.h>
581590Srgrimes#include "extern.h"
591590Srgrimes
6026922Scharnierstatic void usage __P((void));
611590Srgrimes
621590Srgrimesint
631590Srgrimesmain(argc, argv)
641590Srgrimes	int argc;
651590Srgrimes	char **argv;
661590Srgrimes{
671590Srgrimes	extern int optind;
681590Srgrimes	u_long len, val;
691590Srgrimes	register int ch, fd, rval;
701590Srgrimes	char *fn;
711590Srgrimes	int (*cfncn) __P((int, unsigned long *, unsigned long *));
721590Srgrimes	void (*pfncn) __P((char *, unsigned long, unsigned long));
731590Srgrimes
741590Srgrimes	cfncn = crc;
751590Srgrimes	pfncn = pcrc;
7624360Simp	while ((ch = getopt(argc, argv, "o:")) != -1)
771590Srgrimes		switch(ch) {
781590Srgrimes		case 'o':
791590Srgrimes			if (*optarg == '1') {
801590Srgrimes				cfncn = csum1;
811590Srgrimes				pfncn = psum1;
821590Srgrimes			} else if (*optarg == '2') {
831590Srgrimes				cfncn = csum2;
841590Srgrimes				pfncn = psum2;
851590Srgrimes			} else {
8626922Scharnier				warnx("illegal argument to -o option");
871590Srgrimes				usage();
881590Srgrimes			}
891590Srgrimes			break;
901590Srgrimes		case '?':
911590Srgrimes		default:
921590Srgrimes			usage();
931590Srgrimes		}
941590Srgrimes	argc -= optind;
951590Srgrimes	argv += optind;
961590Srgrimes
971590Srgrimes	fd = STDIN_FILENO;
981590Srgrimes	fn = NULL;
991590Srgrimes	rval = 0;
1001590Srgrimes	do {
1011590Srgrimes		if (*argv) {
1021590Srgrimes			fn = *argv++;
1031590Srgrimes			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
10426922Scharnier				warn("%s", fn);
1051590Srgrimes				rval = 1;
1061590Srgrimes				continue;
1071590Srgrimes			}
1081590Srgrimes		}
1091590Srgrimes		if (cfncn(fd, &val, &len)) {
11026922Scharnier			warn("%s", fn ? fn : "stdin");
1111590Srgrimes			rval = 1;
1121590Srgrimes		} else
1131590Srgrimes			pfncn(fn, val, len);
1141590Srgrimes		(void)close(fd);
1151590Srgrimes	} while (*argv);
1161590Srgrimes	exit(rval);
1171590Srgrimes}
1181590Srgrimes
11926922Scharnierstatic void
1201590Srgrimesusage()
1211590Srgrimes{
1221590Srgrimes	(void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n");
1231590Srgrimes	exit(1);
1241590Srgrimes}
125