aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/progress.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/libbb/progress.c b/libbb/progress.c
index e96039042..7fb8536d2 100644
--- a/libbb/progress.c
+++ b/libbb/progress.c
@@ -66,16 +66,29 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
off_t transferred,
off_t totalsize)
{
- off_t abbrevsize;
+ uoff_t beg_and_transferred;
unsigned since_last_update, elapsed;
unsigned ratio;
int barlength, i;
+ /* totalsize == 0 if it is unknown */
+
+ elapsed = monotonic_sec();
+ since_last_update = elapsed - p->lastupdate_sec;
+ /* Do not update on every call
+ * (might be: on every network read!) */
+ if (since_last_update == 0 && !totalsize)
+ return;
+
+ beg_and_transferred = beg_range + transferred;
ratio = 100;
- if (totalsize) {
+ if (beg_and_transferred < totalsize) {
+ /* Do not update on every call
+ * (might be: on every network read!) */
+ if (since_last_update == 0)
+ return;
/* long long helps to have it working even if !LFS */
- ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize);
- if (ratio > 100) ratio = 100;
+ ratio = 100ULL * beg_and_transferred / (uoff_t)totalsize;
}
#if ENABLE_UNICODE_SUPPORT
@@ -95,11 +108,11 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
/* back to multibyte; cant overflow */
wcstombs(buf, wbuf21, INT_MAX);
len = (len > 20) ? 0 : 20 - len;
- fprintf(stderr, "\r%s%*s%4d%% ", buf, len, "", ratio);
+ fprintf(stderr, "\r%s%*s%4u%% ", buf, len, "", ratio);
free(buf);
}
#else
- fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
+ fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
#endif
barlength = get_tty2_width() - 49;
@@ -114,16 +127,14 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
}
}
i = 0;
- abbrevsize = transferred + beg_range;
- while (abbrevsize >= 100000) {
+ while (beg_and_transferred >= 100000) {
i++;
- abbrevsize >>= 10;
+ beg_and_transferred >>= 10;
}
/* see http://en.wikipedia.org/wiki/Tera */
- fprintf(stderr, "%6d%c ", (int)abbrevsize, " kMGTPEZY"[i]);
+ fprintf(stderr, "%6u%c ", (unsigned)beg_and_transferred, " kMGTPEZY"[i]);
+#define beg_and_transferred dont_use_beg_and_transferred_below
- elapsed = monotonic_sec();
- since_last_update = elapsed - p->lastupdate_sec;
if (transferred > p->lastsize) {
p->lastupdate_sec = elapsed;
p->lastsize = transferred;