git: c05533d97ac1 - main - md5: Fix GNU check mode.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 20 Feb 2024 22:05:06 UTC
The branch main has been updated by des:

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

commit c05533d97ac178f8e811407860d47d3c0ae61eed
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-02-20 22:04:40 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-02-20 22:04:40 +0000

    md5: Fix GNU check mode.
    
    Fixes:          9b20849bc5f1b500f2de7aeca77f0e6556069bbb
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    imp, allanjude, markj
    Differential Revision:  https://reviews.freebsd.org/D43990
---
 sbin/md5/md5.c | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c
index 158769812819..74b61f9bc1ad 100644
--- a/sbin/md5/md5.c
+++ b/sbin/md5/md5.c
@@ -287,9 +287,10 @@ MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len)
 }
 
 struct chksumrec {
-	char	*filename;
-	char	*chksum;
-	struct	chksumrec	*next;
+	char *filename;
+	enum input_mode input_mode;
+	char *chksum;
+	struct chksumrec *next;
 };
 
 static struct chksumrec *head = NULL;
@@ -362,16 +363,18 @@ gnu_check(const char *checksumsfile)
 		if (rec == NULL)
 			errx(1, "malloc failed");
 
-		if (*filename == '*' ||
-		    *filename == ' ' ||
-		    *filename == 'U' ||
-		    *filename == '^') {
-			if (lstat(filename, &st) != 0)
-				filename++;
+		if ((*filename == '*' || *filename == ' ' ||
+		    *filename == 'U' || *filename == '^') &&
+		    lstat(filename, &st) != 0 &&
+		    lstat(filename + 1, &st) == 0) {
+			rec->filename = strdup(filename + 1);
+			rec->input_mode = (enum input_mode)*filename;
+		} else {
+			rec->filename = strdup(filename);
+			rec->input_mode = input_mode;
 		}
 
 		rec->chksum = strdup(hashstr);
-		rec->filename = strdup(filename);
 		if (rec->chksum == NULL || rec->filename == NULL)
 			errx(1, "malloc failed");
 		rec->next = NULL;
@@ -612,6 +615,13 @@ main(int argc, char *argv[])
 			const char *filename = *argv;
 			const char *filemode = "rb";
 
+			if (cflag && mode != mode_bsd) {
+				input_mode = rec->input_mode;
+				checkAgainst = rec->chksum;
+				rec = rec->next;
+			}
+			if (input_mode == input_text)
+				filemode = "r";
 			if (strcmp(filename, "-") == 0) {
 				f = stdin;
 			} else {
@@ -626,14 +636,8 @@ main(int argc, char *argv[])
 					warn("%s", filename);
 					failed = true;
 				}
-				if (cflag && mode != mode_bsd)
-					rec = rec->next;
 				continue;
 			}
-			if (cflag && mode != mode_bsd) {
-				checkAgainst = rec->chksum;
-				rec = rec->next;
-			}
 			p = MDInput(&Algorithm[digest], f, buf, false);
 			if (f != stdin)
 				(void)fclose(f);