chiark / gitweb /
tests: add tests for environment serialization
[elogind.git] / src / basic / parse-util.c
index 90bae7149559ee6001bf25b69341fd1da765b583..01135596244836f690e31945e1a6e77761cc7734 100644 (file)
@@ -23,9 +23,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#if defined(__GLIBC__)
-#  include <xlocale.h>
-#endif // defined(__GLIBC__)
 
 #include "alloc-util.h"
 //#include "extract-word.h"
@@ -33,6 +30,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);
 
@@ -270,6 +270,7 @@ int parse_range(const char *t, unsigned *lower, unsigned *upper) {
         *upper = u;
         return 0;
 }
+#endif // 0
 
 char *format_bytes(char *buf, size_t l, uint64_t t) {
         unsigned i;
@@ -311,7 +312,6 @@ finish:
         return buf;
 
 }
-#endif // 0
 
 int safe_atou(const char *s, unsigned *ret_u) {
         char *x = NULL;
@@ -537,7 +537,7 @@ int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
         return 0;
 }
 
-int parse_percent(const char *p) {
+int parse_percent_unbounded(const char *p) {
         const char *pc, *n;
         unsigned v;
         int r;
@@ -550,8 +550,63 @@ int parse_percent(const char *p) {
         r = safe_atou(n, &v);
         if (r < 0)
                 return r;
+
+        return (int) v;
+}
+
+int parse_percent(const char *p) {
+        int v;
+
+        v = parse_percent_unbounded(p);
         if (v > 100)
                 return -ERANGE;
 
-        return (int) v;
+        return v;
+}
+
+#if 0 /// UNNEEDED by elogind
+int parse_nice(const char *p, int *ret) {
+        int n, r;
+
+        r = safe_atoi(p, &n);
+        if (r < 0)
+                return r;
+
+        if (!nice_is_valid(n))
+                return -ERANGE;
+
+        *ret = n;
+        return 0;
+}
+
+int parse_ip_port(const char *s, uint16_t *ret) {
+        uint16_t l;
+        int r;
+
+        r = safe_atou16(s, &l);
+        if (r < 0)
+                return r;
+
+        if (l == 0)
+                return -EINVAL;
+
+        *ret = (uint16_t) l;
+
+        return 0;
+}
+#endif // 0
+
+int parse_dev(const char *s, dev_t *ret) {
+        unsigned x, y;
+        dev_t d;
+
+        if (sscanf(s, "%u:%u", &x, &y) != 2)
+                return -EINVAL;
+
+        d = makedev(x, y);
+        if ((unsigned) major(d) != x || (unsigned) minor(d) != y)
+                return -EINVAL;
+
+        *ret = d;
+        return 0;
 }