From 9f3aa538e307743b2b5048d38f87b7fd32d0c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Tue, 26 Jul 2022 23:04:57 +0200 Subject: [PATCH] sbin/md5.c: fix -q -c for BSD style versions The BSD style commands (with names not ending in "sum") ignored the -c options and the passed digest value when invoked with -q. The man page stated that -q causes only the calculated digest to be printed, but did not consider the case of both the -q and -c being used in combination. Since there is no warning that -c will be ignored when the -q option is used, users night (and did) expect that the exit code would reflect the matching of the calculated digest and the argument passed with -c. This update implements and documents this expected behavior. PR: 265461 Reported by: Dmitrij MFC after: 2 weeks --- sbin/md5/md5.1 | 11 +++++++---- sbin/md5/md5.c | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1 index 899e49ba3517..a3db48596606 100644 --- a/sbin/md5/md5.1 +++ b/sbin/md5/md5.1 @@ -1,5 +1,5 @@ .\" $FreeBSD$ -.Dd Feb 5, 2022 +.Dd July 26, 2022 .Dt MD5 1 .Os .Sh NAME @@ -86,6 +86,9 @@ coreutils versions of these programs. If the program was called with a name that does not end in .Nm sum , compare the digest of the file against this string. +If combined with the +.Fl q +option, the calculated digest is printed in addition to the exit status being set. .Pq Note that this option is not yet useful if multiple files are specified. .It Fl c Ar file If the program was called with a name that does end in @@ -94,7 +97,7 @@ the file passed as argument must contain digest lines generated by the same digest algorithm with or without the .Fl r option -.Pq i.e. in either classical BSD format or in GNU coreutils format . +.Pq i.e., in either classical BSD format or in GNU coreutils format . A line with the file name followed by a colon .Dq ":" and either OK or FAILED is written for each well-formed line in the digest file. @@ -164,7 +167,7 @@ d80bf36c332dc0fdc479366ec3fa44cd /etc/rc.conf .Pd The .Nm -sum -variants put 2 blank characters between hash and file name for full compatibility +variants put 2 blank characters between hash and file name for full compatibility with the coreutils versions of these commands. .Ed .Pp @@ -207,7 +210,7 @@ $ md5 -c digest /boot/loader.conf The digest file may contain any number of lines in the format generated with or without the .Fl r option -.Pq i.e. in either classical BSD format or in GNU coreutils format . +.Pq i.e., in either classical BSD format or in GNU coreutils format . If a hash value does not match the file, FAILED is printed instead of OK. .Sh SEE ALSO .Xr cksum 1 , diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c index 7235e6e0a39f..97c587efd63c 100644 --- a/sbin/md5/md5.c +++ b/sbin/md5/md5.c @@ -436,6 +436,8 @@ MDOutput(const Algorithm_t *alg, char *p, char *argv[]) printf("%s: %s\n", *argv, checkfailed ? "FAILED" : "OK"); } else if (qflag || argv == NULL) { printf("%s\n", p); + if (cflag) + checkfailed = strcasecmp(checkAgainst, p) != 0; } else { if (rflag) if (gnu_emu)