Improve print_linkinfo_brief() and it's callers:

  1) Get rid of custom @struct filter pointer @pfilter: it is NULL in
     all callers anyway and global @filter is used.

  2) Simplify calling code in ipaddr_list_flush_or_save() by
     introducing intermediate variable of @struct nlmsghdr, drop
     duplicated code: print_linkinfo_brief() never returns values other
     than <= 0 so we can move print_selected_addrinfo() outside of each
     block.

Signed-off-by: Serhey Popovych <serhe.popov...@gmail.com>
---
 ip/ip_common.h |    3 +--
 ip/ipaddress.c |   60 ++++++++++++++++++++++++--------------------------------
 ip/iplink.c    |    2 +-
 3 files changed, 28 insertions(+), 37 deletions(-)

diff --git a/ip/ip_common.h b/ip/ip_common.h
index 3203f0c..f5adbad 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -30,8 +30,7 @@ int get_operstate(const char *name);
 int print_linkinfo(const struct sockaddr_nl *who,
                   struct nlmsghdr *n, void *arg);
 int print_linkinfo_brief(const struct sockaddr_nl *who,
-                        struct nlmsghdr *n, void *arg,
-                        struct link_filter *filter);
+                        struct nlmsghdr *n, void *arg);
 int print_addrinfo(const struct sockaddr_nl *who,
                   struct nlmsghdr *n, void *arg);
 int print_addrlabel(const struct sockaddr_nl *who,
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index f8fd392..c15abd1 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -919,8 +919,7 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n)
 }
 
 int print_linkinfo_brief(const struct sockaddr_nl *who,
-                        struct nlmsghdr *n, void *arg,
-                        struct link_filter *pfilter)
+                        struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE *)arg;
        struct ifinfomsg *ifi = NLMSG_DATA(n);
@@ -937,12 +936,9 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
        if (len < 0)
                return -1;
 
-       if (!pfilter)
-               pfilter = &filter;
-
-       if (pfilter->ifindex && ifi->ifi_index != pfilter->ifindex)
+       if (filter.ifindex && ifi->ifi_index != filter.ifindex)
                return -1;
-       if (pfilter->up && !(ifi->ifi_flags&IFF_UP))
+       if (filter.up && !(ifi->ifi_flags&IFF_UP))
                return -1;
 
        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
@@ -953,30 +949,30 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
                name = rta_getattr_str(tb[IFLA_IFNAME]);
        }
 
-       if (pfilter->label &&
-           (!pfilter->family || pfilter->family == AF_PACKET) &&
-           fnmatch(pfilter->label, name, 0))
+       if (filter.label &&
+           (!filter.family || filter.family == AF_PACKET) &&
+           fnmatch(filter.label, name, 0))
                return -1;
 
        if (tb[IFLA_GROUP]) {
                int group = rta_getattr_u32(tb[IFLA_GROUP]);
 
-               if (pfilter->group != -1 && group != pfilter->group)
+               if (filter.group != -1 && group != filter.group)
                        return -1;
        }
 
        if (tb[IFLA_MASTER]) {
                int master = rta_getattr_u32(tb[IFLA_MASTER]);
 
-               if (pfilter->master > 0 && master != pfilter->master)
+               if (filter.master > 0 && master != filter.master)
                        return -1;
-       } else if (pfilter->master > 0)
+       } else if (filter.master > 0)
                return -1;
 
-       if (pfilter->kind && match_link_kind(tb, pfilter->kind, 0))
+       if (filter.kind && match_link_kind(tb, filter.kind, 0))
                return -1;
 
-       if (pfilter->slave_kind && match_link_kind(tb, pfilter->slave_kind, 1))
+       if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
                return -1;
 
        if (n->nlmsg_type == RTM_DELLINK)
@@ -1006,7 +1002,7 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
        if (tb[IFLA_OPERSTATE])
                print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
 
-       if (pfilter->family == AF_PACKET) {
+       if (filter.family == AF_PACKET) {
                SPRINT_BUF(b1);
 
                if (tb[IFLA_ADDRESS]) {
@@ -1020,7 +1016,7 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
                }
        }
 
-       if (pfilter->family == AF_PACKET) {
+       if (filter.family == AF_PACKET) {
                print_link_flags(fp, ifi->ifi_flags, m_flag);
                print_string(PRINT_FP, NULL, "%s", "\n");
        }
@@ -2188,25 +2184,21 @@ static int ipaddr_list_flush_or_save(int argc, char 
**argv, int action)
                ipaddr_filter(&linfo, ainfo);
 
        for (l = linfo.head; l; l = l->next) {
-               int res = 0;
-               struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
+               struct nlmsghdr *n = &l->h;
+               struct ifinfomsg *ifi = NLMSG_DATA(n);
+               int res;
 
                open_json_object(NULL);
-               if (brief) {
-                       if (print_linkinfo_brief(NULL, &l->h,
-                                                stdout, NULL) == 0)
-                               if (filter.family != AF_PACKET)
-                                       print_selected_addrinfo(ifi,
-                                                               ainfo->head,
-                                                               stdout);
-               } else if (no_link ||
-                          (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
-                       if (filter.family != AF_PACKET)
-                               print_selected_addrinfo(ifi,
-                                                       ainfo->head, stdout);
-                       if (res > 0 && !do_link && show_stats)
-                               print_link_stats(stdout, &l->h);
-               }
+               if (brief)
+                       res = print_linkinfo_brief(NULL, n, stdout);
+               else if (no_link)
+                       res = 0;
+               else
+                       res = print_linkinfo(NULL, n, stdout);
+               if (res >= 0 && filter.family != AF_PACKET)
+                       print_selected_addrinfo(ifi, ainfo->head, stdout);
+               if (res > 0 && !do_link && show_stats)
+                       print_link_stats(stdout, n);
                close_json_object();
        }
        fflush(stdout);
diff --git a/ip/iplink.c b/ip/iplink.c
index 230f4c5..882cd13 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -1077,7 +1077,7 @@ int iplink_get(unsigned int flags, char *name, __u32 
filt_mask)
 
        open_json_object(NULL);
        if (brief)
-               print_linkinfo_brief(NULL, answer, stdout, NULL);
+               print_linkinfo_brief(NULL, answer, stdout);
        else
                print_linkinfo(NULL, answer, stdout);
        close_json_object();
-- 
1.7.10.4

Reply via email to