diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-09-12 17:39:33 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-09-13 07:27:47 +0200 |
commit | 868258cf38fb081c7766989fb92a91d2b167147e (patch) | |
tree | 158b50120695a5541c383658d35a15cac5d36e32 /src/basic/format-ifname.c | |
parent | minor fixups for #32212 (diff) | |
download | systemd-868258cf38fb081c7766989fb92a91d2b167147e.tar.xz systemd-868258cf38fb081c7766989fb92a91d2b167147e.zip |
basic: split ifname related calls from format-util.h into format-ifname.h
This way we don't have to pull in net/if.h into format-util.h.
This is supposed to address https://github.com/systemd/systemd/pull/32212#discussion_r1755639881
No actual code changes, just a .c/.h file split-up.
Diffstat (limited to 'src/basic/format-ifname.c')
-rw-r--r-- | src/basic/format-ifname.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/basic/format-ifname.c b/src/basic/format-ifname.c new file mode 100644 index 0000000000..ce4933c57d --- /dev/null +++ b/src/basic/format-ifname.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "format-ifname.h" +#include "string-util.h" + +assert_cc(STRLEN("%") + DECIMAL_STR_MAX(int) <= IF_NAMESIZE); + +int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { + if (ifindex <= 0) + return -EINVAL; + + if (if_indextoname(ifindex, buf)) + return 0; + + if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) + return -errno; + + if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) + assert_se(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex)); + else + assert_se(snprintf_ok(buf, IF_NAMESIZE, "%d", ifindex)); + + return 0; +} + +int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) { + char buf[IF_NAMESIZE]; + int r; + + assert(ret); + + r = format_ifname_full(ifindex, flag, buf); + if (r < 0) + return r; + + return strdup_to(ret, buf); +} |