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 <bugs.freebsd@1fff.net>
MFC after:	2 weeks
This commit is contained in:
Stefan Eßer 2022-07-26 23:04:57 +02:00
parent 08604a023d
commit 9f3aa538e3
2 changed files with 9 additions and 4 deletions

View File

@ -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 ,

View File

@ -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)