From 116ac656e78ab6c6b3b005ec8c994e74812a4043 Mon Sep 17 00:00:00 2001 From: Paul Eggert 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 b4e3cf83ad..816025255a 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 3765a8f65a..549762aba5 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.47.2