diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2003-03-03 14:16:19 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2003-03-03 14:16:19 +0000 |
commit | 05e8929fe4dc52ebf25cfb6152c232ea9f9f68fc (patch) | |
tree | ba56572d945bd6a023451605748f41a5b0a80dc4 | |
parent | 1cc47287b2be4247046a7ec77d0b6eb3dd19d0cb (diff) |
rate2str() overhaul:
-do not print 100.00Kb but 100Kb, but still 1.50Mb. requested by theo. hint
by dhartmei lead to a easier algorithm than initially had
-while beeing there, use a much nicer, shorter algorithm for determining
and printing the unit. a little further easification thanks to a hint by
markus@
ok dhartmei@ cedric@
-rw-r--r-- | sbin/pfctl/pfctl_altq.c | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/sbin/pfctl/pfctl_altq.c b/sbin/pfctl/pfctl_altq.c index 16ea795fbf7..3045d4d0cf4 100644 --- a/sbin/pfctl/pfctl_altq.c +++ b/sbin/pfctl/pfctl_altq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl_altq.c,v 1.40 2003/03/02 23:37:24 henning Exp $ */ +/* $OpenBSD: pfctl_altq.c,v 1.41 2003/03/03 14:16:18 henning Exp $ */ /* * Copyright (C) 2002 @@ -1022,22 +1022,21 @@ rate2str(double rate) char *buf; static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */ static int idx = 0; + int i; + static const char unit[] = " KMG"; buf = r2sbuf[idx++]; if (idx == R2S_BUFS) idx = 0; - if (rate == 0.0) - snprintf(buf, RATESTR_MAX, "0b"); - else if (rate >= 1000 * 1000 * 1000) - snprintf(buf, RATESTR_MAX, "%.2fGb", - rate / (1000.0 * 1000.0 * 1000.0)); - else if (rate >= 1000 * 1000) - snprintf(buf, RATESTR_MAX, "%.2fMb", rate / (1000.0 * 1000.0)); - else if (rate >= 1000) - snprintf(buf, RATESTR_MAX, "%.2fKb", rate / 1000.0); + for (i = 0; rate >= 1000 && i <= 3; i++) + rate /= 1000; + + if ((int)(rate * 100) % 100) + snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]); else - snprintf(buf, RATESTR_MAX, "%db", (int)rate); + snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]); + return (buf); } |