blob: f6a2a3d117d5857143ccc92508d370d8c32c64a0 [file] [log] [blame]
Arnaldo Carvalho de Melo58db1d62017-04-19 16:05:56 -03001#include "units.h"
2#include <inttypes.h>
3#include <linux/kernel.h>
4#include <linux/time64.h>
5
6unsigned long convert_unit(unsigned long value, char *unit)
7{
8 *unit = ' ';
9
10 if (value > 1000) {
11 value /= 1000;
12 *unit = 'K';
13 }
14
15 if (value > 1000) {
16 value /= 1000;
17 *unit = 'M';
18 }
19
20 if (value > 1000) {
21 value /= 1000;
22 *unit = 'G';
23 }
24
25 return value;
26}
27
28int unit_number__scnprintf(char *buf, size_t size, u64 n)
29{
30 char unit[4] = "BKMG";
31 int i = 0;
32
33 while (((n / 1024) > 1) && (i < 3)) {
34 n /= 1024;
35 i++;
36 }
37
38 return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
39}