diff options
author | Jason King <jason.brian.king@gmail.com> | 2017-07-27 15:24:01 +0000 |
---|---|---|
committer | Jason King <jason.brian.king@gmail.com> | 2017-07-27 15:24:01 +0000 |
commit | 22d7d8001dcaae5929f46b876953c0795c1ba892 (patch) | |
tree | 53bd40fe0c10ac46af9167eaf29ffefcfd6c5a0e /usr/src/lib/libzfs/common | |
parent | ad734d51547cf4bbcdc0baaef0bf8e9297afabb4 (diff) | |
download | illumos-joyent-jbk-nicenum.tar.gz |
640 number_to_scaled_sctring is duplicated in several commandsjbk-nicenum
Diffstat (limited to 'usr/src/lib/libzfs/common')
-rw-r--r-- | usr/src/lib/libzfs/common/libzfs_util.c | 38 |
1 files changed, 2 insertions, 36 deletions
diff --git a/usr/src/lib/libzfs/common/libzfs_util.c b/usr/src/lib/libzfs/common/libzfs_util.c index 365ece14e0..9bab674a6b 100644 --- a/usr/src/lib/libzfs/common/libzfs_util.c +++ b/usr/src/lib/libzfs/common/libzfs_util.c @@ -44,6 +44,7 @@ #include <sys/mnttab.h> #include <sys/mntent.h> #include <sys/types.h> +#include <libcmdutils.h> #include <libzfs.h> #include <libzfs_core.h> @@ -571,42 +572,7 @@ zfs_strdup(libzfs_handle_t *hdl, const char *str) void zfs_nicenum(uint64_t num, char *buf, size_t buflen) { - uint64_t n = num; - int index = 0; - char u; - - while (n >= 1024) { - n /= 1024; - index++; - } - - u = " KMGTPE"[index]; - - if (index == 0) { - (void) snprintf(buf, buflen, "%llu", n); - } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { - /* - * If this is an even multiple of the base, always display - * without any decimal precision. - */ - (void) snprintf(buf, buflen, "%llu%c", n, u); - } else { - /* - * We want to choose a precision that reflects the best choice - * for fitting in 5 characters. This can get rather tricky when - * we have numbers that are very close to an order of magnitude. - * For example, when displaying 10239 (which is really 9.999K), - * we want only a single place of precision for 10.0K. We could - * develop some complex heuristics for this, but it's much - * easier just to try each combination in turn. - */ - int i; - for (i = 2; i >= 0; i--) { - if (snprintf(buf, buflen, "%.*f%c", i, - (double)num / (1ULL << 10 * index), u) <= 5) - break; - } - } + nicenum(num, buf, buflen); } void |