2014-11-13 18:54:28 +00:00
|
|
|
/* 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 09:23:25 +00:00
|
|
|
#include <stdint.h>
|
2014-10-18 21:25:00 +00:00
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
char *
|
2015-04-28 10:48:05 +00:00
|
|
|
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";
|
2015-04-24 22:27:20 +00:00
|
|
|
double size;
|
|
|
|
int i;
|
2014-10-18 21:25:00 +00:00
|
|
|
|
2015-04-24 22:27:20 +00:00
|
|
|
for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
|
|
|
|
size /= 1024;
|
2014-10-18 21:25:00 +00:00
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!i)
|
2015-04-28 10:48:05 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n);
|
2014-10-18 21:25:00 +00:00
|
|
|
else
|
2015-04-24 22:27:20 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
|
|
|
|
|
2014-10-18 21:25:00 +00:00
|
|
|
return buf;
|
|
|
|
}
|