[Skiboot] [PATCH v2 15/19] external/pflash: Make the progress bar safe for big numbers
Cyril Bur
cyril.bur at au1.ibm.com
Fri Jul 28 16:46:33 AEST 2017
Signed-off-by: Cyril Bur <cyril.bur at au1.ibm.com>
---
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 f4406a78..df8eb358 100644
--- a/external/pflash/progress.c
+++ b/external/pflash/progress.c
@@ -1,26 +1,29 @@
+#include <inttypes.h>
+#include <limits.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <limits.h>
#include <time.h>
+
#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 673b4958..b073dbe8 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 <inttypes.h>
+
+void progress_init(uint64_t count);
+void progress_tick(uint64_t cur);
void progress_end(void);
#endif /* __PROGRESS_H */
--
2.13.3
More information about the Skiboot
mailing list