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>
This commit is contained in:
parent
2b85ab880d
commit
5d2a98490e
5 changed files with 65 additions and 39 deletions
|
@ -27,6 +27,15 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
|
|||
}
|
||||
|
||||
extern void strmode(mode_t mode, char *str);
|
||||
extern char *size_to_human_string(uint64_t bytes);
|
||||
|
||||
/* Options for size_to_human_string() */
|
||||
enum
|
||||
{
|
||||
SIZE_SUFFIX_1LETTER = 0,
|
||||
SIZE_SUFFIX_3LETTER = 1,
|
||||
SIZE_SUFFIX_SPACE = 2
|
||||
};
|
||||
|
||||
extern char *size_to_human_string(int options, uint64_t bytes);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -517,7 +517,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
|
|||
if (asprintf(&p, "%jd", cxt->size) < 0)
|
||||
p = NULL;
|
||||
} else
|
||||
p = size_to_human_string(cxt->size);
|
||||
p = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size);
|
||||
if (p)
|
||||
tt_line_set_data(ln, col, p);
|
||||
}
|
||||
|
@ -572,7 +572,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
|
|||
|
||||
if (sysfs_read_u64(&cxt->sysfs,
|
||||
"queue/discard_granularity", &x) == 0)
|
||||
p = size_to_human_string(x);
|
||||
p = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
|
||||
}
|
||||
if (p)
|
||||
tt_line_set_data(ln, col, p);
|
||||
|
@ -585,7 +585,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
|
|||
|
||||
if (sysfs_read_u64(&cxt->sysfs,
|
||||
"queue/discard_max_bytes", &x) == 0)
|
||||
p = size_to_human_string(x);
|
||||
p = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
|
||||
}
|
||||
if (p)
|
||||
tt_line_set_data(ln, col, p);
|
||||
|
|
|
@ -426,7 +426,7 @@ static void add_tt_line(struct tt *tt, blkid_partition par)
|
|||
rc = asprintf(&str, "%ju", (uintmax_t)
|
||||
blkid_partition_get_size(par) << 9);
|
||||
else
|
||||
str = size_to_human_string(
|
||||
str = size_to_human_string(SIZE_SUFFIX_1LETTER,
|
||||
blkid_partition_get_size(par) << 9);
|
||||
break;
|
||||
case COL_NAME:
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
test_strutils: invalid size '-1' value
|
||||
0 : 0 : 0B
|
||||
1 : 1 : 1B
|
||||
123 : 123 : 123B
|
||||
18446744073709551615 : 18446744073709551615 : 16E
|
||||
1K : 1024 : 1K
|
||||
1KiB : 1024 : 1K
|
||||
1M : 1048576 : 1M
|
||||
1MiB : 1048576 : 1M
|
||||
1G : 1073741824 : 1G
|
||||
1GiB : 1073741824 : 1G
|
||||
1T : 1099511627776 : 1T
|
||||
1TiB : 1099511627776 : 1T
|
||||
1P : 1125899906842624 : 1P
|
||||
1PiB : 1125899906842624 : 1P
|
||||
1E : 1152921504606846976 : 1E
|
||||
1EiB : 1152921504606846976 : 1E
|
||||
1KB : 1000 : 1000B
|
||||
1MB : 1000000 : 976.6K
|
||||
1GB : 1000000000 : 953.7M
|
||||
1TB : 1000000000000 : 931.3G
|
||||
1PB : 1000000000000000 : 909.5T
|
||||
1EB : 1000000000000000000 : 888.2P
|
||||
0 : 0 : 0B : 0 B
|
||||
1 : 1 : 1B : 1 B
|
||||
123 : 123 : 123B : 123 B
|
||||
18446744073709551615 : 18446744073709551615 : 16E : 16 EiB
|
||||
1K : 1024 : 1K : 1 KiB
|
||||
1KiB : 1024 : 1K : 1 KiB
|
||||
1M : 1048576 : 1M : 1 MiB
|
||||
1MiB : 1048576 : 1M : 1 MiB
|
||||
1G : 1073741824 : 1G : 1 GiB
|
||||
1GiB : 1073741824 : 1G : 1 GiB
|
||||
1T : 1099511627776 : 1T : 1 TiB
|
||||
1TiB : 1099511627776 : 1T : 1 TiB
|
||||
1P : 1125899906842624 : 1P : 1 PiB
|
||||
1PiB : 1125899906842624 : 1P : 1 PiB
|
||||
1E : 1152921504606846976 : 1E : 1 EiB
|
||||
1EiB : 1152921504606846976 : 1E : 1 EiB
|
||||
1KB : 1000 : 1000B : 1000 B
|
||||
1MB : 1000000 : 976.6K : 976.6 KiB
|
||||
1GB : 1000000000 : 953.7M : 953.7 MiB
|
||||
1TB : 1000000000000 : 931.3G : 931.3 GiB
|
||||
1PB : 1000000000000000 : 909.5T : 909.5 TiB
|
||||
1EB : 1000000000000000000 : 888.2P : 888.2 PiB
|
||||
test_strutils: invalid size '' value
|
||||
test_strutils: invalid size ' ' value
|
||||
1 : 1 : 1B
|
||||
1 : 1 : 1B : 1 B
|
||||
test_strutils: invalid size '1 ' value
|
||||
0x0a : 10 : 10B
|
||||
0xff00 : 65280 : 63.8K
|
||||
0x80000000 : 2147483648 : 2G
|
||||
0x0a : 10 : 10B : 10 B
|
||||
0xff00 : 65280 : 63.8K : 63.8 KiB
|
||||
0x80000000 : 2147483648 : 2G : 2 GiB
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue