Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 117711
b: refs/heads/master
c: a865959
h: refs/heads/master
i:
  117709: 392b76e
  117707: a1beceb
  117703: 7f0e516
  117695: 5070920
v: v3
  • Loading branch information
H. Peter Anvin authored and James Bottomley committed Oct 23, 2008
1 parent 929014f commit 7784d38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 3fe68cc152fc7cc6a763c692544a0ab71926c800
refs/heads/master: a8659597bf744b0f8d2560e2a734b5c941569e0e
34 changes: 19 additions & 15 deletions trunk/lib/string_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
int string_get_size(u64 size, const enum string_size_units units,
char *buf, int len)
{
const char *units_10[] = { "B", "KB", "MB", "GB", "TB", "PB",
const char *units_10[] = { "B", "kB", "MB", "GB", "TB", "PB",
"EB", "ZB", "YB", NULL};
const char *units_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB",
"EiB", "ZiB", "YiB", NULL };
const char **units_str[] = {
[STRING_UNITS_10] = units_10,
[STRING_UNITS_2] = units_2,
};
const int divisor[] = {
const unsigned int divisor[] = {
[STRING_UNITS_10] = 1000,
[STRING_UNITS_2] = 1024,
};
Expand All @@ -40,23 +40,27 @@ int string_get_size(u64 size, const enum string_size_units units,
char tmp[8];

tmp[0] = '\0';
i = 0;
if (size >= divisor[units]) {
while (size >= divisor[units] && units_str[units][i]) {
remainder = do_div(size, divisor[units]);
i++;
}

for (i = 0; size > divisor[units] && units_str[units][i]; i++)
remainder = do_div(size, divisor[units]);
sf_cap = size;
for (j = 0; sf_cap*10 < 1000; j++)
sf_cap *= 10;

sf_cap = size;
for (j = 0; sf_cap*10 < 1000; j++)
sf_cap *= 10;

if (j) {
remainder *= 1000;
do_div(remainder, divisor[units]);
snprintf(tmp, sizeof(tmp), ".%03lld",
(unsigned long long)remainder);
tmp[j+1] = '\0';
if (j) {
remainder *= 1000;
do_div(remainder, divisor[units]);
snprintf(tmp, sizeof(tmp), ".%03lld",
(unsigned long long)remainder);
tmp[j+1] = '\0';
}
}

snprintf(buf, len, "%lld%s%s", (unsigned long long)size,
snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
tmp, units_str[units][i]);

return 0;
Expand Down

0 comments on commit 7784d38

Please sign in to comment.