From dc1d7e3fae0effd6409a29f38bbaf355cd7bdb8a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 15 May 2026 22:46:50 +0300 Subject: [PATCH] ls: Print `total` field with -l --- userspace/programs/ls/main.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/userspace/programs/ls/main.cpp b/userspace/programs/ls/main.cpp index 9d037492..02a9a6c0 100644 --- a/userspace/programs/ls/main.cpp +++ b/userspace/programs/ls/main.cpp @@ -185,9 +185,14 @@ int list_directory(const BAN::String& path, Config config) return 2; } + const bool is_directory = S_ISDIR(st.st_mode); + + const size_t block_size = st.st_blksize; + size_t blocks_used = 0; + int ret = 0; - if (!S_ISDIR(st.st_mode)) + if (!is_directory) { MUST(entries.emplace_back(path, st, BAN::String())); if (S_ISLNK(st.st_mode)) @@ -231,6 +236,8 @@ int list_directory(const BAN::String& path, Config config) continue; } + blocks_used += st.st_blocks; + MUST(entries.emplace_back(BAN::StringView(dirent->d_name), st, BAN::String())); if (S_ISLNK(st.st_mode)) { @@ -353,6 +360,14 @@ int list_directory(const BAN::String& path, Config config) MUST(full_entries.push_back(BAN::move(full_entry))); } + if (is_directory) + { + if (config.human_readable) + printf("total: %s\n", build_size_string(blocks_used * block_size, config).data()); + else + printf("total: %zu\n", blocks_used); + } + for (const auto& full_entry : full_entries) printf("%*s %*s %*s %*s %*s %*s %*s %*s %s\n", (int)max_entry.access.size(), full_entry.access.data(),