util-lib: introduce parse_percent() for parsing percent specifications

And port a couple of users over to it.
This commit is contained in:
Lennart Poettering
2016-06-14 19:50:38 +02:00
parent d9ab2bcf05
commit 9184ca48ea
6 changed files with 59 additions and 40 deletions
+19
View File
@@ -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;
}