2 * Copyright (c) 2001-2018 The strace developers.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
11 #ifdef HAVE_IF_INDEXTONAME
14 # define INI_PFX "if_nametoindex(\""
15 # define INI_SFX "\")"
16 # define IFNAME_QUOTED_SZ (sizeof(IFNAMSIZ) * 4 + 3)
19 get_ifname(const unsigned int ifindex)
21 static char name_quoted_buf[IFNAME_QUOTED_SZ];
22 char name_buf[IFNAMSIZ];
24 if (!if_indextoname(ifindex, name_buf))
27 if (string_quote(name_buf, name_quoted_buf, sizeof(name_buf),
28 QUOTE_0_TERMINATED | QUOTE_OMIT_LEADING_TRAILING_QUOTES,
32 return name_quoted_buf;
36 sprint_ifname(const unsigned int ifindex)
38 static char res[IFNAME_QUOTED_SZ + sizeof(INI_PFX INI_SFX)];
40 const char *name_quoted = get_ifname(ifindex);
45 xsprintf(res, INI_PFX "%s" INI_SFX, name_quoted);
50 #else /* !HAVE_IF_INDEXTONAME */
52 const char *get_ifname(const unsigned int ifindex) { return NULL; }
54 #endif /* HAVE_IF_INDEXTONAME */
57 print_ifindex(const unsigned int ifindex)
59 #ifdef HAVE_IF_INDEXTONAME
60 print_xlat_ex(ifindex, sprint_ifname(ifindex), XLAT_STYLE_FMT_U);
62 tprintf("%u", ifindex);