Subject: bug#56710: closed (Re: bug#56710: ls vs. stat display of st_size)

From: [email-masked] (GNU bug Tracking System)
Date: Fri, 22 Jul 2022 20:53:02 +0000

Your bug report

#56710: ls vs. stat display of st_size

which was filed against the coreutils package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
56710: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=56710
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#56710: ls vs. stat display of st_size
From: Paul Eggert <[email-masked]>
To: Andreas Schwab <[email-masked]>
Cc: [email-masked]
Date: Fri, 22 Jul 2022 13:52:33 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for reporting that. I installed the attached.

From 34a93b971dd68ab8ff96aa20bf2f39374ab3a443 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Fri, 22 Jul 2022 13:50:31 -0700
Subject: [PATCH] stat: -c %s now prints unsigned

* src/stat.c (unsigned_file_size): New static function,
copied from src/ls.c.
(print_stat): %s prints an unsigned value now (Bug#56710).
---
 NEWS       |  2 ++
 src/stat.c | 10 +++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index b4e3cf83a..816025255 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   reverting to the behavior in coreutils-9.0 and earlier.
   This behavior is now documented.
 
+  ’stat -c %s' now prints sizes as unsigned, consistent with 'ls'.
+
 ** New Features
 
   factor now accepts the --exponents (-h) option to print factors
diff --git a/src/stat.c b/src/stat.c
index 3765a8f65..549762aba 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -1492,6 +1492,14 @@ do_stat (char const *filename, char const *format,
 }
 #endif /* USE_STATX */
 
+/* POSIX requires 'ls' to print file sizes without a sign, even
+   when negative.  Be consistent with that.  */
+
+static uintmax_t
+unsigned_file_size (off_t size)
+{
+  return size + (size < 0) * ((uintmax_t) OFF_T_MAX - OFF_T_MIN + 1);
+}
 
 /* Print stat info.  Return zero upon success, nonzero upon failure.  */
 static bool
@@ -1575,7 +1583,7 @@ print_stat (char *pformat, size_t prefix_len, char mod, char m,
       fail |= out_mount_point (filename, pformat, prefix_len, statbuf);
       break;
     case 's':
-      out_int (pformat, prefix_len, statbuf->st_size);
+      out_uint (pformat, prefix_len, unsigned_file_size (statbuf->st_size));
       break;
     case 'r':
       if (mod == 'H')
-- 
2.37.1

Subject: ls vs. stat display of st_size
From: Andreas Schwab <[email-masked]>
To: [email-masked]
Date: Fri, 22 Jul 2022 22:09:49 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

$ ls -l /proc/kcore
-r-------- 1 root root 18446744000862892032 Jun 21 00:00 /proc/kcore
$ stat -c %s /proc/kcore
-72846659584

-- 
Andreas Schwab, [email-masked]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."