Subject: [patch] md5sum --digest-only

From: Michael Vogt <[email-masked]>
Date: Mon, 6 Mar 2017 08:55:59 +0100

Hi,

this is a bit of a RFC (anlong with a patch). I would like to have an
option in md5sum (and friends) that displays only the digest without
the filename. The use-case is mostly package maintainer scripts. They
often need the md5sum to e.g. check if a conffile has changed and use
patterns like:

  `md5sum /etc/papersize 2> /dev/null | awk '{print $1}'`
  `md5sum /etc/lsb-release | cut -d" " -f1`
  $(md5sum /etc/networks | sed -e 's/ .*//')

Having a --digest-only option in md5sum would make this a bit more
uniform.

Attached is a small patch, if the suggestion gets accepted I'm happy
to flesh it out more and write the man-page entry and tests.

Cheers,
 Michael
diff --git a/src/md5sum.c b/src/md5sum.c
index e58a68e..17cbde0 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -154,6 +154,9 @@ static bool quiet = false;
    improperly formatted. */
 static bool strict = false;
 
+/* With --digest-only show only the digest, not the filename */
+static bool digest_only = false;
+
 /* Whether a BSD reversed format checksum is detected.  */
 static int bsd_reversed = -1;
 
@@ -197,7 +200,8 @@ enum
   STATUS_OPTION,
   QUIET_OPTION,
   STRICT_OPTION,
-  TAG_OPTION
+  TAG_OPTION,
+  DIGEST_ONLY_OPTION
 };
 
 static struct option const long_options[] =
@@ -214,6 +218,7 @@ static struct option const long_options[] =
   { "warn", no_argument, NULL, 'w' },
   { "strict", no_argument, NULL, STRICT_OPTION },
   { "tag", no_argument, NULL, TAG_OPTION },
+  { "digest-only", no_argument, NULL, DIGEST_ONLY_OPTION },
   { GETOPT_HELP_OPTION_DECL },
   { GETOPT_VERSION_OPTION_DECL },
   { NULL, 0, NULL, 0 }
@@ -541,6 +546,9 @@ hex_digits (unsigned char const *s)
 static void
 print_filename (char const *file, bool escape)
 {
+  if (digest_only)
+    return;
+
   if (! escape)
     {
       fputs (file, stdout);
@@ -931,6 +939,9 @@ main (int argc, char **argv)
         prefix_tag = true;
         binary = 1;
         break;
+      case DIGEST_ONLY_OPTION:
+        digest_only = true;
+        break;
       case_GETOPT_HELP_CHAR;
       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
       default: