chiark / gitweb /
util-lib: rework /tmp and /var/tmp handling code
[elogind.git] / src / basic / parse-util.c
index d0208e93f01a028c6e5880afcb1fb8d79e3b02a4..77e85f658a9a23f16cb63e72496739631076d7aa 100644 (file)
@@ -33,6 +33,9 @@
 #include "parse-util.h"
 #include "string-util.h"
 
+/// Additional includes needed by elogind
+#include "musl_missing.h"
+
 int parse_boolean(const char *v) {
         assert(v);
 
@@ -536,3 +539,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;
+}