From: Yu Watanabe Date: Wed, 29 May 2019 01:43:12 +0000 (+0900) Subject: util: introduce parse_ifindex_or_ifname() X-Git-Tag: v243-rc1~353^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=88465a4ee7b86c4b69f96a23082163692076fb96;p=thirdparty%2Fsystemd.git util: introduce parse_ifindex_or_ifname() --- diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 87724af6930..ae764c581df 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,24 @@ int parse_ifindex(const char *s, int *ret) { return 0; } +int parse_ifindex_or_ifname(const char *s, int *ret) { + int r; + + assert(s); + assert(ret); + + r = parse_ifindex(s, ret); + if (r >= 0) + return r; + + r = (int) if_nametoindex(s); + if (r <= 0) + return -errno; + + *ret = r; + return 0; +} + int parse_mtu(int family, const char *s, uint32_t *ret) { uint64_t u; size_t m; diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h index e47641b4295..7f5b0cf2b48 100644 --- a/src/basic/parse-util.h +++ b/src/basic/parse-util.h @@ -16,6 +16,7 @@ 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); +int parse_ifindex_or_ifname(const char *s, int *ret); int parse_mtu(int family, const char *s, uint32_t *ret); int parse_size(const char *t, uint64_t base, uint64_t *size);