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 * 4. Neither the name of the University nor the names of its contributors 171590Srgrimes * may be used to endorse or promote products derived from this software 181590Srgrimes * without specific prior written permission. 191590Srgrimes * 201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301590Srgrimes * SUCH DAMAGE. 311590Srgrimes */ 321590Srgrimes 331590Srgrimes#ifndef lint 3427223Sbdestatic const char copyright[] = 351590Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 361590Srgrimes The Regents of the University of California. All rights reserved.\n"; 371590Srgrimes#endif /* not lint */ 381590Srgrimes 391590Srgrimes#ifndef lint 4027223Sbde#if 0 4127222Sbdestatic char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95"; 4227223Sbde#endif 431590Srgrimes#endif /* not lint */ 44112212Srobert 4599112Sobrien#include <sys/cdefs.h> 4699112Sobrien__FBSDID("$FreeBSD$"); 471590Srgrimes 481590Srgrimes#include <sys/types.h> 4927222Sbde 5026922Scharnier#include <err.h> 511590Srgrimes#include <fcntl.h> 521590Srgrimes#include <stdio.h> 5378718Sdd#include <stdlib.h> 541590Srgrimes#include <string.h> 5526922Scharnier#include <unistd.h> 5627222Sbde 571590Srgrimes#include "extern.h" 581590Srgrimes 5992920Simpstatic void usage(void); 601590Srgrimes 611590Srgrimesint 62100815Sdwmalonemain(int argc, char **argv) 631590Srgrimes{ 64112212Srobert uint32_t val; 6587212Smarkm int ch, fd, rval; 66112212Srobert off_t len; 6727222Sbde char *fn, *p; 68112212Srobert int (*cfncn)(int, uint32_t *, off_t *); 69179024Sbrooks void (*pfncn)(char *, uint32_t, off_t); 701590Srgrimes 71229403Sed if ((p = strrchr(argv[0], '/')) == NULL) 7227222Sbde p = argv[0]; 7327222Sbde else 7427222Sbde ++p; 7527223Sbde if (!strcmp(p, "sum")) { 7627223Sbde cfncn = csum1; 7727223Sbde pfncn = psum1; 7831056Sobrien ++argv; 7927223Sbde } else { 8027222Sbde cfncn = crc; 8127222Sbde pfncn = pcrc; 8227222Sbde 8331054Sobrien while ((ch = getopt(argc, argv, "o:")) != -1) 8431054Sobrien switch (ch) { 8531054Sobrien case 'o': 8631054Sobrien if (!strcmp(optarg, "1")) { 8731054Sobrien cfncn = csum1; 8831054Sobrien pfncn = psum1; 8931054Sobrien } else if (!strcmp(optarg, "2")) { 9031054Sobrien cfncn = csum2; 9131054Sobrien pfncn = psum2; 9254162Scharnier } else if (!strcmp(optarg, "3")) { 9331054Sobrien cfncn = crc32; 9431054Sobrien pfncn = pcrc; 9531054Sobrien } else { 9631054Sobrien warnx("illegal argument to -o option"); 9731054Sobrien usage(); 9831054Sobrien } 9931054Sobrien break; 10031054Sobrien case '?': 10131054Sobrien default: 1021590Srgrimes usage(); 1031590Srgrimes } 10431054Sobrien argc -= optind; 10531054Sobrien argv += optind; 10631054Sobrien } 1071590Srgrimes 1081590Srgrimes fd = STDIN_FILENO; 1091590Srgrimes fn = NULL; 1101590Srgrimes rval = 0; 1111590Srgrimes do { 1121590Srgrimes if (*argv) { 1131590Srgrimes fn = *argv++; 1141590Srgrimes if ((fd = open(fn, O_RDONLY, 0)) < 0) { 11526922Scharnier warn("%s", fn); 1161590Srgrimes rval = 1; 1171590Srgrimes continue; 1181590Srgrimes } 1191590Srgrimes } 1201590Srgrimes if (cfncn(fd, &val, &len)) { 12126922Scharnier warn("%s", fn ? fn : "stdin"); 1221590Srgrimes rval = 1; 1231590Srgrimes } else 1241590Srgrimes pfncn(fn, val, len); 1251590Srgrimes (void)close(fd); 1261590Srgrimes } while (*argv); 1271590Srgrimes exit(rval); 1281590Srgrimes} 1291590Srgrimes 13026922Scharnierstatic void 131100815Sdwmaloneusage(void) 1321590Srgrimes{ 13354162Scharnier (void)fprintf(stderr, "usage: cksum [-o 1 | 2 | 3] [file ...]\n"); 13427223Sbde (void)fprintf(stderr, " sum [file ...]\n"); 1351590Srgrimes exit(1); 1361590Srgrimes} 137