diff --git a/src/iperf_api.c b/src/iperf_api.c index 77c0c59..0354421 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -637,7 +637,7 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) return -1; } } - test->settings->rate = unit_atof(optarg); + test->settings->rate = unit_atof_rate(optarg); rate_flag = 1; client_flag = 1; break; diff --git a/src/units.c b/src/units.c index 917422b..87a8471 100644 --- a/src/units.c +++ b/src/units.c @@ -71,6 +71,10 @@ extern "C" const long MEGA_UNIT = 1024 * 1024; const long GIGA_UNIT = 1024 * 1024 * 1024; + const long KILO_RATE_UNIT = 1000; + const long MEGA_RATE_UNIT = 1000 * 1000; + const long GIGA_RATE_UNIT = 1000 * 1000 * 1000; + /* ------------------------------------------------------------------- * unit_atof * @@ -107,6 +111,44 @@ extern "C" return n; } /* end unit_atof */ + +/* ------------------------------------------------------------------- + * unit_atof_rate + * + * Similar to unit_atof, but uses 10-based rather than 2-based + * suffixes. + * ------------------------------------------------------------------- */ + + double unit_atof_rate(const char *s) + { + double n; + char suffix = '\0'; + + assert(s != NULL); + + /* scan the number and any suffices */ + sscanf(s, "%lf%c", &n, &suffix); + + /* convert according to [Gg Mm Kk] */ + switch (suffix) + { + case 'g': case 'G': + n *= GIGA_RATE_UNIT; + break; + case 'm': case 'M': + n *= MEGA_RATE_UNIT; + break; + case 'k': case 'K': + n *= KILO_RATE_UNIT; + break; + default: + break; + } + return n; + } /* end unit_atof_rate */ + + + /* ------------------------------------------------------------------- * unit_atoi * diff --git a/src/units.h b/src/units.h index dfd115a..ce99594 100644 --- a/src/units.h +++ b/src/units.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2011, 2014, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * @@ -12,5 +12,6 @@ enum { }; double unit_atof( const char *s ); +double unit_atof_rate( const char *s ); iperf_size_t unit_atoi( const char *s ); void unit_snprintf( char *s, int inLen, double inNum, char inFormat );