From 4915837a7b7f482c0d4561061e01f5e998e65120 Mon Sep 17 00:00:00 2001 From: Cyril Bur Date: Fri, 28 Jul 2017 16:46:33 +1000 Subject: external/pflash: Make the progress bar safe for big numbers Signed-off-by: Cyril Bur Reviewed-by: Samuel Mendoza-Jonas Signed-off-by: Stewart Smith --- external/pflash/progress.c | 35 ++++++++++++++++++++--------------- external/pflash/progress.h | 6 ++++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/external/pflash/progress.c b/external/pflash/progress.c index f4406a7..df8eb35 100644 --- a/external/pflash/progress.c +++ b/external/pflash/progress.c @@ -1,26 +1,29 @@ +#include +#include +#include #include #include #include -#include #include + #include "progress.h" -static unsigned long progress_max; -static unsigned int progress_pcent; -static unsigned long progress_n_upd; -static unsigned int progress_prevsec; +static uint64_t progress_max; +static uint64_t progress_pcent; +static uint64_t progress_n_upd; +static time_t progress_prevsec; static struct timespec progress_start; #define PROGRESS_CHARS 50 -void progress_init(unsigned long count) +void progress_init(uint64_t count) { unsigned int i; progress_max = count; progress_pcent = 0; progress_n_upd = ULONG_MAX; - progress_prevsec = UINT_MAX; + progress_prevsec = ULONG_MAX; printf("\r["); for (i = 0; i < PROGRESS_CHARS; i++) @@ -29,10 +32,12 @@ void progress_init(unsigned long count) fflush(stdout); clock_gettime(CLOCK_MONOTONIC, &progress_start);} -void progress_tick(unsigned long cur) +void progress_tick(uint64_t cur) { - unsigned int pcent, i, pos, sec; + unsigned int i, pos; struct timespec now; + uint64_t pcent; + double sec; pcent = (cur * 100) / progress_max; if (progress_pcent == pcent && cur < progress_n_upd && @@ -47,12 +52,12 @@ void progress_tick(unsigned long cur) printf("="); for (; i < PROGRESS_CHARS; i++) printf(" "); - printf("] %u%%", pcent); + printf("] %" PRIu64 "%%", pcent); - sec = now.tv_sec - progress_start.tv_sec; + sec = difftime(now.tv_sec, progress_start.tv_sec); if (sec >= 5 && pcent > 0) { - unsigned int persec = cur / sec; - unsigned int rem_sec; + uint64_t persec = cur / sec; + uint64_t rem_sec; if (!persec) persec = 1; @@ -62,9 +67,9 @@ void progress_tick(unsigned long cur) rem_sec = progress_prevsec; progress_prevsec = rem_sec; if (rem_sec < 60) - printf(" ETA:%us ", rem_sec); + printf(" ETA:%" PRIu64 "s ", rem_sec); else { - printf(" ETA:%u:%02d:%02u ", + printf(" ETA:%" PRIu64 ":%02" PRIu64 ":%02" PRIu64 " ", rem_sec / 3600, (rem_sec / 60) % 60, rem_sec % 60); diff --git a/external/pflash/progress.h b/external/pflash/progress.h index 673b495..b073dbe 100644 --- a/external/pflash/progress.h +++ b/external/pflash/progress.h @@ -1,8 +1,10 @@ #ifndef __PROGRESS_H #define __PROGRESS_H -void progress_init(unsigned long count); -void progress_tick(unsigned long cur); +#include + +void progress_init(uint64_t count); +void progress_tick(uint64_t cur); void progress_end(void); #endif /* __PROGRESS_H */ -- cgit v1.1