aboutsummaryrefslogtreecommitdiffstats
path: root/lib/string_helpers.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:28 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:28 -0500
commit818099574b04c5301eacbbcd441022b353a65466 (patch)
tree77b3645b375105cb0389df2b4ea5ffa90329f7f8 /lib/string_helpers.c
parent802ea9d8645d33d24b7b4cd4537c14f3e698bde0 (diff)
parent6016daed58ee482a2f7684e93342e89139cf4419 (diff)
Merge branch 'akpm' (patches from Andrew)
Merge third set of updates from Andrew Morton: - the rest of MM [ This includes getting rid of the numa hinting bits, in favor of just generic protnone logic. Yay. - Linus ] - core kernel - procfs - some of lib/ (lots of lib/ material this time) * emailed patches from Andrew Morton <akpm@linux-foundation.org>: (104 commits) lib/lcm.c: replace include lib/percpu_ida.c: remove redundant includes lib/strncpy_from_user.c: replace module.h include lib/stmp_device.c: replace module.h include lib/sort.c: move include inside #if 0 lib/show_mem.c: remove redundant include lib/radix-tree.c: change to simpler include lib/plist.c: remove redundant include lib/nlattr.c: remove redundant include lib/kobject_uevent.c: remove redundant include lib/llist.c: remove redundant include lib/md5.c: simplify include lib/list_sort.c: rearrange includes lib/genalloc.c: remove redundant include lib/idr.c: remove redundant include lib/halfmd4.c: simplify includes lib/dynamic_queue_limits.c: simplify includes lib/sort.c: use simpler includes lib/interval_tree.c: simplify includes hexdump: make it return number of bytes placed in buffer ...
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