cksum.c revision 54162
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 54162 1999-12-05 20:03:22Z charnier $"; 491590Srgrimes#endif /* not lint */ 501590Srgrimes 511590Srgrimes#include <sys/types.h> 5227222Sbde 5326922Scharnier#include <err.h> 541590Srgrimes#include <fcntl.h> 551590Srgrimes#include <stdio.h> 561590Srgrimes#include <string.h> 5726922Scharnier#include <unistd.h> 5827222Sbde 591590Srgrimes#include "extern.h" 601590Srgrimes 6126922Scharnierstatic void usage __P((void)); 621590Srgrimes 631590Srgrimesint 641590Srgrimesmain(argc, argv) 651590Srgrimes int argc; 661590Srgrimes char **argv; 671590Srgrimes{ 6827222Sbde register int ch, fd, rval; 6934447Sjb u_int32_t len, val; 7027222Sbde char *fn, *p; 7134447Sjb int (*cfncn) __P((int, u_int32_t *, u_int32_t *)); 7234447Sjb void (*pfncn) __P((char *, u_int32_t, u_int32_t)); 731590Srgrimes 7427222Sbde if ((p = rindex(argv[0], '/')) == NULL) 7527222Sbde p = argv[0]; 7627222Sbde else 7727222Sbde ++p; 7827223Sbde if (!strcmp(p, "sum")) { 7927223Sbde cfncn = csum1; 8027223Sbde pfncn = psum1; 8131056Sobrien ++argv; 8227223Sbde } else { 8327222Sbde cfncn = crc; 8427222Sbde pfncn = pcrc; 8527222Sbde 8631054Sobrien while ((ch = getopt(argc, argv, "o:")) != -1) 8731054Sobrien switch (ch) { 8831054Sobrien case 'o': 8931054Sobrien if (!strcmp(optarg, "1")) { 9031054Sobrien cfncn = csum1; 9131054Sobrien pfncn = psum1; 9231054Sobrien } else if (!strcmp(optarg, "2")) { 9331054Sobrien cfncn = csum2; 9431054Sobrien pfncn = psum2; 9554162Scharnier } else if (!strcmp(optarg, "3")) { 9631054Sobrien cfncn = crc32; 9731054Sobrien pfncn = pcrc; 9831054Sobrien } else { 9931054Sobrien warnx("illegal argument to -o option"); 10031054Sobrien usage(); 10131054Sobrien } 10231054Sobrien break; 10331054Sobrien case '?': 10431054Sobrien default: 1051590Srgrimes usage(); 1061590Srgrimes } 10731054Sobrien argc -= optind; 10831054Sobrien argv += optind; 10931054Sobrien } 1101590Srgrimes 1111590Srgrimes fd = STDIN_FILENO; 1121590Srgrimes fn = NULL; 1131590Srgrimes rval = 0; 1141590Srgrimes do { 1151590Srgrimes if (*argv) { 1161590Srgrimes fn = *argv++; 1171590Srgrimes if ((fd = open(fn, O_RDONLY, 0)) < 0) { 11826922Scharnier warn("%s", fn); 1191590Srgrimes rval = 1; 1201590Srgrimes continue; 1211590Srgrimes } 1221590Srgrimes } 1231590Srgrimes if (cfncn(fd, &val, &len)) { 12426922Scharnier warn("%s", fn ? fn : "stdin"); 1251590Srgrimes rval = 1; 1261590Srgrimes } else 1271590Srgrimes pfncn(fn, val, len); 1281590Srgrimes (void)close(fd); 1291590Srgrimes } while (*argv); 1301590Srgrimes exit(rval); 1311590Srgrimes} 1321590Srgrimes 13326922Scharnierstatic void 1341590Srgrimesusage() 1351590Srgrimes{ 13654162Scharnier (void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n"); 13727223Sbde (void)fprintf(stderr, " sum [file ...]\n"); 1381590Srgrimes exit(1); 1391590Srgrimes} 140