From d9b78a232948124c1a1421005e5d3bf5de758591 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Sun, 12 Sep 2021 16:57:12 +0100 Subject: [PATCH] cksum: support --zero in default mode * 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 01549ecffb..116d23f800 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 e059965ff3..6310c71092 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 2150b487dd..e841bbb842 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 { @@ -933,7 +934,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 '\\' @@ -1298,7 +1300,7 @@ main (int argc, char **argv) binary = 1; break; case 'z': - delim = '\0'; + digest_delim = '\0'; break; #endif #if HASH_ALGO_SUM @@ -1350,9 +1352,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}")); @@ -1377,7 +1376,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")); @@ -1457,7 +1456,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 4e1efe9e11..3c5e687db5 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 ca68f750a2..1681697aba 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 7d95b69a44..8c4b32551b 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