print_ifindex: respect xlat style settings

Print interface indices according to the preferred xlat style settings.

* print_ifindex.c (INI_PFX, INI_SFX): New helper macros.
[HAVE_IF_INDEXTONAME] (get_ifname): New function.
[HAVE_IF_INDEXTONAME] (print_ifindex): Implement as a wrapper around
get_ifname and print_xlat_ex.
This commit is contained in:
Eugene Syromyatnikov 2018-05-07 08:42:24 +02:00 committed by Dmitry V. Levin
parent bf15325cd6
commit 004742588e

@ -28,19 +28,41 @@
#include "defs.h"
#include <net/if.h>
#ifdef HAVE_IF_INDEXTONAME
# include "xstring.h"
# define INI_PFX "if_nametoindex("
# define INI_SFX ")"
# define IFNAME_QUOTED_SZ (sizeof(IFNAMSIZ) * 4 + 3)
static const char *
get_ifname(const unsigned int ifindex)
{
static char res[IFNAME_QUOTED_SZ + sizeof(INI_PFX INI_SFX)];
char name_buf[IFNAMSIZ];
char name_quoted_buf[IFNAME_QUOTED_SZ];
if (if_indextoname(ifindex, name_buf)) {
if (string_quote(name_buf, name_quoted_buf, sizeof(name_buf),
QUOTE_0_TERMINATED, NULL))
return NULL;
xsprintf(res, INI_PFX "%s" INI_SFX, name_quoted_buf);
return res;
}
return NULL;
}
#endif /* HAVE_IF_INDEXTONAME */
void
print_ifindex(const unsigned int ifindex)
{
#ifdef HAVE_IF_INDEXTONAME
char buf[IFNAMSIZ + 1];
if (if_indextoname(ifindex, buf)) {
tprints("if_nametoindex(");
print_quoted_cstring(buf, sizeof(buf));
tprints(")");
return;
}
#endif
print_xlat_ex(ifindex, get_ifname(ifindex), XLAT_STYLE_FMT_U);
#else
tprintf("%u", ifindex);
#endif
}