sbase/libutil/human.c
Dionysis Grigoropoulos 2d6cde1862 humansize: Use uintmax_t for size
du(1) breaks on 32-bit size_t for files greater than 4G.
2015-04-28 11:36:58 +01:00

26 lines
469 B
C

/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "../util.h"
char *
humansize(uintmax_t n)
{
static char buf[16];
const char postfixes[] = "BKMGTPE";
double size;
int i;
for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
size /= 1024;
if (!i)
snprintf(buf, sizeof(buf), "%ju", n);
else
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
return buf;
}