summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-06-08 19:25:38 +0200
committerLennart Poettering <lennart@poettering.net>2016-06-14 19:50:38 +0200
commit9184ca48ea43e009cd6f379f319926109a30926c (patch)
tree0db1fa083fb240966e47d621d861cd7cb45501e6 /src/basic
parentd9ab2bcf0591b496f1a4750c7ff790b33f9c7e59 (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.c19
-rw-r--r--src/basic/parse-util.h2
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);