From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: [RFC iproute2-next 09/16] iproute: refactor printing of interface Date: Thu, 1 Feb 2018 17:19:39 -0800 Message-ID: <20180202011946.21929-10-sthemmin@microsoft.com> References: <20180202011946.21929-1-sthemmin@microsoft.com> Cc: netdev@vger.kernel.org, Stephen Hemminger , Stephen Hemminger To: dsahern@gmail.com Return-path: Received: from mail-pl0-f66.google.com ([209.85.160.66]:45946 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751848AbeBBBUP (ORCPT ); Thu, 1 Feb 2018 20:20:15 -0500 Received: by mail-pl0-f66.google.com with SMTP id p5so4925756plo.12 for ; Thu, 01 Feb 2018 17:20:15 -0800 (PST) In-Reply-To: <20180202011946.21929-1-sthemmin@microsoft.com> Sender: netdev-owner@vger.kernel.org List-ID: For JSON and colorization, make common code a function. Signed-off-by: Stephen Hemminger --- ip/iproute.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ip/iproute.c b/ip/iproute.c index b2eea4be9c37..a9842cfc8205 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -383,6 +383,14 @@ static void print_rt_pref(FILE *fp, unsigned int pref) } } +static void print_rta_if(FILE *fp, const struct rtattr *rta, + const char *prefix) +{ + const char *ifname = ll_index_to_name(rta_getattr_u32(rta)); + + fprintf(fp, "%s %s ", prefix, ifname); +} + static void print_ipv4_flags(FILE *fp, __u32 flags) { int first = 1; @@ -709,7 +717,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) print_rta_via(fp, tb[RTA_VIA]); if (tb[RTA_OIF] && filter.oifmask != -1) - fprintf(fp, "dev %s ", ll_index_to_name(rta_getattr_u32(tb[RTA_OIF]))); + print_rta_if(fp, tb[RTA_OIF], "dev"); if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb) fprintf(fp, "table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1))); @@ -766,10 +774,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) if (tb[RTA_METRICS]) print_rta_metrics(fp, tb[RTA_METRICS]); - if (tb[RTA_IIF] && filter.iifmask != -1) { - fprintf(fp, "iif %s ", - ll_index_to_name(rta_getattr_u32(tb[RTA_IIF]))); - } + if (tb[RTA_IIF] && filter.iifmask != -1) + print_rta_if(fp, tb[RTA_IIF], "iif"); if (tb[RTA_MULTIPATH]) print_rta_multipath(fp, r, tb[RTA_MULTIPATH]); -- 2.15.1