summaryrefslogtreecommitdiff
path: root/libbb/progress.c
diff options
context:
space:
mode:
authorBradley M. Kuhn2010-08-08 02:51:20 +0200
committerDenys Vlasenko2010-08-08 02:51:20 +0200
commitc97131c2af832f03e769a12b2a95e4de86c5858f (patch)
tree6bb49cd582b75953c822431162a94809a9539918 /libbb/progress.c
parent33bbb27e45c7c6a0fecb40b3a5aa36aef69825f9 (diff)
downloadbusybox-c97131c2af832f03e769a12b2a95e4de86c5858f.zip
busybox-c97131c2af832f03e769a12b2a95e4de86c5858f.tar.gz
wget: implement -T SEC; rework progress meter to not use signals (it was unsafe)
function old new delta retrieve_file_data 364 450 +86 bb_progress_update 615 682 +67 packed_usage 27406 27422 +16 wget_main 2440 2453 +13 static.wget_longopts 145 155 +10 progress_meter 199 159 -40 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/1 up/down: 192/-40) Total: 152 bytes Signed-off-by: Bradley M. Kuhn <bkuhn@ebb.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/progress.c')
-rw-r--r--libbb/progress.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/libbb/progress.c b/libbb/progress.c
index e960390..7fb8536 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;