Files
sbase/libutil/human.c
T

26 lines
476 B
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
2014-10-18 21:25:00 +00:00
#include <stdio.h>
#include <string.h>
2015-04-28 12:23:25 +03:00
#include <stdint.h>
2014-10-18 21:25:00 +00:00
#include "../util.h"
char *
humansize(off_t n)
2014-10-18 21:25:00 +00:00
{
static char buf[16];
2014-10-19 09:48:04 +00:00
const char postfixes[] = "BKMGTPE";
double size;
int i;
2014-10-18 21:25:00 +00:00
for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
size /= 1024;
2014-10-18 21:25:00 +00:00
if (!i)
snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n);
2014-10-18 21:25:00 +00:00
else
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
2014-10-18 21:25:00 +00:00
return buf;
}