Home | History | Annotate | Line # | Download | only in cksum
cksum.c revision 1.10
      1  1.10    lukem /*	$NetBSD: cksum.c,v 1.10 1997/10/17 11:36:59 lukem Exp $	*/
      2   1.6    glass 
      3   1.1      cgd /*-
      4   1.8  thorpej  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
      5   1.3      cgd  * Copyright (c) 1991, 1993
      6   1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1      cgd  *
      8   1.1      cgd  * This code is derived from software contributed to Berkeley by
      9   1.3      cgd  * James W. Williams of NASA Goddard Space Flight Center.
     10   1.1      cgd  *
     11   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1      cgd  * modification, are permitted provided that the following conditions
     13   1.1      cgd  * are met:
     14   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1      cgd  *    must display the following acknowledgement:
     21   1.1      cgd  *	This product includes software developed by the University of
     22   1.1      cgd  *	California, Berkeley and its contributors.
     23   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1      cgd  *    may be used to endorse or promote products derived from this software
     25   1.1      cgd  *    without specific prior written permission.
     26   1.1      cgd  *
     27   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1      cgd  * SUCH DAMAGE.
     38   1.1      cgd  */
     39   1.1      cgd 
     40  1.10    lukem #include <sys/cdefs.h>
     41   1.1      cgd #ifndef lint
     42  1.10    lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     43  1.10    lukem 	The Regents of the University of California.  All rights reserved.\n");
     44   1.1      cgd #endif /* not lint */
     45   1.1      cgd 
     46   1.1      cgd #ifndef lint
     47   1.6    glass #if 0
     48   1.7      jtc static char sccsid[] = "@(#)cksum.c	8.2 (Berkeley) 4/28/95";
     49   1.6    glass #endif
     50  1.10    lukem __RCSID("$NetBSD: cksum.c,v 1.10 1997/10/17 11:36:59 lukem Exp $");
     51   1.1      cgd #endif /* not lint */
     52   1.1      cgd 
     53   1.1      cgd #include <sys/cdefs.h>
     54   1.1      cgd #include <sys/types.h>
     55   1.5  mycroft 
     56   1.5  mycroft #include <err.h>
     57   1.5  mycroft #include <errno.h>
     58   1.1      cgd #include <fcntl.h>
     59   1.9   kleink #include <locale.h>
     60   1.8  thorpej #include <md5.h>
     61   1.1      cgd #include <stdio.h>
     62   1.1      cgd #include <stdlib.h>
     63   1.1      cgd #include <string.h>
     64   1.5  mycroft #include <unistd.h>
     65   1.5  mycroft 
     66   1.1      cgd #include "extern.h"
     67   1.1      cgd 
     68  1.10    lukem int	main __P((int, char **));
     69  1.10    lukem int	md5_digest_file __P((char *));
     70  1.10    lukem void	requiremd5 __P((const char *));
     71  1.10    lukem void	usage __P((void));
     72   1.3      cgd 
     73   1.3      cgd int
     74   1.1      cgd main(argc, argv)
     75   1.1      cgd 	int argc;
     76   1.1      cgd 	char **argv;
     77   1.1      cgd {
     78   1.8  thorpej 	register int ch, fd, rval, domd5, dosum, pflag, nomd5stdin;
     79   1.4      cgd 	u_int32_t len, val;
     80   1.1      cgd 	char *fn;
     81   1.4      cgd 	int (*cfncn) __P((int, u_int32_t *, u_int32_t *));
     82   1.4      cgd 	void (*pfncn) __P((char *, u_int32_t, u_int32_t));
     83   1.5  mycroft 	extern char *__progname;
     84   1.1      cgd 
     85  1.10    lukem 	cfncn = NULL;
     86  1.10    lukem 	pfncn = NULL;
     87   1.8  thorpej 	dosum = domd5 = pflag = nomd5stdin = 0;
     88   1.9   kleink 
     89   1.9   kleink 	setlocale(LC_ALL, "");
     90   1.8  thorpej 
     91   1.8  thorpej 	if (!strcmp(__progname, "md5"))
     92   1.8  thorpej 		domd5 = 1;
     93   1.8  thorpej 	else if (!strcmp(__progname, "sum")) {
     94   1.8  thorpej 		dosum = 1;
     95   1.5  mycroft 		cfncn = csum1;
     96   1.5  mycroft 		pfncn = psum1;
     97   1.5  mycroft 	} else {
     98   1.5  mycroft 		cfncn = crc;
     99   1.5  mycroft 		pfncn = pcrc;
    100   1.5  mycroft 	}
    101   1.5  mycroft 
    102   1.8  thorpej 	while ((ch = getopt(argc, argv, "mo:ps:tx")) != -1)
    103   1.1      cgd 		switch(ch) {
    104   1.8  thorpej 		case 'm':
    105   1.8  thorpej 			if (dosum) {
    106   1.8  thorpej 				warnx("sum mutually exclusive with md5");
    107   1.8  thorpej 				usage();
    108   1.8  thorpej 			}
    109   1.8  thorpej 			domd5 = 1;
    110   1.8  thorpej 			break;
    111   1.1      cgd 		case 'o':
    112   1.8  thorpej 			if (domd5) {
    113   1.8  thorpej 				warnx("md5 mutually exclusive with sum");
    114   1.8  thorpej 				usage();
    115   1.8  thorpej 			}
    116   1.5  mycroft 			if (!strcmp(optarg, "1")) {
    117   1.1      cgd 				cfncn = csum1;
    118   1.1      cgd 				pfncn = psum1;
    119   1.5  mycroft 			} else if (!strcmp(optarg, "2")) {
    120   1.1      cgd 				cfncn = csum2;
    121   1.1      cgd 				pfncn = psum2;
    122   1.1      cgd 			} else {
    123   1.5  mycroft 				warnx("illegal argument to -o option");
    124   1.1      cgd 				usage();
    125   1.1      cgd 			}
    126   1.1      cgd 			break;
    127   1.8  thorpej 		case 'p':
    128   1.8  thorpej 			if (!domd5)
    129   1.8  thorpej 				requiremd5("-p");
    130   1.8  thorpej 			pflag = 1;
    131   1.8  thorpej 			break;
    132   1.8  thorpej 		case 's':
    133   1.8  thorpej 			if (!domd5)
    134   1.8  thorpej 				requiremd5("-s");
    135   1.8  thorpej 			nomd5stdin = 1;
    136   1.8  thorpej 			MDString(optarg);
    137   1.8  thorpej 			break;
    138   1.8  thorpej 		case 't':
    139   1.8  thorpej 			if (!domd5)
    140   1.8  thorpej 				requiremd5("-t");
    141   1.8  thorpej 			MDTimeTrial();
    142   1.8  thorpej 			nomd5stdin = 1;
    143   1.8  thorpej 			break;
    144   1.8  thorpej 		case 'x':
    145   1.8  thorpej 			if (!domd5)
    146   1.8  thorpej 				requiremd5("-x");
    147   1.8  thorpej 			MDTestSuite();
    148   1.8  thorpej 			nomd5stdin = 1;
    149   1.8  thorpej 			break;
    150   1.1      cgd 		case '?':
    151   1.1      cgd 		default:
    152   1.1      cgd 			usage();
    153   1.1      cgd 		}
    154   1.1      cgd 	argc -= optind;
    155   1.1      cgd 	argv += optind;
    156   1.1      cgd 
    157   1.1      cgd 	fd = STDIN_FILENO;
    158   1.3      cgd 	fn = NULL;
    159   1.1      cgd 	rval = 0;
    160   1.1      cgd 	do {
    161   1.1      cgd 		if (*argv) {
    162   1.1      cgd 			fn = *argv++;
    163   1.8  thorpej 			if (domd5) {
    164   1.8  thorpej 				if (md5_digest_file(fn)) {
    165   1.8  thorpej 					warn("%s", fn);
    166   1.8  thorpej 					rval = 1;
    167   1.8  thorpej 				}
    168   1.8  thorpej 				continue;
    169   1.8  thorpej 			}
    170   1.1      cgd 			if ((fd = open(fn, O_RDONLY, 0)) < 0) {
    171   1.5  mycroft 				warn("%s", fn);
    172   1.1      cgd 				rval = 1;
    173   1.1      cgd 				continue;
    174   1.1      cgd 			}
    175   1.8  thorpej 		} else if (domd5 && !nomd5stdin)
    176   1.8  thorpej 			MDFilter(pflag);
    177   1.8  thorpej 
    178   1.8  thorpej 		if (!domd5) {
    179   1.8  thorpej 			if (cfncn(fd, &val, &len)) {
    180   1.8  thorpej 				warn("%s", fn ? fn : "stdin");
    181   1.8  thorpej 				rval = 1;
    182   1.8  thorpej 			} else
    183   1.8  thorpej 				pfncn(fn, val, len);
    184   1.8  thorpej 			(void)close(fd);
    185   1.1      cgd 		}
    186   1.1      cgd 	} while (*argv);
    187   1.1      cgd 	exit(rval);
    188   1.1      cgd }
    189   1.1      cgd 
    190   1.8  thorpej int
    191   1.8  thorpej md5_digest_file(fn)
    192   1.8  thorpej 	char *fn;
    193   1.8  thorpej {
    194   1.8  thorpej 	char buf[33], *cp;
    195   1.8  thorpej 
    196   1.8  thorpej 	cp = MD5File(fn, buf);
    197   1.8  thorpej 	if (cp == NULL)
    198   1.8  thorpej 		return (1);
    199   1.8  thorpej 
    200   1.8  thorpej 	printf("MD5 (%s) = %s\n", fn, cp);
    201   1.8  thorpej 	return (0);
    202   1.8  thorpej }
    203   1.8  thorpej 
    204   1.8  thorpej void
    205   1.8  thorpej requiremd5(flg)
    206   1.8  thorpej 	const char *flg;
    207   1.8  thorpej {
    208   1.8  thorpej 	warnx("%s flag requires `md5' or -m", flg);
    209   1.8  thorpej 	usage();
    210   1.8  thorpej }
    211   1.8  thorpej 
    212   1.3      cgd void
    213   1.1      cgd usage()
    214   1.1      cgd {
    215   1.5  mycroft 
    216   1.8  thorpej 	(void)fprintf(stderr, "usage: cksum [-m | [-o 1 | 2]] [file ...]\n");
    217   1.8  thorpej 	(void)fprintf(stderr, "       sum [file ...]\n");
    218   1.8  thorpej 	(void)fprintf(stderr,
    219   1.8  thorpej 	    "       md5 [-p | -t | -x | -s string] [file ...]\n");
    220   1.1      cgd 	exit(1);
    221   1.1      cgd }
    222