| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "format-ifname.h" |
| 4 |
#include "stdio-util.h" |
| 5 |
#include "string-util.h" |
| 6 |
|
| 7 |
assert_cc(STRLEN("%") + DECIMAL_STR_MAX(int) <= IF_NAMESIZE); |
| 8 |
|
| 9 |
int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NAMESIZE]) { |
| 10 |
if (ifindex <= 0) |
| 11 |
return -EINVAL; |
| 12 |
|
| 13 |
if (if_indextoname(ifindex, buf)) |
| 14 |
return 0; |
| 15 |
|
| 16 |
if (!FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX)) |
| 17 |
return -errno; |
| 18 |
|
| 19 |
if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) |
| 20 |
assert_se(snprintf_ok(buf, IF_NAMESIZE, "%%%d", ifindex)); |
| 21 |
else |
| 22 |
assert_se(snprintf_ok(buf, IF_NAMESIZE, "%d", ifindex)); |
| 23 |
|
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) { |
| 28 |
char buf[IF_NAMESIZE]; |
| 29 |
int r; |
| 30 |
|
| 31 |
assert(ret); |
| 32 |
|
| 33 |
r = format_ifname_full(ifindex, flag, buf); |
| 34 |
if (r < 0) |
| 35 |
return r; |
| 36 |
|
| 37 |
return strdup_to(ret, buf); |
| 38 |
} |