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>
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 @@
 		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 @@
 		/* 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 @@
 		}
 	}
 	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;