diff options
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/ether-addr-util.c | 13 | ||||
-rw-r--r-- | src/basic/ether-addr-util.h | 15 |
2 files changed, 23 insertions, 5 deletions
diff --git a/src/basic/ether-addr-util.c b/src/basic/ether-addr-util.c index b97997e1bf..c609ea6ce0 100644 --- a/src/basic/ether-addr-util.c +++ b/src/basic/ether-addr-util.c @@ -11,7 +11,11 @@ #include "macro.h" #include "string-util.h" -char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]) { +char *hw_addr_to_string_full( + const struct hw_addr_data *addr, + HardwareAddressToStringFlags flags, + char buffer[static HW_ADDR_TO_STRING_MAX]) { + assert(addr); assert(buffer); assert(addr->length <= HW_ADDR_MAX_SIZE); @@ -19,10 +23,13 @@ char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_ for (size_t i = 0, j = 0; i < addr->length; i++) { buffer[j++] = hexchar(addr->bytes[i] >> 4); buffer[j++] = hexchar(addr->bytes[i] & 0x0f); - buffer[j++] = ':'; + if (!FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON)) + buffer[j++] = ':'; } - buffer[addr->length > 0 ? addr->length * 3 - 1 : 0] = '\0'; + buffer[addr->length == 0 || FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON) ? + addr->length * 2 : + addr->length * 3 - 1] = '\0'; return buffer; } diff --git a/src/basic/ether-addr-util.h b/src/basic/ether-addr-util.h index b2a3f0d58c..bee1fc9c7a 100644 --- a/src/basic/ether-addr-util.h +++ b/src/basic/ether-addr-util.h @@ -31,13 +31,24 @@ static inline int parse_hw_addr(const char *s, struct hw_addr_data *ret) { } int parse_ether_addr(const char *s, struct ether_addr *ret); +typedef enum HardwareAddressToStringFlags { + HW_ADDR_TO_STRING_NO_COLON = 1 << 0, +} HardwareAddressToStringFlags; + #define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE) -char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]); +char *hw_addr_to_string_full( + const struct hw_addr_data *addr, + HardwareAddressToStringFlags flags, + char buffer[static HW_ADDR_TO_STRING_MAX]); +static inline char *hw_addr_to_string(const struct hw_addr_data *addr, char buffer[static HW_ADDR_TO_STRING_MAX]) { + return hw_addr_to_string_full(addr, 0, buffer); +} /* Note: the lifetime of the compound literal is the immediately surrounding block, * see C11 ยง6.5.2.5, and * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ -#define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){}) +#define HW_ADDR_TO_STR_FULL(hw_addr, flags) hw_addr_to_string_full((hw_addr), flags, (char[HW_ADDR_TO_STRING_MAX]){}) +#define HW_ADDR_TO_STR(hw_addr) HW_ADDR_TO_STR_FULL(hw_addr, 0) #define HW_ADDR_NULL ((const struct hw_addr_data){}) |