summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorFrancesco Cosoleto2011-05-26 15:17:25 +0200
committerKarel Zak2011-05-26 15:32:33 +0200
commit5d2a98490e87872214e6acdeac1031c0eb8cfc92 (patch)
treed2eaea544a6d8b662612e71395196c92bb6b5833 /lib/strutils.c
parentbuild-sys: use AUTOMAKE_OPTIONS = gnu (diff)
downloadkernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.tar.gz
kernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.tar.xz
kernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.zip
This adds a second parameter to size_to_human_string() to return a
string with a different format based on the following flags: SIZE_SUFFIX_1LETTER = "1K" SIZE_SUFFIX_3LETTER = "1KiB", SIZE_SUFFIX_SPACE = "1 KiB" or "1 K" [kzak@redhat.com: - rename flags to SIZE_SUFFIX_* format, - fix suffix[] buffer size - add 3 letter version to the test] Signed-off-by: Francesco Cosoleto <cosoleto@gmail.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index 21c58daea..07010f32c 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -12,6 +12,7 @@
#include <locale.h>
#include <string.h>
#include "c.h"
+#include "strutils.h"
static int do_scale_by_power (uintmax_t *x, int base, int power)
{
@@ -266,21 +267,34 @@ static int get_exp(uint64_t n)
return shft - 10;
}
-char *size_to_human_string(uint64_t bytes)
+char *size_to_human_string(int options, uint64_t bytes)
{
char buf[32];
int dec, exp;
uint64_t frac;
const char *letters = "BKMGTPE";
+ char suffix[sizeof(" KiB")], *psuf = suffix;
char c;
+ if (options & SIZE_SUFFIX_SPACE)
+ *psuf++ = ' ';
+
exp = get_exp(bytes);
c = *(letters + (exp ? exp / 10 : 0));
dec = exp ? bytes / (1ULL << exp) : bytes;
frac = exp ? bytes % (1ULL << exp) : 0;
- /* fprintf(stderr, "exp: %d, c: %c, dec: %d, frac: %jd\n",
- * exp, c, dec, frac);
+ *psuf++ = c;
+
+ if ((options & SIZE_SUFFIX_3LETTER) && (c != 'B')) {
+ *psuf++ = 'i';
+ *psuf++ = 'B';
+ }
+
+ *psuf = '\0';
+
+ /* fprintf(stderr, "exp: %d, unit: %c, dec: %d, frac: %jd\n",
+ * exp, suffix[0], dec, frac);
*/
if (frac) {
@@ -296,9 +310,9 @@ char *size_to_human_string(uint64_t bytes)
if (!dp || !*dp)
dp = ".";
- snprintf(buf, sizeof(buf), "%d%s%jd%c", dec, dp, frac, c);
+ snprintf(buf, sizeof(buf), "%d%s%jd%s", dec, dp, frac, suffix);
} else
- snprintf(buf, sizeof(buf), "%d%c", dec, c);
+ snprintf(buf, sizeof(buf), "%d%s", dec, suffix);
return strdup(buf);
}
@@ -309,7 +323,7 @@ char *size_to_human_string(uint64_t bytes)
int main(int argc, char *argv[])
{
uintmax_t size = 0;
- char *hum;
+ char *hum, *hum2;
if (argc < 2) {
fprintf(stderr, "usage: %s <number>[suffix]\n", argv[0]);
@@ -319,10 +333,13 @@ int main(int argc, char *argv[])
if (strtosize(argv[1], &size))
errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]);
- hum = size_to_human_string(size);
+ hum = size_to_human_string(SIZE_SUFFIX_1LETTER, size);
+ hum2 = size_to_human_string(SIZE_SUFFIX_3LETTER |
+ SIZE_SUFFIX_SPACE, size);
- printf("%25s : %20ju : %8s\n", argv[1], size, hum);
+ printf("%25s : %20ju : %8s : %12s\n", argv[1], size, hum, hum2);
free(hum);
+ free(hum2);
return EXIT_FAILURE;
}