From 2e55bedc45e836899a18ea7f4a488f50597afad5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 21 Sep 2024 19:31:16 +0200 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- fmt.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'fmt.c') diff --git a/fmt.c b/fmt.c index 4c931dd..7b73ed7 100644 --- a/fmt.c +++ b/fmt.c @@ -187,3 +187,19 @@ humanbytespersecond(double bytes_per_second, char *buf) return buf; } + + +const char * +exact_and_human_size(off_t bytes, char *buf, int with_unit) +{ + char *p = buf; + p += sprintf(p, "%ji%s", (intmax_t)bytes, !with_unit ? "" : bytes == 1 ? " byte" : " bytes"); + if (bytes >= 1024) { + p = stpcpy(p, " ("); + humansize1000(bytes, p); + p = stpcpy(strchr(p, '\0'), ", "); + humansize1024(bytes, p); + p = stpcpy(strchr(p, '\0'), ")"); + } + return buf; +} -- cgit v1.2.3-70-g09d2