aboutsummaryrefslogtreecommitdiffstats
path: root/lib/string_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string_helpers.c')
-rw-r--r--lib/string_helpers.c26
1 files changed, 11 insertions, 15 deletions
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 58b78ba57439..8f8c4417f228 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -20,19 +20,18 @@
20 * @len: length of buffer 20 * @len: length of buffer
21 * 21 *
22 * This function returns a string formatted to 3 significant figures 22 * This function returns a string formatted to 3 significant figures
23 * giving the size in the required units. Returns 0 on success or 23 * giving the size in the required units. @buf should have room for
24 * error on failure. @buf is always zero terminated. 24 * at least 9 bytes and will always be zero terminated.
25 * 25 *
26 */ 26 */
27int string_get_size(u64 size, const enum string_size_units units, 27void string_get_size(u64 size, const enum string_size_units units,
28 char *buf, int len) 28 char *buf, int len)
29{ 29{
30 static const char *const units_10[] = { 30 static const char *const units_10[] = {
31 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL 31 "B", "kB", "MB", "GB", "TB", "PB", "EB"
32 }; 32 };
33 static const char *const units_2[] = { 33 static const char *const units_2[] = {
34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", 34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
35 NULL
36 }; 35 };
37 static const char *const *const units_str[] = { 36 static const char *const *const units_str[] = {
38 [STRING_UNITS_10] = units_10, 37 [STRING_UNITS_10] = units_10,
@@ -43,13 +42,13 @@ int string_get_size(u64 size, const enum string_size_units units,
43 [STRING_UNITS_2] = 1024, 42 [STRING_UNITS_2] = 1024,
44 }; 43 };
45 int i, j; 44 int i, j;
46 u64 remainder = 0, sf_cap; 45 u32 remainder = 0, sf_cap;
47 char tmp[8]; 46 char tmp[8];
48 47
49 tmp[0] = '\0'; 48 tmp[0] = '\0';
50 i = 0; 49 i = 0;
51 if (size >= divisor[units]) { 50 if (size >= divisor[units]) {
52 while (size >= divisor[units] && units_str[units][i]) { 51 while (size >= divisor[units]) {
53 remainder = do_div(size, divisor[units]); 52 remainder = do_div(size, divisor[units]);
54 i++; 53 i++;
55 } 54 }
@@ -60,17 +59,14 @@ int string_get_size(u64 size, const enum string_size_units units,
60 59
61 if (j) { 60 if (j) {
62 remainder *= 1000; 61 remainder *= 1000;
63 do_div(remainder, divisor[units]); 62 remainder /= divisor[units];
64 snprintf(tmp, sizeof(tmp), ".%03lld", 63 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
65 (unsigned long long)remainder);
66 tmp[j+1] = '\0'; 64 tmp[j+1] = '\0';
67 } 65 }
68 } 66 }
69 67
70 snprintf(buf, len, "%lld%s %s", (unsigned long long)size, 68 snprintf(buf, len, "%u%s %s", (u32)size,
71 tmp, units_str[units][i]); 69 tmp, units_str[units][i]);
72
73 return 0;
74} 70}
75EXPORT_SYMBOL(string_get_size); 71EXPORT_SYMBOL(string_get_size);
76 72