147169Sbostic /*- 247169Sbostic * Copyright (c) 1991 The Regents of the University of California. 347169Sbostic * All rights reserved. 447169Sbostic * 547169Sbostic * This code is derived from software contributed to Berkeley by 6*51066Sbostic * James W. Williams of NASA Goddard Space Flight Center. 747169Sbostic * 847169Sbostic * %sccs.include.redist.c% 947169Sbostic */ 1047169Sbostic 1147169Sbostic #ifndef lint 1247169Sbostic char copyright[] = 1347169Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 1447169Sbostic All rights reserved.\n"; 1547169Sbostic #endif /* not lint */ 1647169Sbostic 1747169Sbostic #ifndef lint 18*51066Sbostic static char sccsid[] = "@(#)cksum.c 5.4 (Berkeley) 09/09/91"; 1947169Sbostic #endif /* not lint */ 2047169Sbostic 2147801Sbostic #include <sys/cdefs.h> 2247169Sbostic #include <sys/types.h> 2347169Sbostic #include <fcntl.h> 2447721Sbostic #include <unistd.h> 2547169Sbostic #include <stdio.h> 2647721Sbostic #include <errno.h> 2747169Sbostic #include <stdlib.h> 2847169Sbostic #include <string.h> 2947801Sbostic #include "extern.h" 3047169Sbostic 3147169Sbostic main(argc, argv) 3247169Sbostic int argc; 3347169Sbostic char **argv; 3447169Sbostic { 3547169Sbostic extern int optind; 3647721Sbostic u_long len, val; 3747169Sbostic register int ch, fd, rval; 3847169Sbostic char *fn; 3947801Sbostic int (*cfncn) __P((int, unsigned long *, unsigned long *)); 4047801Sbostic void (*pfncn) __P((char *, unsigned long, unsigned long)); 4147169Sbostic 4247801Sbostic cfncn = crc; 4347801Sbostic pfncn = pcrc; 4447801Sbostic while ((ch = getopt(argc, argv, "o:")) != EOF) 4547169Sbostic switch(ch) { 4647801Sbostic case 'o': 4747801Sbostic if (*optarg == '1') { 4847801Sbostic cfncn = csum1; 4947801Sbostic pfncn = psum1; 5047801Sbostic } else if (*optarg == '2') { 5147801Sbostic cfncn = csum2; 5247801Sbostic pfncn = psum2; 5347801Sbostic } else { 5447801Sbostic (void)fprintf(stderr, 5547801Sbostic "cksum: illegal argument to -o option\n"); 5647801Sbostic usage(); 5747801Sbostic } 5847801Sbostic break; 5947169Sbostic case '?': 6047169Sbostic default: 6147169Sbostic usage(); 6247169Sbostic } 6347169Sbostic argc -= optind; 6447169Sbostic argv += optind; 6547169Sbostic 6647169Sbostic fd = STDIN_FILENO; 6747169Sbostic fn = "stdin"; 6847169Sbostic rval = 0; 6947169Sbostic do { 7047169Sbostic if (*argv) { 7147169Sbostic fn = *argv++; 7247169Sbostic if ((fd = open(fn, O_RDONLY, 0)) < 0) { 7347169Sbostic (void)fprintf(stderr, 7447169Sbostic "cksum: %s: %s\n", fn, strerror(errno)); 7547169Sbostic rval = 1; 7647169Sbostic continue; 7747169Sbostic } 7847169Sbostic } 7947801Sbostic if (cfncn(fd, &val, &len)) { 8047721Sbostic (void)fprintf(stderr, 8147721Sbostic "cksum: %s: %s\n", fn, strerror(errno)); 8247721Sbostic rval = 1; 8347721Sbostic } else 8447801Sbostic pfncn(fn, val, len); 8547169Sbostic (void)close(fd); 8647169Sbostic } while (*argv); 8747169Sbostic exit(rval); 8847169Sbostic } 8947169Sbostic 9047169Sbostic usage() 9147169Sbostic { 9247801Sbostic (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n"); 9347169Sbostic exit(1); 9447169Sbostic } 95