It would be nice to be able to support users who want to replace all
the *sum utilities with a script that did:

  exec cksum -a <algo> --untagged "$@"

To that end, it would be good to support all the options of the older utils.
This is a bit awkward with --base64 added in the last few months, as that
also added the short -b option.  However it's only a few months released,
so it's probably not too onerous to change.

The attached makes the change, which I'll augment with NEWS and tests/ upon 
agreement.

cheers,
Pádraig
From f985d90d001bea75c64031cd241d602cd9968dbe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Sat, 8 Jul 2023 15:14:12 +0100
Subject: [PATCH] cksum: support transparent emulation of older utils

Support -b, --binary, and -t, --text
to allow full emulation of older utilities with:
  exec cksum -a $algo --untagged "$@"

* src/digest.c: Change -b to mean --binary, not --base64 in all cases.
Accept -b and -t in all cases.  Keep --binary and --text undocumented
for cksum.
* tests/cksum/cksum-base64.pl: s/-b/--base64/.
---
 src/digest.c                | 22 +++++++++++-----------
 tests/cksum/cksum-base64.pl |  2 +-
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/digest.c b/src/digest.c
index bc8967e56..83f304e47 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -367,6 +367,7 @@ enum
   UNTAG_OPTION,
   DEBUG_PROGRAM_OPTION,
   RAW_OPTION,
+  BASE64_OPTION,
 };
 
 static struct option const long_options[] =
@@ -387,14 +388,13 @@ static struct option const long_options[] =
 
 # if HASH_ALGO_CKSUM
   { "algorithm", required_argument, nullptr, 'a'},
-  { "base64", no_argument, nullptr, 'b' },
+  { "base64", no_argument, nullptr, BASE64_OPTION },
   { "debug", no_argument, nullptr, DEBUG_PROGRAM_OPTION},
   { "raw", no_argument, nullptr, RAW_OPTION},
   { "untagged", no_argument, nullptr, UNTAG_OPTION },
-# else
+# endif
   { "binary", no_argument, nullptr, 'b' },
   { "text", no_argument, nullptr, 't' },
-# endif
 
 #else
   {"sysv", no_argument, nullptr, 's'},
@@ -445,7 +445,7 @@ Print or check %s (%d-bit) checksums.\n\
 \n\
 "), stdout);
         fputs (_("\
-  -b, --base64          emit base64-encoded digests, not hexadecimal\
+      --base64          emit base64-encoded digests, not hexadecimal\
 \n\
 "), stdout);
 #endif
@@ -1344,11 +1344,10 @@ main (int argc, char **argv)
   bool do_check = false;
   int opt;
   bool ok = true;
+  int binary = -1;
 #if HASH_ALGO_CKSUM
-  int binary = 1;
   bool prefix_tag = true;
 #else
-  int binary = -1;
   bool prefix_tag = false;
 #endif
 
@@ -1413,14 +1412,12 @@ main (int argc, char **argv)
         warn = false;
         quiet = false;
         break;
-# if !HASH_ALGO_CKSUM
       case 'b':
         binary = 1;
         break;
       case 't':
         binary = 0;
         break;
-# endif
       case 'w':
         status_only = false;
         warn = true;
@@ -1438,7 +1435,7 @@ main (int argc, char **argv)
         strict = true;
         break;
 # if HASH_ALGO_CKSUM
-      case 'b':
+      case BASE64_OPTION:
         base64_digest = true;
         break;
       case RAW_OPTION:
@@ -1546,14 +1543,12 @@ main (int argc, char **argv)
     }
 #endif
 
-#if !HASH_ALGO_CKSUM
   if (0 <= binary && do_check)
     {
       error (0, 0, _("the --binary and --text options are meaningless when "
                      "verifying checksums"));
       usage (EXIT_FAILURE);
     }
-#endif
 
   if (ignore_missing && !do_check)
     {
@@ -1591,8 +1586,13 @@ main (int argc, char **argv)
      usage (EXIT_FAILURE);
    }
 
+#if HASH_ALGO_CKSUM
+  if (binary < 0)
+    binary = 1;
+#else
   if (!O_BINARY && binary < 0)
     binary = 0;
+#endif
 
   char **operand_lim = argv + argc;
   if (optind == argc)
diff --git a/tests/cksum/cksum-base64.pl b/tests/cksum/cksum-base64.pl
index 4fec7a5d7..607d4a4a8 100755
--- a/tests/cksum/cksum-base64.pl
+++ b/tests/cksum/cksum-base64.pl
@@ -53,7 +53,7 @@ my @Tests =
   (
    # Ensure that each of the above works with -b:
    (map {my ($h,$v)= @$_; my $o=fmt $h,$v;
-         [$h, "-ba $h", {IN=>{f=>''}}, {OUT=>"$o\n"}]} @pairs),
+         [$h, "--base64 -a $h", {IN=>{f=>''}}, {OUT=>"$o\n"}]} @pairs),
 
    # For each that accepts --check, ensure that works with base64 digests:
    (map {my ($h,$v)= @$_; my $o=fmt $h,$v;
-- 
2.41.0

Reply via email to