git: 5f13ee9179e1 - stable/12 - md5: Create md5sum, etc compatible programs

Stefan Eßer se at FreeBSD.org
Tue Jul 6 15:38:48 UTC 2021


The branch stable/12 has been updated by se:

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

commit 5f13ee9179e15702129d5ea330c599ae9a3b871f
Author:     Warner Losh <imp at FreeBSD.org>
AuthorDate: 2021-05-19 17:26:20 +0000
Commit:     Stefan Eßer <se at FreeBSD.org>
CommitDate: 2021-07-06 15:37:24 +0000

    md5: Create md5sum, etc compatible programs
    
    On Linux, there's a similar set of programs to ours, but that end in the
    letters 'sum'. These act basically like FreeBSD versions run with the -r
    option. Add code so that when the program ends in 'sum' you get the
    linux -r behavior. This is enough to make most things that use sha*sum
    work correctly (the -c / --check options, as well as the long args are
    not implemented). When running with the -sum programs, ignore -t instead
    of running internal speed tests and make -c an error.
    
    Reviewed by:            sef, and kp and allanjude (earlier version)
    Relnotes:               yes
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D30309
---
 sbin/md5/Makefile | 30 ++++++++++++++++++++++++++----
 sbin/md5/md5.1    | 32 +++++++++++++++++++++++++++++++-
 sbin/md5/md5.c    | 35 ++++++++++++++++++++++++++++++-----
 3 files changed, 87 insertions(+), 10 deletions(-)

diff --git a/sbin/md5/Makefile b/sbin/md5/Makefile
index 6e29d94eb92a..e499967d23d5 100644
--- a/sbin/md5/Makefile
+++ b/sbin/md5/Makefile
@@ -4,27 +4,49 @@
 PACKAGE=runtime
 PROG=	md5
 
-LINKS=	${BINDIR}/md5 ${BINDIR}/rmd160 \
+LINKS=	${BINDIR}/md5 ${BINDIR}/md5sum \
+	${BINDIR}/md5 ${BINDIR}/rmd160 \
+	${BINDIR}/md5 ${BINDIR}/rmd160sum \
 	${BINDIR}/md5 ${BINDIR}/sha1 \
+	${BINDIR}/md5 ${BINDIR}/sha1sum \
 	${BINDIR}/md5 ${BINDIR}/sha224 \
+	${BINDIR}/md5 ${BINDIR}/sha224sum \
 	${BINDIR}/md5 ${BINDIR}/sha256 \
+	${BINDIR}/md5 ${BINDIR}/sha256sum \
 	${BINDIR}/md5 ${BINDIR}/sha384 \
+	${BINDIR}/md5 ${BINDIR}/sha384sum \
 	${BINDIR}/md5 ${BINDIR}/sha512 \
+	${BINDIR}/md5 ${BINDIR}/sha512sum \
 	${BINDIR}/md5 ${BINDIR}/sha512t256 \
+	${BINDIR}/md5 ${BINDIR}/sha512t256sum \
 	${BINDIR}/md5 ${BINDIR}/skein256 \
+	${BINDIR}/md5 ${BINDIR}/skein256sum \
 	${BINDIR}/md5 ${BINDIR}/skein512 \
-	${BINDIR}/md5 ${BINDIR}/skein1024
+	${BINDIR}/md5 ${BINDIR}/skein512sum \
+	${BINDIR}/md5 ${BINDIR}/skein1024 \
+	${BINDIR}/md5 ${BINDIR}/skein1024sum
 
-MLINKS=	md5.1 rmd160.1 \
+MLINKS=	md5.1 md5sum.1 \
+	md5.1 rmd160.1 \
+	md5.1 rmd160sum.1 \
 	md5.1 sha1.1 \
+	md5.1 sha1sum.1 \
 	md5.1 sha224.1 \
+	md5.1 sha224sum.1 \
 	md5.1 sha256.1 \
+	md5.1 sha256sum.1 \
 	md5.1 sha384.1 \
+	md5.1 sha384sum.1 \
 	md5.1 sha512.1 \
+	md5.1 sha512sum.1 \
 	md5.1 sha512t256.1 \
+	md5.1 sha512t256sum.1 \
 	md5.1 skein256.1 \
+	md5.1 skein256sum.1 \
 	md5.1 skein512.1 \
-	md5.1 skein1024.1
+	md5.1 skein512sum.1 \
+	md5.1 skein1024.1 \
+	md5.1 skein1024sum.1
 
 LIBADD=	md
 
diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1
index 1cf0db333b74..af4e606b62e7 100644
--- a/sbin/md5/md5.1
+++ b/sbin/md5/md5.1
@@ -4,7 +4,9 @@
 .Os
 .Sh NAME
 .Nm md5 , sha1 , sha224 , sha256 , sha384 , sha512 , sha512t256 , rmd160 ,
-.Nm skein256 , skein512 , skein1024
+.Nm skein256 , skein512 , skein1024 ,
+.Nm md5sum , sha1sum , sha224sum , sha256sum , sha384sum , sha512sum ,
+.Nm sha512t256sum , rmd160sum , skein256sum , skein512sum , skein1024sum
 .Nd calculate a message-digest fingerprint (checksum) for a file
 .Sh SYNOPSIS
 .Nm
@@ -26,6 +28,15 @@ output a
 or
 .Dq message digest
 of the input.
+The
+.Nm md5sum , sha1sum , sha224sum , sha256sum , sha384sum , sha512sum ,
+.Nm sha512t256sum , rmd160sum , skein256sum , skein512sum ,
+and
+.Nm skein1024sum
+utilities do the same, but default to the reversed format of
+the
+.Fl r
+flag.
 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.
@@ -73,9 +84,18 @@ precede any files named on the command line.
 The hexadecimal checksum of each file listed on the command line is printed
 after the options are processed.
 .Bl -tag -width indent
+.It Fl b
+Ignored for compatibility with the coreutils
+.Nm -sum
+programs.
 .It Fl c Ar string
 Compare the digest of the file against this string.
 .Pq Note that this option is not yet useful if multiple files are specified.
+This option causes an error in for the
+.Nm -sum
+programs because it check the checksums listed in a file for the coreutils
+.Nm -sum
+programs that is not yet implemented.
 .It Fl s Ar string
 Print a checksum of the given
 .Ar string .
@@ -95,6 +115,9 @@ when combined with the
 options.
 .It Fl t
 Run a built-in time trial.
+For the
+.Nm -sum
+versions, this is a nop for compatibility with coreutils.
 .It Fl x
 Run a built-in test script.
 .El
@@ -144,6 +167,13 @@ Secure Hash Standard (SHS):
 .Pp
 The RIPEMD-160 page:
 .Pa http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html .
+.Sh BUGS
+All of the utilities that end in
+.Sq sum
+are intended to be compatible with the GNU coreutils programs.
+However, the long arguments and the
+.Fl -check
+functionality are not provided.
 .Sh ACKNOWLEDGMENTS
 This program is placed in the public domain for free general use by
 RSA Data Security.
diff --git a/sbin/md5/md5.c b/sbin/md5/md5.c
index 96dfcede9711..4381ef124c7b 100644
--- a/sbin/md5/md5.c
+++ b/sbin/md5/md5.c
@@ -177,13 +177,32 @@ main(int argc, char *argv[])
 	char	buf[HEX_DIGEST_LENGTH];
 	size_t	len;
  	unsigned	digest;
- 	const char*	progname;
+	char	*progname;
+	bool	gnu_emu = false;
 
  	if ((progname = strrchr(argv[0], '/')) == NULL)
  		progname = argv[0];
  	else
  		progname++;
 
+	/*
+	 * GNU coreutils has a number of programs named *sum. These produce
+	 * similar results to the BSD version, but in a different format,
+	 * similar to BSD's -r flag. We install links to this program with
+	 * ending 'sum' to provide this compatibility. Check here to see if the
+	 * name of the program ends in 'sum', set the flag and drop the 'sum' so
+	 * the digest lookup works. Also, make -t a nop when running in this mode
+	 * since that means 'text file' there (though it's a nop in coreutils
+	 * on unix-like systems). The -c flag conflicts, so it's just disabled
+	 * in this mode (though in the future it might be implemented).
+	 */
+	len = strlen(progname);
+	if (len > 3 && strcmp(progname + len - 3, "sum") == 0) {
+		progname[len - 3] = '\0';
+		rflag = 1;
+		gnu_emu = true;
+	}
+
  	for (digest = 0; digest < sizeof(Algorithm)/sizeof(*Algorithm); digest++)
  		if (strcasecmp(Algorithm[digest].progname, progname) == 0)
  			break;
@@ -195,9 +214,13 @@ main(int argc, char *argv[])
 	checkAgainst = NULL;
 	checksFailed = 0;
 	skip = 0;
-	while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1)
+	while ((ch = getopt(argc, argv, "bc:pqrs:tx")) != -1)
 		switch (ch) {
+		case 'b':
+			break;
 		case 'c':
+			if (gnu_emu)
+				errx(1, "-c check option not supported");
 			checkAgainst = optarg;
 			break;
 		case 'p':
@@ -214,8 +237,10 @@ main(int argc, char *argv[])
 			string = optarg;
 			break;
 		case 't':
-			MDTimeTrial(&Algorithm[digest]);
-			skip = 1;
+			if (!gnu_emu) {
+				MDTimeTrial(&Algorithm[digest]);
+				skip = 1;
+			} /* else: text mode is a nop */
 			break;
 		case 'x':
 			MDTestSuite(&Algorithm[digest]);
@@ -348,7 +373,7 @@ MDTimeTrial(const Algorithm_t *alg)
 	printf(" done\n");
 	printf("Digest = %s", p);
 	printf("\nTime = %f seconds\n", seconds);
-	printf("Speed = %f MiB/second\n", (float) TEST_BLOCK_LEN * 
+	printf("Speed = %f MiB/second\n", (float) TEST_BLOCK_LEN *
 		(float) TEST_BLOCK_COUNT / seconds / (1 << 20));
 }
 /*


More information about the dev-commits-src-all mailing list