summaryrefslogtreecommitdiffstats
path: root/util/types.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/types.c')
-rw-r--r--util/types.c57
1 files changed, 54 insertions, 3 deletions
diff --git a/util/types.c b/util/types.c
index daef298..044391d 100644
--- a/util/types.c
+++ b/util/types.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <string.h>
#include <locale.h>
+#include <time.h>
#include <ccan/endian/endian.h>
@@ -14,6 +15,7 @@ nvme_uint128_t le128_to_cpu(__u8 *data)
{
nvme_uint128_t u;
nvme_uint128_t tmp;
+
memcpy(tmp.bytes, data, 16);
u.words[0] = le32_to_cpu(tmp.words[3]);
u.words[1] = le32_to_cpu(tmp.words[2]);
@@ -24,8 +26,8 @@ nvme_uint128_t le128_to_cpu(__u8 *data)
long double int128_to_double(__u8 *data)
{
- int i;
long double result = 0;
+ int i;
for (i = 0; i < 16; i++) {
result *= 256;
@@ -48,8 +50,8 @@ uint64_t int48_to_long(__u8 *data)
static long double uint128_t_to_double(nvme_uint128_t data)
{
- int i;
long double result = 0;
+ int i;
for (i = 0; i < sizeof(data.words) / sizeof(*data.words); i++) {
result *= 4294967296;
@@ -117,8 +119,8 @@ char *uint128_t_to_l10n_string(nvme_uint128_t val)
char *uint128_t_to_si_string(nvme_uint128_t val, __u32 bytes_per_unit)
{
- static char str[40];
long double bytes = uint128_t_to_double(val) * bytes_per_unit;
+ static char str[40];
const char *suffix = suffix_si_get_ld(&bytes);
int n = snprintf(str, sizeof(str), "%.2Lf %sB", bytes, suffix);
@@ -150,3 +152,52 @@ const char *util_fw_to_string(char *c)
ret[i] = '\0';
return ret;
}
+
+int convert_ts(time_t time, char *ts_buf)
+{
+ struct tm time_info;
+ time_t time_human, time_ms;
+ char buf[80];
+
+ time_human = time / 1000;
+ time_ms = time % 1000;
+
+ gmtime_r((const time_t *)&time_human, &time_info);
+
+ strftime(buf, sizeof(buf), "%Y-%m-%dD|%H:%M:%S", &time_info);
+ sprintf(ts_buf, "%s:%03ld", buf, time_ms);
+
+ return 0;
+}
+
+void util_spinner(const char *disp_name, float percent)
+{
+ static const char dash[51] = {[0 ... 49] = '=', '\0'};
+ static const char space[51] = {[0 ... 49] = ' ', '\0'};
+ static const char spin[] = {'-', '\\', '|', '/' };
+ static int progress;
+ static int i;
+ const char *dn = disp_name ? disp_name : "";
+
+ if (percent < 0)
+ percent = 0;
+ else if (percent > 1)
+ percent = 1;
+
+ progress = (int)(percent * 100.0);
+ if (progress < 2)
+ printf("\r%s [%c%.*s] %3d%%", dn,
+ spin[i % 4], 49,
+ space, progress);
+ else if (progress < 100)
+ printf("\r%s [%.*s%c%.*s] %3d%%", dn,
+ progress / 2 - 1, dash,
+ spin[i % 4], 50 - progress / 2,
+ space, progress);
+ else
+ printf("\r%s [%.*s] %3d%%\n", dn,
+ 50, dash, 100);
+ i++;
+
+ fflush(stdout);
+}