diff options
author | Lennart Poettering <lennart@poettering.net> | 2016-06-08 19:25:38 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2016-06-14 19:50:38 +0200 |
commit | 9184ca48ea43e009cd6f379f319926109a30926c (patch) | |
tree | 0db1fa083fb240966e47d621d861cd7cb45501e6 /src/basic | |
parent | d9ab2bcf0591b496f1a4750c7ff790b33f9c7e59 (diff) |
util-lib: introduce parse_percent() for parsing percent specifications
And port a couple of users over to it.
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/parse-util.c | 19 | ||||
-rw-r--r-- | src/basic/parse-util.h | 2 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 6c11b605a9..503a895731 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -532,3 +532,22 @@ int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) { return 0; } + +int parse_percent(const char *p) { + const char *pc, *n; + unsigned v; + int r; + + pc = endswith(p, "%"); + if (!pc) + return -EINVAL; + + n = strndupa(p, pc - p); + r = safe_atou(n, &v); + if (r < 0) + return r; + if (v > 100) + return -ERANGE; + + return (int) v; +} diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h index 7dc579a159..73441bb6fd 100644 --- a/src/basic/parse-util.h +++ b/src/basic/parse-util.h @@ -105,3 +105,5 @@ static inline int safe_atozu(const char *s, size_t *ret_u) { int safe_atod(const char *s, double *ret_d); int parse_fractional_part_u(const char **s, size_t digits, unsigned *res); + +int parse_percent(const char *p); |