From 94b6ab8fc6e2b539c7a9f818ba460143f8d30a94 Mon Sep 17 00:00:00 2001 From: pdw <> Date: Fri, 18 Oct 2002 10:06:01 +0000 Subject: [PATCH] Updated units display (again). This time goes up to GB. Spec file to 0.8. --- TODO | 2 ++ iftop.spec | 4 ++-- ui.c | 44 +++++++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index 616d9dc..c315e2d 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,8 @@ Things to do for iftop $Id$ +* Turn off services resolution. + * IP types other than v4? * Which average to use for the bar graph? Show several and peaks? Colours? diff --git a/iftop.spec b/iftop.spec index 6a94f0c..ef57673 100644 --- a/iftop.spec +++ b/iftop.spec @@ -1,10 +1,10 @@ Summary: iftop - provides realtime bandwith usage on an interface Name: iftop -Version: 0.8pre1 +Version: 0.8 Release: 1 License: GPL Group: Applications/Internet -Source0: http://www.ex-parrot.com/~pdw/iftop/download/iftop-0.8pre1.tar.gz +Source0: http://www.ex-parrot.com/~pdw/iftop/download/iftop-0.8.tar.gz URL: http://www.ex-parrot.com/~pdw/iftop/ Buildroot: %{_tmppath}/%{name}-%{version}-%(id -u -n) Packager: Riku Meskanen diff --git a/ui.c b/ui.c index d630859..e90b808 100644 --- a/ui.c +++ b/ui.c @@ -27,6 +27,9 @@ /* 1, 15 and 60 seconds */ int history_divs[HISTORY_DIVISIONS] = {1, 5, 20}; +#define UNIT_DIVISIONS 4 +char* unit_bits[UNIT_DIVISIONS] = { "b", "K", "M", "G"}; +char* unit_bytes[UNIT_DIVISIONS] = { "B", "KB", "MB", "GB"}; typedef struct host_pair_line_tag { addr_pair ap; @@ -63,27 +66,26 @@ int screen_line_compare(void* a, void* b) { } void readable_size(float n, char* buf, int bsize, int ksize, int bytes) { - if(n >= 100 * ksize * ksize) { - snprintf(buf, bsize, " %4.0f%s", n / (ksize * ksize), bytes ? "MB" : "M"); - } - else if(n >= 10 * ksize * ksize) { - snprintf(buf, bsize, " %4.1f%s", n / (ksize * ksize), bytes ? "MB" : "M"); - } - if(n >= ksize * ksize) { - snprintf(buf, bsize, " %4.2f%s", n / (ksize * ksize), bytes ? "MB" : "M" ); - } - else if(n >= 100 * ksize) { - snprintf(buf, bsize, " %4.0f%s", n / ksize, bytes ? "KB" : "K" ); - } - else if(n >= 10 * ksize) { - snprintf(buf, bsize, " %4.1f%s", n / ksize, bytes ? "KB" : "K" ); - } - else if(n >= ksize) { - snprintf(buf, bsize, " %4.2f%s", n / ksize, bytes ? "KB" : "K" ); - } - else { - snprintf(buf, bsize, " %4.0f%s", n, bytes ? "B" : "b"); - } + + int i = 0; + int size = 1; + + while(1) { + if(n < size * ksize || i >= UNIT_DIVISIONS - 1) { + snprintf(buf, bsize, " %4.0f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]); + break; + } + i++; + size *= ksize; + if(n < size * 10) { + snprintf(buf, bsize, " %4.2f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]); + break; + } + else if(n < size * 100) { + snprintf(buf, bsize, " %4.1f%s", n / size, bytes ? unit_bytes[i] : unit_bits[i]); + break; + } + } }