[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1517335761-22095-7-git-send-email-serhe.popovych@gmail.com>
Date: Tue, 30 Jan 2018 20:09:21 +0200
From: Serhey Popovych <serhe.popovych@...il.com>
To: netdev@...r.kernel.org
Subject: [PATCH iproute2-next v2 6/6] ipaddress: Make print_linkinfo_brief() static
It shares lot of code with print_linkinfo(): drop duplicated part,
change parameters list, make it static and call from print_linkinfo()
after common path.
While there move SPRINT_BUF() to the function scope from blocks to
avoid duplication and use "%s" to print "\n" to help compiler optimize
exit for both print_linkinfo_brief() and normal paths.
Signed-off-by: Serhey Popovych <serhe.popovych@...il.com>
---
ip/ip_common.h | 2 --
ip/ipaddress.c | 74 ++++++++------------------------------------------------
ip/iplink.c | 5 +---
3 files changed, 11 insertions(+), 70 deletions(-)
diff --git a/ip/ip_common.h b/ip/ip_common.h
index f5adbad..a7bbf1d 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -29,8 +29,6 @@ struct link_filter {
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);
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 5afc9d4..450d3cc 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -918,63 +918,12 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n)
fprintf(fp, "%s", _SL_);
}
-int print_linkinfo_brief(const struct sockaddr_nl *who,
- struct nlmsghdr *n, void *arg)
+static int print_linkinfo_brief(FILE *fp, const char *name,
+ const struct ifinfomsg *ifi,
+ struct rtattr *tb[])
{
- FILE *fp = (FILE *)arg;
- struct ifinfomsg *ifi = NLMSG_DATA(n);
- struct rtattr *tb[IFLA_MAX+1];
- int len = n->nlmsg_len;
- const char *name;
unsigned int m_flag = 0;
- if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
- return -1;
-
- len -= NLMSG_LENGTH(sizeof(*ifi));
- if (len < 0)
- return -1;
-
- if (filter.ifindex && ifi->ifi_index != filter.ifindex)
- return -1;
- if (filter.up && !(ifi->ifi_flags&IFF_UP))
- return -1;
-
- parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
-
- name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
- if (!name)
- return -1;
-
- 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 (filter.group != -1 && group != filter.group)
- return -1;
- }
-
- if (tb[IFLA_MASTER]) {
- int master = rta_getattr_u32(tb[IFLA_MASTER]);
-
- if (filter.master > 0 && master != filter.master)
- return -1;
- } else if (filter.master > 0)
- return -1;
-
- if (filter.kind && match_link_kind(tb, filter.kind, 0))
- return -1;
-
- if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
- return -1;
-
- if (n->nlmsg_type == RTM_DELLINK)
- print_bool(PRINT_ANY, "deleted", "Deleted ", true);
-
m_flag = print_name_and_link("%-16s ", COLOR_NONE, name, tb);
if (tb[IFLA_OPERSTATE])
@@ -1033,6 +982,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
int len = n->nlmsg_len;
const char *name;
unsigned int m_flag = 0;
+ SPRINT_BUF(b1);
if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
return 0;
@@ -1081,6 +1031,9 @@ int print_linkinfo(const struct sockaddr_nl *who,
if (n->nlmsg_type == RTM_DELLINK)
print_bool(PRINT_ANY, "deleted", "Deleted ", true);
+ if (brief)
+ return print_linkinfo_brief(fp, name, ifi, tb);
+
print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index);
m_flag = print_name_and_link("%s: ", COLOR_IFNAME, name, tb);
print_link_flags(fp, ifi->ifi_flags, m_flag);
@@ -1097,8 +1050,6 @@ int print_linkinfo(const struct sockaddr_nl *who,
"qdisc %s ",
rta_getattr_str(tb[IFLA_QDISC]));
if (tb[IFLA_MASTER]) {
- SPRINT_BUF(b1);
-
print_string(PRINT_ANY,
"master",
"master %s ",
@@ -1112,7 +1063,6 @@ int print_linkinfo(const struct sockaddr_nl *who,
print_linkmode(fp, tb[IFLA_LINKMODE]);
if (tb[IFLA_GROUP]) {
- SPRINT_BUF(b1);
int group = rta_getattr_u32(tb[IFLA_GROUP]);
print_string(PRINT_ANY,
@@ -1279,7 +1229,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
close_json_array(PRINT_JSON, NULL);
}
- print_string(PRINT_FP, NULL, "\n", NULL);
+ print_string(PRINT_FP, NULL, "%s", "\n");
fflush(fp);
return 1;
}
@@ -2138,14 +2088,10 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
for (l = linfo.head; l; l = l->next) {
struct nlmsghdr *n = &l->h;
struct ifinfomsg *ifi = NLMSG_DATA(n);
- int res;
+ int res = 0;
open_json_object(NULL);
- if (brief)
- res = print_linkinfo_brief(NULL, n, stdout);
- else if (no_link)
- res = 0;
- else
+ if (brief || !no_link)
res = print_linkinfo(NULL, n, stdout);
if (res >= 0 && filter.family != AF_PACKET)
print_selected_addrinfo(ifi, ainfo->head, stdout);
diff --git a/ip/iplink.c b/ip/iplink.c
index 882cd13..6f8f7cd 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -1076,10 +1076,7 @@ int iplink_get(unsigned int flags, char *name, __u32 filt_mask)
return -2;
open_json_object(NULL);
- if (brief)
- print_linkinfo_brief(NULL, answer, stdout);
- else
- print_linkinfo(NULL, answer, stdout);
+ print_linkinfo(NULL, answer, stdout);
close_json_object();
free(answer);
--
1.7.10.4
Powered by blists - more mailing lists