Rewrite to fix bug #1140 and make it slightly smaller.
This commit is contained in:
parent
f957c77910
commit
7cd0cfeab6
@ -28,35 +28,23 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
|
||||||
|
static char buffer[10];
|
||||||
|
static const char *suffixes[] = { "", "k", "M", "G", "T" };
|
||||||
|
|
||||||
const char *make_human_readable_str(unsigned long val, unsigned long hr)
|
const char *make_human_readable_str(unsigned long val, unsigned long hr)
|
||||||
{
|
{
|
||||||
int i=0;
|
int suffix, base;
|
||||||
static char str[10] = "\0";
|
|
||||||
static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
|
|
||||||
unsigned long divisor = 1;
|
|
||||||
|
|
||||||
if(val == 0)
|
for (suffix = 0, base = 1; suffix < 5; suffix++, base <<= 10) {
|
||||||
return("0");
|
if (val < (base << 10)) {
|
||||||
if(hr)
|
if (suffix && val < 10 * base)
|
||||||
snprintf(str, 9, "%ld", val/hr);
|
sprintf(buffer, "%lu.%lu%s", val / base,
|
||||||
else {
|
(val % base) * 10 / base, suffixes[suffix]);
|
||||||
while(val >= divisor && i <= 4) {
|
else
|
||||||
divisor=divisor<<10, i++;
|
sprintf(buffer, "%lu%s", val / base, suffixes[suffix]);
|
||||||
}
|
break;
|
||||||
divisor=divisor>>10, i--;
|
}
|
||||||
snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
|
|
||||||
}
|
}
|
||||||
return(str);
|
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* END CODE */
|
|
||||||
/*
|
|
||||||
Local Variables:
|
|
||||||
c-file-style: "linux"
|
|
||||||
c-basic-offset: 4
|
|
||||||
tab-width: 4
|
|
||||||
End:
|
|
||||||
*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user