cksum.c revision 87212
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[] = 4850477Speter "$FreeBSD: head/usr.bin/cksum/cksum.c 87212 2001-12-02 12:54:40Z markm $"; 491590Srgrimes#endif /* not lint */ 501590Srgrimes 511590Srgrimes#include <sys/types.h> 5227222Sbde 5326922Scharnier#include <err.h> 541590Srgrimes#include <fcntl.h> 551590Srgrimes#include <stdio.h> 5678718Sdd#include <stdlib.h> 571590Srgrimes#include <string.h> 5826922Scharnier#include <unistd.h> 5927222Sbde 601590Srgrimes#include "extern.h" 611590Srgrimes 6226922Scharnierstatic void usage __P((void)); 631590Srgrimes 641590Srgrimesint 651590Srgrimesmain(argc, argv) 661590Srgrimes int argc; 671590Srgrimes char **argv; 681590Srgrimes{ 6987212Smarkm int ch, fd, rval; 7034447Sjb u_int32_t len, val; 7127222Sbde char *fn, *p; 7234447Sjb int (*cfncn) __P((int, u_int32_t *, u_int32_t *)); 7334447Sjb void (*pfncn) __P((char *, u_int32_t, u_int32_t)); 741590Srgrimes 7527222Sbde if ((p = rindex(argv[0], '/')) == NULL) 7627222Sbde p = argv[0]; 7727222Sbde else 7827222Sbde ++p; 7927223Sbde if (!strcmp(p, "sum")) { 8027223Sbde cfncn = csum1; 8127223Sbde pfncn = psum1; 8231056Sobrien ++argv; 8327223Sbde } else { 8427222Sbde cfncn = crc; 8527222Sbde pfncn = pcrc; 8627222Sbde 8731054Sobrien while ((ch = getopt(argc, argv, "o:")) != -1) 8831054Sobrien switch (ch) { 8931054Sobrien case 'o': 9031054Sobrien if (!strcmp(optarg, "1")) { 9131054Sobrien cfncn = csum1; 9231054Sobrien pfncn = psum1; 9331054Sobrien } else if (!strcmp(optarg, "2")) { 9431054Sobrien cfncn = csum2; 9531054Sobrien pfncn = psum2; 9654162Scharnier } else if (!strcmp(optarg, "3")) { 9731054Sobrien cfncn = crc32; 9831054Sobrien pfncn = pcrc; 9931054Sobrien } else { 10031054Sobrien warnx("illegal argument to -o option"); 10131054Sobrien usage(); 10231054Sobrien } 10331054Sobrien break; 10431054Sobrien case '?': 10531054Sobrien default: 1061590Srgrimes usage(); 1071590Srgrimes } 10831054Sobrien argc -= optind; 10931054Sobrien argv += optind; 11031054Sobrien } 1111590Srgrimes 1121590Srgrimes fd = STDIN_FILENO; 1131590Srgrimes fn = NULL; 1141590Srgrimes rval = 0; 1151590Srgrimes do { 1161590Srgrimes if (*argv) { 1171590Srgrimes fn = *argv++; 1181590Srgrimes if ((fd = open(fn, O_RDONLY, 0)) < 0) { 11926922Scharnier warn("%s", fn); 1201590Srgrimes rval = 1; 1211590Srgrimes continue; 1221590Srgrimes } 1231590Srgrimes } 1241590Srgrimes if (cfncn(fd, &val, &len)) { 12526922Scharnier warn("%s", fn ? fn : "stdin"); 1261590Srgrimes rval = 1; 1271590Srgrimes } else 1281590Srgrimes pfncn(fn, val, len); 1291590Srgrimes (void)close(fd); 1301590Srgrimes } while (*argv); 1311590Srgrimes exit(rval); 1321590Srgrimes} 1331590Srgrimes 13426922Scharnierstatic void 1351590Srgrimesusage() 1361590Srgrimes{ 13754162Scharnier (void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n"); 13827223Sbde (void)fprintf(stderr, " sum [file ...]\n"); 1391590Srgrimes exit(1); 1401590Srgrimes} 141