When parsing the -b (bandwidth) argument, use 10-based suffices.
We support using k, m, and g as suffices on input values. In most cases these are 2-based suffixes (i.e. K == 1024) because they are sizes of objects. In the case of rates, we need to use 10-based suffices (i.e. K == 1000). We do this by implementing (using copy-and-paste) a unit_atof_rate() subroutine that parses strings similarly to unit_atof but using 10-based suffices instead. Fixes #173. (cherry picked from commit f50627706fa67808ba2c04c66bd4012510d4ca71) Signed-off-by: Bruce A. Mah <bmah@es.net>
This commit is contained in:
parent
93a83a3b7a
commit
c71ac9dd78
@ -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;
|
||||
|
42
src/units.c
42
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
|
||||
*
|
||||
|
@ -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 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user