]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
stat: -c %s now prints unsigned
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 22 Jul 2022 20:50:31 +0000 (13:50 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 22 Jul 2022 20:51:13 +0000 (13:51 -0700)
* 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
src/stat.c

diff --git a/NEWS b/NEWS
index b4e3cf83ad26e3b98cd69ae66308034bee42dabd..816025255a8027d6d1cea55ec4c7cc1526aa9ddd 100644 (file)
--- 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
index 3765a8f65a433e7213a7ce2119636ad9c6584863..549762aba5eb471b83b4609c1e2ba68fc39383c5 100644 (file)
@@ -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')