Bug 217571 - [Feature suggestion] make md5 print the checksum to stderr
Summary: [Feature suggestion] make md5 print the checksum to stderr
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 11.0-STABLE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2017-03-05 22:43 UTC by manon
Modified: 2017-03-06 04:06 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description manon 2017-03-05 22:43:38 UTC
Hi, 

the following patch patch adds  option -P to md5/sha[1/256..] to print the digest to stderr when operating in filter mode. this is usefull when  printing the digest while redirecting stdout. eg: sha512 -P < tarfile | tar -xf- -C /test/dir 

hope this makes sense.

Kind regards,
Manon

diff -ur /usr/src/sbin/md5/md5.1 ./md5.1
--- /usr/src/sbin/md5/md5.1	2016-10-15 22:19:31.346635000 +0200
+++ ./md5.1	2017-03-05 23:05:31.938946000 +0100
@@ -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
@@ -109,6 +109,8 @@
 .Ar string .
 .It Fl p
 Echo stdin to stdout and append the checksum to stdout.
+.It Fl P
+Echo stdin to stdout and write the checksum to stderr.
 .It Fl q
 Quiet mode \(em only the checksum is printed out.
 Overrides the
diff -ur /usr/src/sbin/md5/md5.c ./md5.c
--- /usr/src/sbin/md5/md5.c	2016-10-15 22:19:31.349312000 +0200
+++ ./md5.c	2017-03-05 23:07:54.742808000 +0100
@@ -176,7 +176,7 @@
 	failed = 0;
 	checkAgainst = NULL;
 	checksFailed = 0;
-	while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1)
+	while ((ch = getopt(argc, argv, "c:Ppqrs:tx")) != -1)
 		switch (ch) {
 		case 'c':
 			checkAgainst = optarg;
@@ -184,6 +184,9 @@
 		case 'p':
 			MDFilter(&Algorithm[digest], 1);
 			break;
+		case 'P':
+			MDFilter(&Algorithm[digest], 2);
+			break;
 		case 'q':
 			qflag = 1;
 			break;
@@ -456,6 +459,7 @@
 {
 	DIGEST_CTX context;
 	unsigned int len;
+	FILE *filehandle;
 	unsigned char buffer[BUFSIZ];
 	char buf[HEX_DIGEST_LENGTH];
 
@@ -465,13 +469,17 @@
 			err(1, "stdout");
 		alg->Update(&context, buffer, len);
 	}
-	printf("%s\n", alg->End(&context, buf));
+	
+	filehandle = stdin;
+	if (tee > 1) 
+		filehandle = stderr;
+	fprintf(filehandle, "%s\n", alg->End(&context, buf));
 }
 
 static void
 usage(const Algorithm_t *alg)
 {
 
-	fprintf(stderr, "usage: %s [-pqrtx] [-c string] [-s string] [files ...]\n", alg->progname);
+	fprintf(stderr, "usage: %s [-Ppqrtx] [-c string] [-s string] [files ...]\n", alg->progname);
 	exit(1);
 }