From 0b4add3077e27767bc7cfc91457b82e103c7bbb6 Mon Sep 17 00:00:00 2001 From: hubertf Date: Mon, 24 Apr 2006 21:07:43 +0000 Subject: [PATCH] Allow reading the checksum list from stdin if no file is given with -c. This allows verifying the checksums e.g. after downloading a NetBSD release: cat BSDSUM CKSUM MD5 SHA512 SYSVSUM | cksum -c --- usr.bin/cksum/cksum.1 | 31 ++++++++++++++++--------------- usr.bin/cksum/cksum.c | 27 ++++++++++++++++----------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/usr.bin/cksum/cksum.1 b/usr.bin/cksum/cksum.1 index 5c51bd98974c..4b161b5aa8cc 100644 --- a/usr.bin/cksum/cksum.1 +++ b/usr.bin/cksum/cksum.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: cksum.1,v 1.35 2006/04/23 17:45:04 wiz Exp $ +.\" $NetBSD: cksum.1,v 1.36 2006/04/24 21:07:43 hubertf Exp $ .\" .\" Copyright (c) 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -49,18 +49,18 @@ .Op Fl nw .Oo .Fl a Ar algorithm | -.Fl c Ar file | +.Fl c | .Op Fl o Ar 1 | Ar 2 .Oc .Op Ar .Nm sum .Op Fl w -.Op Fl c Ar file +.Op Fl c .Op Ar .Nm md2 .Op Fl nw .Oo -.Fl c Ar file | +.Fl c | .Fl p | .Fl t | .Fl x | @@ -70,7 +70,7 @@ .Nm md4 .Op Fl nw .Oo -.Fl c Ar file | +.Fl c | .Fl p | .Fl t | .Fl x | @@ -80,7 +80,7 @@ .Nm md5 .Op Fl nw .Oo -.Fl c Ar file | +.Fl c | .Fl p | .Fl t | .Fl x | @@ -90,7 +90,7 @@ .Nm sha1 .Op Fl nw .Oo -.Fl c Ar file | +.Fl c | .Fl p | .Fl t | .Fl x | @@ -100,7 +100,7 @@ .Nm rmd160 .Op Fl nw .Oo -.Fl c Ar file | +.Fl c | .Fl p | .Fl t | .Fl x | @@ -210,18 +210,19 @@ and .Fl o Ar 2 , respectively. The default is CRC. -.It Fl c Ar file -Verify a list of checksums generated by the -.Nm -program and stored in -.Pa file -against files on disk. +.It Fl c +Verify (check) files against a list of checksums. +The list is read from a filename given on the command line, or from +stdin if no filename is given. E.g. first run .Dl Ic md5 *.tgz \*[Gt] MD5 +.Dl Ic sha1 *.tgz \*[Gt] SHA1 to generate a list of MD5 checksums in .Pa MD5 , then use the following command to verify them: -.Dl Ic md5 -c MD5 +.Dl Ic cat MD5 SHA1 | cksum -c +If an error is found during checksum verification, an error +message is printed, and the program returns an error code of 1. .It Fl o Use historic algorithms instead of the (superior) default one. .Pp diff --git a/usr.bin/cksum/cksum.c b/usr.bin/cksum/cksum.c index b567052e4de7..07b9f850628c 100644 --- a/usr.bin/cksum/cksum.c +++ b/usr.bin/cksum/cksum.c @@ -1,4 +1,4 @@ -/* $NetBSD: cksum.c,v 1.34 2006/04/24 19:41:41 hubertf Exp $ */ +/* $NetBSD: cksum.c,v 1.35 2006/04/24 21:07:43 hubertf Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\ #if 0 static char sccsid[] = "@(#)cksum.c 8.2 (Berkeley) 4/28/95"; #endif -__RCSID("$NetBSD: cksum.c,v 1.34 2006/04/24 19:41:41 hubertf Exp $"); +__RCSID("$NetBSD: cksum.c,v 1.35 2006/04/24 21:07:43 hubertf Exp $"); #endif /* not lint */ #include @@ -163,15 +163,14 @@ main(int argc, char **argv) int (*cfncn) (int, u_int32_t *, off_t *); void (*pfncn) (char *, u_int32_t, off_t); struct hash *hash; - int normal, i, check_warn; - char *checkfile; + int normal, i, check_warn, do_check; cfncn = NULL; pfncn = NULL; dosum = pflag = nohashstdin = 0; normal = 0; - checkfile = NULL; check_warn = 0; + do_check = 0; setlocale(LC_ALL, ""); @@ -199,7 +198,7 @@ main(int argc, char **argv) * are still supported in code to not break anything that might * be using them. */ - while ((ch = getopt(argc, argv, "a:c:mno:ps:twx12456")) != -1) + while ((ch = getopt(argc, argv, "a:cmno:ps:twx12456")) != -1) switch(ch) { case 'a': if (hash != NULL || dosum) { @@ -267,7 +266,7 @@ main(int argc, char **argv) hash = &hashes[HASH_RMD160]; break; case 'c': - checkfile = optarg; + do_check = 1; break; case 'n': normal = 1; @@ -322,7 +321,7 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (checkfile) { + if (do_check) { /* * Verify checksums */ @@ -336,10 +335,16 @@ main(int argc, char **argv) rval = 0; cnt = badcnt = 0; - - f = fopen(checkfile, "r"); + + if (argc == 0) { + f = fdopen(STDIN_FILENO, "r"); + } else { + f = fopen(argv[0], "r"); + } if (f == NULL) - err(1, "Cannot read %s", checkfile); + err(1, "Cannot read %s", + argc>0?argv[0]:"stdin"); + while(fgets(buf, sizeof(buf), f) != NULL) { s=strrchr(buf, '\n'); if (s)