Commit 031a72de authored by Cedric Roux's avatar Cedric Roux

T: new function 'bps'

This function is used to print rates (say '1kb/s' or '16Mb/s').
parent d7831c1f
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <ctype.h> #include <ctype.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <math.h>
void new_thread(void *(*f)(void *), void *data) void new_thread(void *(*f)(void *), void *data)
{ {
...@@ -36,6 +37,16 @@ void sleepms(int ms) ...@@ -36,6 +37,16 @@ void sleepms(int ms)
if (nanosleep(&t, NULL)) abort(); if (nanosleep(&t, NULL)) abort();
} }
void bps(char *out, float v, char *suffix)
{
static char *bps_unit[4] = { "", "k", "M", "G" };
int flog;
if (v < 1000) flog = 0; else flog = floor(floor(log10(v)) / 3);
if (flog > 3) flog = 3;
v /= pow(10, flog*3);
sprintf(out, "%g%s%s", round(v*100)/100, bps_unit[flog], suffix);
}
/****************************************************************************/ /****************************************************************************/
/* list */ /* list */
/****************************************************************************/ /****************************************************************************/
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
void new_thread(void *(*f)(void *), void *data); void new_thread(void *(*f)(void *), void *data);
void sleepms(int ms); void sleepms(int ms);
void bps(char *out, float v, char *suffix);
/****************************************************************************/ /****************************************************************************/
/* list */ /* list */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment