I'm working on consolidating digest algorithms
(interfaced through cksum --algorithm),
and I noticed this inconsistency in sum(1),
which seems appropriate to address before consolidation
(see attached).

cheers,
Pádraig
>From 482b0a8871909e2a70f00c2b26e24910ab66b042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Sun, 29 Aug 2021 20:57:33 +0100
Subject: [PATCH] sum: always output a file name if one passed

Adjust to output the file name if any name parameter is passed.
This is consistent with sum -s, cksum, and sum implementations
on other platforms.  This should not cause significant compat
issues, as multiple fields are already output, and so already
need to be parsed.

* src/sum.c (bsd_sum_file): Output the file name
if any name parameter is passed.
* tests/misc/sum.pl: Adjust accordingly.
* doc/coreutils.texi (sum invocation): Likewise.
* NEWS: Mention the change in behavior.
---
 NEWS               |  2 ++
 doc/coreutils.texi |  6 ++----
 src/sum.c          |  4 ++--
 tests/misc/sum.pl  | 18 +++++++++---------
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/NEWS b/NEWS
index ddec56bdf..6571bebea 100644
--- a/NEWS
+++ b/NEWS
@@ -79,6 +79,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   stat will use decomposed (major,minor) device numbers in its default format.
   This is less ambiguous, and more consistent with ls.
 
+  sum [-r] will output a file name, even if only a single name is passed.
+
 ** New Features
 
   expr and factor now support bignums on all platforms.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index a435ed63e..5c12298cf 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3887,10 +3887,8 @@ sum [@var{option}]@dots{} [@var{file}]@dots{}
 @end example
 
 @command{sum} prints the checksum for each @var{file} followed by the
-number of blocks in the file (rounded up).  If more than one @var{file}
-is given, file names are also printed (by default).  (With the
-@option{--sysv} option, corresponding file names are printed when there is
-at least one file argument.)
+number of blocks in the file (rounded up).  If at least one @var{file}
+is given, file names are also printed.
 
 By default, GNU @command{sum} computes checksums using an algorithm
 compatible with BSD @command{sum} and prints file sizes in units of
diff --git a/src/sum.c b/src/sum.c
index c17af3f6b..018623d47 100644
--- a/src/sum.c
+++ b/src/sum.c
@@ -80,7 +80,7 @@ Print checksum and block counts for each FILE.\n\
 
 /* Calculate and print the rotated checksum and the size in 1K blocks
    of file FILE, or of the standard input if FILE is "-".
-   If PRINT_NAME is >1, print FILE next to the checksum and size.
+   If PRINT_NAME is >0, print FILE next to the checksum and size.
    The checksum varies depending on sizeof (int).
    Return true if successful.  */
 
@@ -135,7 +135,7 @@ bsd_sum_file (char const *file, int print_name)
 
   printf ("%05d %5s", checksum,
           human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));
-  if (print_name > 1)
+  if (print_name)
     printf (" %s", file);
   putchar ('\n');
 
diff --git a/tests/misc/sum.pl b/tests/misc/sum.pl
index 0b737c4e6..0b0f88189 100755
--- a/tests/misc/sum.pl
+++ b/tests/misc/sum.pl
@@ -28,18 +28,18 @@ my $in_2k = 'b' x 2048;
 
 my @Tests =
     (
-     ['1', {IN=> {f=> ''}},	{OUT=>"00000     0\n"}],
-     ['2', {IN=> {f=> 'a'}},	{OUT=>"00097     1\n"}],
-     ['3', {IN=> {f=> 'abc'}},	{OUT=>"16556     1\n"}],
-     ['4', {IN=> {f=> 'message digest'}}, {OUT=>"26423     1\n"}],
-     ['5', {IN=> {f=> 'abcdefghijklmnopqrstuvwxyz'}}, {OUT=>"53553     1\n"}],
+     ['1', {IN=> {f=> ''}},	{OUT=>"00000     0 f\n"}],
+     ['2', {IN=> {f=> 'a'}},	{OUT=>"00097     1 f\n"}],
+     ['3', {IN=> {f=> 'abc'}},	{OUT=>"16556     1 f\n"}],
+     ['4', {IN=> {f=> 'message digest'}}, {OUT=>"26423     1 f\n"}],
+     ['5', {IN=> {f=> 'abcdefghijklmnopqrstuvwxyz'}}, {OUT=>"53553     1 f\n"}],
      ['6', {IN=> {f=> join ('', 'A'..'Z', 'a'..'z', '0'..'9')}},
-                                {OUT=>"25587     1\n"}],
-     ['7', {IN=> {f=> '1234567890' x 8}}, {OUT=>"21845     1\n"}],
+                                {OUT=>"25587     1 f\n"}],
+     ['7', {IN=> {f=> '1234567890' x 8}}, {OUT=>"21845     1 f\n"}],
 
-     ['a-r-1k', '-r', {IN=> {f=> $in_1k}}, {OUT=>"65409     1\n"}],
+     ['a-r-1k', '-r', {IN=> {f=> $in_1k}}, {OUT=>"65409     1 f\n"}],
      ['a-s-1k', '-s', {IN=> {f=> $in_1k}}, {OUT=>"33793 2 f\n"}],
-     ['b-r-2k', '-r', {IN=> {f=> $in_2k}}, {OUT=>"65223     2\n"}],
+     ['b-r-2k', '-r', {IN=> {f=> $in_2k}}, {OUT=>"65223     2 f\n"}],
      ['b-s-2k', '-s', {IN=> {f=> $in_2k}}, {OUT=>"4099 4 f\n"}],
 
      ['1s', '-s', {IN=> {f=> ''}},	{OUT=>"0 0 f\n"}],
-- 
2.26.2

Reply via email to