diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1 index 6fa0393..e9eb936 100644 --- a/sbin/md5/md5.1 +++ b/sbin/md5/md5.1 @@ -7,37 +7,37 @@ .Nd calculate a message-digest fingerprint (checksum) for a file .Sh SYNOPSIS .Nm md5 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm sha1 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm sha256 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm sha384 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm sha512 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm sha512t256 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar .Nm rmd160 -.Op Fl pqrtx +.Op Fl Ppqrtx .Op Fl c Ar string .Op Fl s Ar string .Op Ar @@ -102,11 +102,13 @@ The hexadecimal checksum of each file listed on the command line is printed after the options are processed. .Bl -tag -width indent .It Fl c Ar string -Compare the digest of the file against this string. +Compare the digest of the file or stdin against this string. .Pq Note that this option is not yet useful if multiple files are specified. .It Fl s Ar string Print a checksum of the given .Ar string . +.It Fl P +Echo stdin to stdout and append the checksum to stderr. .It Fl p Echo stdin to stdout and append the checksum to stdout. .It Fl q diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c index ddf3a3c..80b4864 100644 --- a/sbin/md5/md5.c +++ b/sbin/md5/md5.c @@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$"); #define TEST_BLOCK_COUNT 100000 #define MDTESTCOUNT 8 +static int pflag; static int qflag; static int rflag; static int sflag; @@ -176,13 +177,17 @@ main(int argc, char *argv[]) failed = 0; checkAgainst = NULL; checksFailed = 0; - while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1) + pflag = 0; + while ((ch = getopt(argc, argv, "c:Ppqrs:tx")) != -1) switch (ch) { case 'c': checkAgainst = optarg; break; + case 'P': + pflag = STDERR_FILENO; + break; case 'p': - MDFilter(&Algorithm[digest], 1); + pflag = STDOUT_FILENO; break; case 'q': qflag = 1; @@ -229,8 +234,8 @@ main(int argc, char *argv[]) printf("\n"); } } while (*++argv); - } else if (!sflag && (optind == 1 || qflag || rflag)) - MDFilter(&Algorithm[digest], 0); + } else if (!sflag && (optind == 1 || qflag || rflag || pflag || checkAgainst)) + MDFilter(&Algorithm[digest], pflag); if (failed != 0) return (1); @@ -449,7 +454,9 @@ MDTestSuite(const Algorithm_t *alg) } /* - * Digests the standard input and prints the result. + * Digests the standard input and prints the result to stdoud or stderr. In + * any case digested input is either passed to stdout (tee > 0), or not passed + * at all (tee == 0) */ static void MDFilter(const Algorithm_t *alg, int tee) @@ -458,6 +465,7 @@ MDFilter(const Algorithm_t *alg, int tee) unsigned int len; unsigned char buffer[BUFSIZ]; char buf[HEX_DIGEST_LENGTH]; + char *p; alg->Init(&context); while ((len = fread(buffer, 1, BUFSIZ, stdin))) { @@ -465,7 +473,14 @@ MDFilter(const Algorithm_t *alg, int tee) err(1, "stdout"); alg->Update(&context, buffer, len); } - printf("%s\n", alg->End(&context, buf)); + p = alg->End(&context, buf); + if (checkAgainst && strcmp(checkAgainst,p)) + { + checksFailed++; + if (!qflag) + printf(" [ Failed ]"); + } + fprintf(tee == STDERR_FILENO ? stderr : stdout, "%s\n", p); } static void