git: 72ee91fed4cf - main - md5: Accept "-" as alias for stdin.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 16 Feb 2024 12:50:51 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=72ee91fed4cfdcfbfb767cc166370b40e50d446a

commit 72ee91fed4cfdcfbfb767cc166370b40e50d446a
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-02-16 12:36:58 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-02-16 12:37:29 +0000

    md5: Accept "-" as alias for stdin.
    
    (based on a patch by jhb)
    
    MFC after:      1 week
    PR:             276915
    Reported by:    Hannes Hauswedell <h2+fbsdports@fsfe.org>
    Reviewed by:    allanjude, markj, jhb, emaste
    Differential Revision:  https://reviews.freebsd.org/D43870
---
 sbin/md5/md5.1             |  7 ++++++-
 sbin/md5/md5.c             | 12 +++++++++---
 sbin/md5/tests/md5_test.sh | 10 ++++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1
index 0cdfff928211..a17ef5ae30b7 100644
--- a/sbin/md5/md5.1
+++ b/sbin/md5/md5.1
@@ -1,4 +1,4 @@
-.Dd May 10, 2023
+.Dd February 13, 2024
 .Dt MD5 1
 .Os
 .Sh NAME
@@ -79,6 +79,11 @@ utility does the same, but with command-line options and an output
 format that match those of the similarly named utility that ships with
 Perl.
 .Pp
+In all cases, each file listed on the command line is processed separately.
+If no files are listed on the command line, or a file name is given as
+.Pa - ,
+input is taken from stdin instead.
+.Pp
 It is conjectured that it is computationally infeasible to
 produce two messages having the same message digest, or to produce any
 message having a given prespecified target message digest.
diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c
index c8292fe2f692..29f212148ce3 100644
--- a/sbin/md5/md5.c
+++ b/sbin/md5/md5.c
@@ -611,11 +611,16 @@ main(int argc, char *argv[])
 			const char *filename = *argv;
 			const char *filemode = "rb";
 
+			if (strcmp(filename, "-") == 0) {
+				f = stdin;
+			} else {
 #ifdef HAVE_CAPSICUM
-			if ((f = fileargs_fopen(fa, filename, filemode)) == NULL) {
+				f = fileargs_fopen(fa, filename, filemode);
 #else
-			if ((f = fopen(filename, filemode)) == NULL) {
+				f = fopen(filename, filemode);
 #endif
+			}
+			if (f == NULL) {
 				if (errno != ENOENT || !(cflag && ignoreMissing)) {
 					warn("%s", filename);
 					failed = true;
@@ -633,7 +638,8 @@ main(int argc, char *argv[])
 				rec = rec->next;
 			}
 			p = MDInput(&Algorithm[digest], f, buf, false);
-			(void)fclose(f);
+			if (f != stdin)
+				(void)fclose(f);
 			MDOutput(&Algorithm[digest], p, filename);
 		} while (*++argv);
 	} else if (!cflag && string == NULL && !skip) {
diff --git a/sbin/md5/tests/md5_test.sh b/sbin/md5/tests/md5_test.sh
index c6bc1dfd7be0..6b00a6b102c4 100644
--- a/sbin/md5/tests/md5_test.sh
+++ b/sbin/md5/tests/md5_test.sh
@@ -197,7 +197,9 @@ bsd_${alg}_vec${i}_body() {
 	printf '%s' \"\$inp_${i}\" >in
 	atf_check -o inline:\"\$out_${i}_${alg}\n\" ${alg} <in
 	atf_check -o inline:\"\$name_bsd_${alg} (in) = \$out_${i}_${alg}\n\" ${alg} in
+	atf_check -o inline:\"\$name_bsd_${alg} (-) = \$out_${i}_${alg}\n\" ${alg} - <in
 	atf_check -o inline:\"\$out_${i}_${alg} in\n\" ${alg} -r in
+	atf_check -o inline:\"\$out_${i}_${alg} -\n\" ${alg} -r - <in
 	# -q overrides -r regardless of order
 	for opt in -q -qr -rq ; do
 		atf_check -o inline:\"\$out_${i}_${alg}\n\" ${alg} \${opt} in
@@ -215,9 +217,13 @@ gnu_${alg}_vec${i}_body() {
 	atf_check -o inline:\"\$out_${i}_${alg}  -\n\" ${alg}sum <in
 	atf_check -o inline:\"\$out_${i}_${alg} *-\n\" ${alg}sum -b <in
 	atf_check -o inline:\"\$out_${i}_${alg}  in\n\" ${alg}sum in
+	atf_check -o inline:\"\$out_${i}_${alg}  -\n\" ${alg}sum - <in
 	atf_check -o inline:\"\$out_${i}_${alg} *in\n\" ${alg}sum -b in
+	atf_check -o inline:\"\$out_${i}_${alg} *-\n\" ${alg}sum -b - <in
 	atf_check -o inline:\"\$name_bsd_${alg} (in) = \$out_${i}_${alg}\n\" ${alg}sum --tag in
+	atf_check -o inline:\"\$name_bsd_${alg} (-) = \$out_${i}_${alg}\n\" ${alg}sum --tag - <in
 	atf_check -o inline:\"\$out_${i}_${alg}  in\0\" ${alg}sum -z in
+	atf_check -o inline:\"\$out_${i}_${alg}  -\0\" ${alg}sum -z - <in
 }
 "
 		eval "
@@ -233,9 +239,13 @@ perl_${alg}_vec${i}_body() {
 	atf_check -o inline:\"\$out_${i}_${alg} *-\n\" shasum \$alg_perl_${alg} -b <in
 	atf_check -o inline:\"\$out_${i}_${alg} U-\n\" shasum \$alg_perl_${alg} -U <in
 	atf_check -o inline:\"\$out_${i}_${alg}  in\n\" shasum \$alg_perl_${alg} in
+	atf_check -o inline:\"\$out_${i}_${alg}  -\n\" shasum \$alg_perl_${alg} - <in
 	atf_check -o inline:\"\$out_${i}_${alg} *in\n\" shasum \$alg_perl_${alg} -b in
+	atf_check -o inline:\"\$out_${i}_${alg} *-\n\" shasum \$alg_perl_${alg} -b - <in
 	atf_check -o inline:\"\$out_${i}_${alg} Uin\n\" shasum \$alg_perl_${alg} -U in
+	atf_check -o inline:\"\$out_${i}_${alg} U-\n\" shasum \$alg_perl_${alg} -U - <in
 	atf_check -o inline:\"\$name_perl_${alg} (in) = \$out_${i}_${alg}\n\" shasum \$alg_perl_${alg} --tag in
+	atf_check -o inline:\"\$name_perl_${alg} (-) = \$out_${i}_${alg}\n\" shasum \$alg_perl_${alg} --tag - <in
 }
 "
 	done