From 868258cf38fb081c7766989fb92a91d2b167147e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 12 Sep 2024 17:39:33 +0200 Subject: 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. --- src/basic/format-ifname.c | 37 +++++++++++++++++++++++++++++++++++++ src/basic/format-ifname.h | 27 +++++++++++++++++++++++++++ src/basic/format-util.c | 32 -------------------------------- src/basic/format-util.h | 24 ------------------------ src/basic/meson.build | 1 + src/basic/socket-util.c | 2 +- 6 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 src/basic/format-ifname.c create mode 100644 src/basic/format-ifname.h (limited to 'src/basic') 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); +} diff --git a/src/basic/format-ifname.h b/src/basic/format-ifname.h new file mode 100644 index 0000000000..84415b1388 --- /dev/null +++ b/src/basic/format-ifname.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +typedef enum { + FORMAT_IFNAME_IFINDEX = 1 << 0, + FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, +} FormatIfnameFlag; + +int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]); +int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret); + +static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) { + return format_ifname_full(ifindex, 0, buf); +} +static inline int format_ifname_alloc(int ifindex, char **ret) { + return format_ifname_full_alloc(ifindex, 0, ret); +} + +static inline char* _format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { + (void) format_ifname_full(ifindex, flag, buf); + return buf; +} + +#define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){}) +#define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){}) diff --git a/src/basic/format-util.c b/src/basic/format-util.c index 7098bf6a99..09cb716e7d 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -5,38 +5,6 @@ #include "stdio-util.h" #include "strxcpyx.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(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex)); - else - assert(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); -} - char* format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { typedef struct { const char *suffix; diff --git a/src/basic/format-util.h b/src/basic/format-util.h index 4a030b7557..b528c005ca 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -2,7 +2,6 @@ #pragma once #include -#include #include #include "cgroup-util.h" @@ -66,29 +65,6 @@ assert_cc(sizeof(gid_t) == sizeof(uint32_t)); # error Unknown ino_t size #endif -typedef enum { - FORMAT_IFNAME_IFINDEX = 1 << 0, - FORMAT_IFNAME_IFINDEX_WITH_PERCENT = (1 << 1) | FORMAT_IFNAME_IFINDEX, -} FormatIfnameFlag; - -int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]); -int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret); - -static inline int format_ifname(int ifindex, char buf[static IF_NAMESIZE]) { - return format_ifname_full(ifindex, 0, buf); -} -static inline int format_ifname_alloc(int ifindex, char **ret) { - return format_ifname_full_alloc(ifindex, 0, ret); -} - -static inline char* _format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { - (void) format_ifname_full(ifindex, flag, buf); - return buf; -} - -#define FORMAT_IFNAME_FULL(index, flag) _format_ifname_full(index, flag, (char[IF_NAMESIZE]){}) -#define FORMAT_IFNAME(index) _format_ifname_full(index, 0, (char[IF_NAMESIZE]){}) - typedef enum { FORMAT_BYTES_USE_IEC = 1 << 0, FORMAT_BYTES_BELOW_POINT = 1 << 1, diff --git a/src/basic/meson.build b/src/basic/meson.build index 5b62fe90bc..4f83eb67a0 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -33,6 +33,7 @@ basic_sources = files( 'fd-util.c', 'fileio.c', 'filesystems.c', + 'format-ifname.c', 'format-util.c', 'fs-util.c', 'gcrypt-util.c', diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index e4fb5a280c..94089323b4 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -21,7 +21,7 @@ #include "escape.h" #include "fd-util.h" #include "fileio.h" -#include "format-util.h" +#include "format-ifname.h" #include "io-util.h" #include "log.h" #include "memory-util.h" -- cgit v1.2.3