coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 12/12] cksum: support --zero in default mode


From: Pádraig Brady
Subject: [PATCH 12/12] cksum: support --zero in default mode
Date: Sun, 12 Sep 2021 19:14:06 +0100

* src/cksum.h: Thread DELIM through the output functions.
* src/digest.c: Likewise.
* src/sum.c: Likewise.
* src/sum.h: Likewise.
* src/cksum.c: Likewise. Also adjust check to allow -z
with traditional output modes.  Also ajust the global variable
name to avoid shadowing warnings.
* tests/misc/cksum-a.sh: Adjust accordingly.
---
 src/cksum.c           |  5 +++--
 src/cksum.h           |  3 ++-
 src/digest.c          | 19 +++++++++----------
 src/sum.c             | 10 ++++++----
 src/sum.h             |  6 ++++--
 tests/misc/cksum-a.sh |  1 -
 6 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/cksum.c b/src/cksum.c
index 01549ecff..116d23f80 100644
--- a/src/cksum.c
+++ b/src/cksum.c
@@ -285,13 +285,14 @@ crc_sum_stream (FILE *stream, void *resstream, uintmax_t 
*length)
 
 void
 output_crc (char const *file, int binary_file, void const *digest,
-            bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED)
+            bool tagged, unsigned char delim, bool args _GL_UNUSED,
+            uintmax_t length _GL_UNUSED)
 {
   char length_buf[INT_BUFSIZE_BOUND (uintmax_t)];
   printf ("%u %s", *(unsigned int *)digest, umaxtostr (length, length_buf));
   if (args)
     printf (" %s", file);
-  putchar ('\n');
+  putchar (delim);
 }
 
 #endif /* !CRCTAB */
diff --git a/src/cksum.h b/src/cksum.h
index e059965ff..6310c7109 100644
--- a/src/cksum.h
+++ b/src/cksum.h
@@ -8,7 +8,8 @@ crc_sum_stream (FILE *stream, void *resstream, uintmax_t 
*length);
 
 extern void
 output_crc (char const *file, int binary_file, void const *digest,
-            bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED);
+            bool tagged, unsigned char delim, bool args _GL_UNUSED,
+            uintmax_t length _GL_UNUSED);
 
 extern bool
 cksum_pclmul (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out);
diff --git a/src/digest.c b/src/digest.c
index aa672c916..3931c8ae8 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -165,7 +165,8 @@
 #if !HASH_ALGO_SUM
 static void
 output_file (char const *file, int binary_file, void const *digest,
-             bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED);
+             bool tagged, unsigned char delim, bool args _GL_UNUSED,
+             uintmax_t length _GL_UNUSED);
 #endif
 
 /* True if any of the files read were the standard input. */
@@ -199,7 +200,7 @@ static bool strict = false;
 static int bsd_reversed = -1;
 
 /* line delimiter.  */
-static unsigned char delim = '\n';
+static unsigned char digest_delim = '\n';
 
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
 # define BLAKE2B_MAX_LEN BLAKE2B_OUTBYTES
