aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPiotr Pawel Stefaniak <pstef@FreeBSD.org>2025-01-06 17:59:03 +0000
committerPiotr Pawel Stefaniak <pstef@FreeBSD.org>2025-01-16 16:00:17 +0000
commit82fa7f83b53b3c5b7f3a5c5560edc7caeeaa1029 (patch)
tree1be20e05f3572649cbc2b3fd426e7c543a312995
parente2012b81cb87793be589cffa0a89d9b230df7c93 (diff)
ls -lh: humanize the total
Before this change, the total printed on the first line was always in blocks. Now the long format with -h will print the "humanized number" instead. `ls -sh` never provided sizes in anything other than blocks, total or individual, and this commit doesn't change that. The total number of blocks is a sum of fts_statp->st_blocks, so it's multiplied by 512 and not by blocksize. $ ls -lh /usr/bin | head -n 2 total 442 MB -r-xr-xr-x 1 root wheel 70B Dec 20 21:06 CC Differential Revision: https://reviews.freebsd.org/D48329
-rw-r--r--bin/ls/ls.14
-rw-r--r--bin/ls/print.c9
2 files changed, 12 insertions, 1 deletions
diff --git a/bin/ls/ls.1 b/bin/ls/ls.1
index d86250b82db2..6aaa5025a6e1 100644
--- a/bin/ls/ls.1
+++ b/bin/ls/ls.1
@@ -539,6 +539,10 @@ which are listed as the directory's contents
and
.Pa ..
and other files which start with a dot, depending on other options).
+If the
+.Fl h
+option is given,
+the total size is displayed as the number of bytes.
.Pp
The default block size is 512 bytes.
The block size may be set with option
diff --git a/bin/ls/print.c b/bin/ls/print.c
index f651dea5de90..a504ec63dc26 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -210,7 +210,14 @@ printlong(const DISPLAY *dp)
if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
(f_longform || f_size)) {
- (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
+ if (!f_humanval)
+ (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
+ else {
+ (void)humanize_number(buf, 7 /* "1024 KB" */,
+ dp->btotal * 512, "B", HN_AUTOSCALE, HN_DECIMAL);
+
+ (void)printf("total %s\n", buf);
+ }
}
for (p = dp->list; p; p = p->fts_link) {