diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-09-08 19:42:32 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-10-24 14:20:48 +0900 |
commit | 882e21c72ff93795467cdd7e83a597a9cab1f2ea (patch) | |
tree | 4f7d792a967611ced5a13bdd226808f22e1e0b14 /src/basic | |
parent | 99b8517ca7eb0a9015155cc1e0a9ae66e07240f0 (diff) | |
download | systemd-882e21c72ff93795467cdd7e83a597a9cab1f2ea.tar.gz |
format-util: introduce format_ifname_full()
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/format-util.c | 17 | ||||
-rw-r--r-- | src/basic/format-util.h | 10 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/basic/format-util.c b/src/basic/format-util.c index aec929a06a..9fea2e0690 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -4,11 +4,24 @@ #include "format-util.h" #include "memory-util.h" +#include "stdio-util.h" -char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]) { +assert_cc(DECIMAL_STR_MAX(int) + 1 <= IF_NAMESIZE + 1); +char *format_ifname_full(int ifindex, char buf[static IF_NAMESIZE + 1], FormatIfnameFlag flag) { /* Buffer is always cleared */ memzero(buf, IF_NAMESIZE + 1); - return if_indextoname(ifindex, buf); + if (if_indextoname(ifindex, buf)) + return buf; + + if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) + return NULL; + + if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) + snprintf(buf, IF_NAMESIZE + 1, "%%%d", ifindex); + else + snprintf(buf, IF_NAMESIZE + 1, "%d", ifindex); + + return buf; } char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { diff --git a/src/basic/format-util.h b/src/basic/format-util.h index e0d184a541..59622508a3 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -68,7 +68,15 @@ # error Unknown ino_t size #endif -char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]); +typedef enum { + FORMAT_IFNAME_IFINDEX = 1 << 0, + FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, +} FormatIfnameFlag; + +char *format_ifname_full(int ifindex, char buf[static IF_NAMESIZE + 1], FormatIfnameFlag flag); +static inline char *format_ifname(int ifindex, char buf[static IF_NAMESIZE + 1]) { + return format_ifname_full(ifindex, buf, 0); +} typedef enum { FORMAT_BYTES_USE_IEC = 1 << 0, |