@@ -207,7 +208,7 @@ static uintmax_t digest_length;
 #endif /* HASH_ALGO_BLAKE2 */
 
 typedef void (*digest_output_fn)(char const*, int, void const*,
-                                 bool, bool, uintmax_t);
+                                 bool, unsigned char, bool, uintmax_t);
 #if HASH_ALGO_SUM
 enum Algorithm
 {
@@ -932,7 +933,8 @@ digest_file (char const *filename, int *binary, unsigned 
char *bin_result,
 #if !HASH_ALGO_SUM
 static void
 output_file (char const *file, int binary_file, void const *digest,
-             bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED)
+             bool tagged, unsigned char delim, bool args _GL_UNUSED,
+             uintmax_t length _GL_UNUSED)
 {
   unsigned char const *bin_buffer = digest;
   /* We don't really need to escape, and hence detect, the '\\'
@@ -1297,7 +1299,7 @@ main (int argc, char **argv)
         binary = 1;
         break;
       case 'z':
-        delim = '\0';
+        digest_delim = '\0';
         break;
 #endif
 #if HASH_ALGO_SUM
@@ -1349,9 +1351,6 @@ main (int argc, char **argv)
     case bsd:
     case sysv:
     case crc:
-        if (delim != '\n')
-          die (EXIT_FAILURE, 0,
-              _("--zero is not supported with --algorithm={bsd,sysv,crc}"));
         if (prefix_tag)
           die (EXIT_FAILURE, 0,
               _("--tag is not supported with --algorithm={bsd,sysv,crc}"));
@@ -1376,7 +1375,7 @@ main (int argc, char **argv)
      usage (EXIT_FAILURE);
    }
 
-  if (delim != '\n' && do_check)
+  if (digest_delim != '\n' && do_check)
     {
       error (0, 0, _("the --zero option is not supported when "
                      "verifying checksums"));
@@ -1456,7 +1455,7 @@ main (int argc, char **argv)
           else
             {
               DIGEST_OUT (file, binary_file, bin_buffer, prefix_tag,
-                          optind != argc, length);
+                          digest_delim, optind != argc, length);
             }
         }
     }
diff --git a/src/sum.c b/src/sum.c
index 4e1efe9e1..3c5e687db 100644
--- a/src/sum.c
+++ b/src/sum.c
@@ -184,7 +184,8 @@ cleanup_buffer:
 
 void
 output_bsd (char const *file, int binary_file, void const *digest,
-            bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED)
+            bool tagged, unsigned char delim, bool args _GL_UNUSED,
+            uintmax_t length _GL_UNUSED)
 {
 
   char hbuf[LONGEST_HUMAN_READABLE + 1];
@@ -192,7 +193,7 @@ output_bsd (char const *file, int binary_file, void const 
*digest,
           human_readable (length, hbuf, human_ceiling, 1, 1024));
   if (args)
     printf (" %s", file);
-  putchar ('\n');
+  putchar (delim);
 }
 
 /* Print the checksum and size (in 512 byte blocks) to stdout.
@@ -200,7 +201,8 @@ output_bsd (char const *file, int binary_file, void const 
*digest,
 
 void
 output_sysv (char const *file, int binary_file, void const *digest,
-             bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED)
+             bool tagged, unsigned char delim, bool args _GL_UNUSED,
+             uintmax_t length _GL_UNUSED)
 {
 
   char hbuf[LONGEST_HUMAN_READABLE + 1];
@@ -208,5 +210,5 @@ output_sysv (char const *file, int binary_file, void const 
*digest,
           human_readable (length, hbuf, human_ceiling, 1, 512));
   if (args)
     printf (" %s", file);
-  putchar ('\n');
+  putchar (delim);
 }
diff --git a/src/sum.h b/src/sum.h
index ca68f750a..1681697ab 100644
--- a/src/sum.h
+++ b/src/sum.h
@@ -9,8 +9,10 @@ typedef int (*sumfn)(FILE *, void *, uintmax_t*);
 
 extern void
 output_bsd (char const *file, int binary_file, void const *digest,
-            bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED);
+            bool tagged, unsigned char delim, bool args _GL_UNUSED,
+            uintmax_t length _GL_UNUSED);
 
 extern void
 output_sysv (char const *file, int binary_file, void const *digest,
-             bool tagged, bool args _GL_UNUSED, uintmax_t length _GL_UNUSED);
+             bool tagged, unsigned char delim, bool args _GL_UNUSED,
+             uintmax_t length _GL_UNUSED);
diff --git a/tests/misc/cksum-a.sh b/tests/misc/cksum-a.sh
index c402f26e2..15fc94b7d 100755
--- a/tests/misc/cksum-a.sh
+++ b/tests/misc/cksum-a.sh
@@ -37,7 +37,6 @@ done
 compare out out-a || fail=1
 
 returns_ 1 cksum -a bsd --tag </dev/null
-returns_ 1 cksum -a bsd --zero </dev/null
 returns_ 1 cksum -a bsd --check </dev/null
 
 Exit $fail
-- 
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]