]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cksum: ensure appropriate "binary" mode with --untagged
authorPádraig Brady <P@draigBrady.com>
Wed, 6 Mar 2024 21:54:02 +0000 (21:54 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 6 Mar 2024 21:54:02 +0000 (21:54 +0000)
* src/digest.c (main): If --binary was enabled with a previous --tag,
then reset the binary mode to auto select if --untagged then specified.
* tests/cksum/cksum-a.sh: Add a test case.

src/digest.c
tests/cksum/cksum-a.sh

index 96b811b6c8d987e231ec531a81a15c44d5155061..1a4cfd1fbfead794ea673d7cfd0ae02ec9b3006b 100644 (file)
 #include "xdectoint.h"
 #include "xstrtol.h"
 
+#ifndef HASH_ALGO_CKSUM
+# define HASH_ALGO_CKSUM 0
+#endif
+
 #if HASH_ALGO_SUM || HASH_ALGO_CKSUM
 # include "sum.h"
 #endif
@@ -1346,11 +1350,7 @@ main (int argc, char **argv)
   int opt;
   bool ok = true;
   int binary = -1;
-#if HASH_ALGO_CKSUM
-  bool prefix_tag = true;
-#else
-  bool prefix_tag = false;
-#endif
+  int prefix_tag = -1;
 
   /* Setting values of global variables.  */
   initialize_main (&argc, &argv);
@@ -1438,11 +1438,13 @@ main (int argc, char **argv)
         raw_digest = true;
         break;
       case UNTAG_OPTION:
-        prefix_tag = false;
+        if (prefix_tag == 1)
+          binary = -1;
+        prefix_tag = 0;
         break;
 # endif
       case TAG_OPTION:
-        prefix_tag = true;
+        prefix_tag = 1;
         binary = 1;
         break;
       case 'z':
@@ -1518,6 +1520,9 @@ main (int argc, char **argv)
    }
 #endif
 
+  if (prefix_tag == -1)
+    prefix_tag = HASH_ALGO_CKSUM;
+
   if (prefix_tag && !binary)
    {
      /* This could be supported in a backwards compatible way
index f01e78a3a7209cd97e6cd782690ba85ab0a36ce1..2933940c6beb02199039a1cd5e21ec8a81b6025c 100755 (executable)
@@ -58,4 +58,13 @@ returns_ 1 cksum -a bsd --check </dev/null || fail=1
 # Ensure abbreviations not supported for algorithm selection
 returns_ 1 cksum -a sha22 </dev/null || fail=1
 
+# Ensure --tag -> --untagged transition resets binary indicator
+cksum --tag --untagged -a md5 /dev/null >out-1 || fail=1
+# --binary ignored in this edge case
+cksum --binary --tag --untagged -a md5 /dev/null >out-2 || fail=1
+# base case for comparison
+cksum --untagged -a md5 /dev/null >out || fail=1
+compare out out-1 || fail=1
+compare out out-2 || fail=1
+
 Exit $fail