1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-07 01:27:11 +03:00

parse-util: introduce parse_dev() helper

This commit is contained in:
Franck Bui 2017-04-26 18:18:03 +02:00
parent 9bfc0df113
commit fbcc7f4168
3 changed files with 30 additions and 0 deletions

View File

@ -589,3 +589,18 @@ int parse_ip_port(const char *s, uint16_t *ret) {
return 0; return 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;
}

View File

@ -30,6 +30,7 @@
#define MODE_INVALID ((mode_t) -1) #define MODE_INVALID ((mode_t) -1)
int parse_boolean(const char *v) _pure_; 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_pid(const char *s, pid_t* ret_pid);
int parse_mode(const char *s, mode_t *ret); int parse_mode(const char *s, mode_t *ret);
int parse_ifindex(const char *s, int *ret); int parse_ifindex(const char *s, int *ret);

View File

@ -526,6 +526,19 @@ static void test_parse_nice(void) {
assert_se(parse_nice("+20", &n) == -ERANGE); assert_se(parse_nice("+20", &n) == -ERANGE);
} }
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[]) { int main(int argc, char *argv[]) {
log_parse_environment(); log_parse_environment();
log_open(); log_open();
@ -542,6 +555,7 @@ int main(int argc, char *argv[]) {
test_parse_percent(); test_parse_percent();
test_parse_percent_unbounded(); test_parse_percent_unbounded();
test_parse_nice(); test_parse_nice();
test_parse_dev();
return 0; return 0;
} }