summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/basic/parse-util.c15
-rw-r--r--src/basic/parse-util.h1
-rw-r--r--src/test/test-parse-util.c14
3 files changed, 30 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index c38e14d96..011355962 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -595,3 +595,18 @@ int parse_ip_port(const char *s, uint16_t *ret) {
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;
+}
diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h
index 3555aa1ea..ecbf4bdad 100644
--- a/src/basic/parse-util.h
+++ b/src/basic/parse-util.h
@@ -30,6 +30,7 @@
#define MODE_INVALID ((mode_t) -1)
int parse_boolean(const char *v) _pure_;
+int parse_dev(const char *s, dev_t *ret);
int parse_pid(const char *s, pid_t* ret_pid);
int parse_mode(const char *s, mode_t *ret);
int parse_ifindex(const char *s, int *ret);
diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c
index 981370ab9..83f16da27 100644
--- a/src/test/test-parse-util.c
+++ b/src/test/test-parse-util.c
@@ -530,6 +530,19 @@ static void test_parse_nice(void) {
}
#endif // 0
+static void test_parse_dev(void) {
+ dev_t dev;
+
+ assert_se(parse_dev("0", &dev) == -EINVAL);
+ assert_se(parse_dev("5", &dev) == -EINVAL);
+ assert_se(parse_dev("5:", &dev) == -EINVAL);
+ assert_se(parse_dev(":5", &dev) == -EINVAL);
+#if SIZEOF_DEV_T < 8
+ assert_se(parse_dev("4294967295:4294967295", &dev) == -EINVAL);
+#endif
+ assert_se(parse_dev("8:11", &dev) >= 0 && major(dev) == 8 && minor(dev) == 11);
+}
+
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
@@ -550,6 +563,7 @@ int main(int argc, char *argv[]) {
#if 0 /// UNNEEDED by elogind
test_parse_nice();
#endif // 0
+ test_parse_dev();
return 0;
}