[git commit master] dd: use correct multiplication factor and simplify code

Denys Vlasenko vda.linux at googlemail.com
Wed Oct 14 15:43:41 UTC 2009


commit: http://git.busybox.net/busybox/commit/?id=7eabffafa5b6faaaa87ff8ba0efbf637aaa364e8
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
dd_output_status                                     332     364     +32

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/dd.c |   26 +++++++-------------------
 1 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/coreutils/dd.c b/coreutils/dd.c
index f2e1a16..8bba62c 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -49,9 +49,9 @@ struct globals {
 static void dd_output_status(int UNUSED_PARAM cur_signal)
 {
 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
-	unsigned long long total;
-	unsigned long long diff_scaled;
-	unsigned long long diff_us = monotonic_us(); /* before fprintf */
+	double seconds;
+	unsigned long long bytes_sec;
+	unsigned long long now_us = monotonic_us(); /* before fprintf */
 #endif
 
 	/* Deliberately using %u, not %d */
@@ -72,24 +72,12 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
 	 * (echo DONE) | ./busybox dd >/dev/null
 	 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
 	 */
-	diff_us -= G.begin_time_us;
-	/* We need to calculate "(total * 1000000) / usec" without overflow.
-	 * this would work too, but is bigger than integer code below.
-	 * total = G.total_bytes * (double)1000000 / (diff_us ? diff_us : 1);
-	 */
-	diff_scaled = diff_us;
-	total = G.total_bytes;
-	while (total > MAXINT(unsigned long long) / (1024 * 1024)) {
-		total >>= 1;
-		diff_scaled >>= 1;
-	}
-	total *= (1024 * 1024); /* should be 1000000, but it's +45 bytes */
-	if (diff_scaled > 1)
-		total /= diff_scaled;
+	seconds = (now_us - G.begin_time_us) / 1000000.0;
+	bytes_sec = G.total_bytes / seconds;
 	fprintf(stderr, "%f seconds, %sB/s\n",
-			diff_us / 1000000.0,
+			seconds,
 			/* show fractional digit, use suffixes */
-			make_human_readable_str(total, 1, 0)
+			make_human_readable_str(bytes_sec, 1, 0)
 	);
 #endif
 }
-- 
1.6.3.3



More information about the busybox-cvs mailing list