diff options
author | Nick Mathewson <nickm@torproject.org> | 2009-07-15 10:02:49 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2009-07-15 10:02:49 -0400 |
commit | d4b31cf98f85550155156726ad9f3424def07fb7 (patch) | |
tree | 3bb24af1f16102f36c58ad653d23469cd10c4c48 /src | |
parent | 72c5a46b43c6d89c773a99240a95301b91b1f269 (diff) | |
download | tor-d4b31cf98f85550155156726ad9f3424def07fb7.tar.gz tor-d4b31cf98f85550155156726ad9f3424def07fb7.zip |
Allow interval and memunit cfg variables to be set to fractions.
Diffstat (limited to 'src')
-rw-r--r-- | src/or/config.c | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/src/or/config.c b/src/or/config.c index 087a907e48..19212fe899 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -4791,30 +4791,55 @@ static struct unit_table_t time_units[] = { static uint64_t config_parse_units(const char *val, struct unit_table_t *u, int *ok) { - uint64_t v; - char *cp; + uint64_t v = 0; + double d = 0; + int use_float = 0; + + smartlist_t *sl; tor_assert(ok); + sl = smartlist_create(); + smartlist_split_string(sl, val, NULL, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3); - v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp); - if (!*ok) - return 0; - if (!cp) { + if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) { + *ok = 0; + goto done; + } + + v = tor_parse_uint64(smartlist_get(sl,0), 10, 0, UINT64_MAX, ok, NULL); + if (!*ok) { + int r = sscanf(smartlist_get(sl,0), "%lf", &d); + if (r == 0 || d < 0) + goto done; + use_float = 1; + } + + if (smartlist_len(sl) == 1) { *ok = 1; - return v; + v = use_float ? DBL_TO_U64(d) : v; + goto done; } - while (TOR_ISSPACE(*cp)) - ++cp; + for ( ;u->unit;++u) { - if (!strcasecmp(u->unit, cp)) { - v *= u->multiplier; + if (!strcasecmp(u->unit, smartlist_get(sl,1))) { + if (use_float) + v = u->multiplier * d; + else + v *= u->multiplier; *ok = 1; - return v; + goto done; } } - log_warn(LD_CONFIG, "Unknown unit '%s'.", cp); + log_warn(LD_CONFIG, "Unknown unit '%s'.", (char*)smartlist_get(sl,1)); *ok = 0; - return 0; + done: + SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); + smartlist_free(sl); + + if (*ok) + return v; + else + return 0; } /** Parse a string in the format "number unit", where unit is a unit of |