From mboxrd@z Thu Jan 1 00:00:00 1970 From: Serhey Popovych Subject: [PATCH iproute2-next 3/9] utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n() Date: Mon, 5 Feb 2018 21:49:28 +0200 Message-ID: <1517860174-18333-4-git-send-email-serhe.popovych@gmail.com> References: <1517860174-18333-1-git-send-email-serhe.popovych@gmail.com> To: netdev@vger.kernel.org Return-path: Received: from mail-lf0-f65.google.com ([209.85.215.65]:44520 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751826AbeBETuN (ORCPT ); Mon, 5 Feb 2018 14:50:13 -0500 Received: by mail-lf0-f65.google.com with SMTP id v188so43420831lfa.11 for ; Mon, 05 Feb 2018 11:50:13 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id h39sm1941284lji.72.2018.02.05.11.50.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 11:50:11 -0800 (PST) In-Reply-To: <1517860174-18333-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: Now all users of ll_idx_n2a() replaced with ll_index_to_name() we can move it's functionality to ll_index_to_name() and implement index to name conversion using snprintf() and "if%u". Use %u specifier in "if%..." template consistently: network device indexes are always greather than zero. Also introduce ll_idx_n2a() conterpart: ll_idx_a2n() that is used to translate name of the "if%u" form to index using sscanf(). Signed-off-by: Serhey Popovych --- include/ll_map.h | 4 +++- lib/ll_map.c | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/ll_map.h b/include/ll_map.h index c8474e6..8546ff9 100644 --- a/include/ll_map.h +++ b/include/ll_map.h @@ -8,9 +8,11 @@ int ll_remember_index(const struct sockaddr_nl *who, void ll_init_map(struct rtnl_handle *rth); unsigned ll_name_to_index(const char *name); const char *ll_index_to_name(unsigned idx); -const char *ll_idx_n2a(unsigned idx, char *buf); int ll_index_to_type(unsigned idx); int ll_index_to_flags(unsigned idx); unsigned namehash(const char *str); +const char *ll_idx_n2a(unsigned int idx); +unsigned int ll_idx_a2n(const char *name); + #endif /* __LL_MAP_H__ */ diff --git a/lib/ll_map.c b/lib/ll_map.c index f65614f..0afe689 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -136,8 +136,26 @@ int ll_remember_index(const struct sockaddr_nl *who, return 0; } -const char *ll_idx_n2a(unsigned idx, char *buf) +const char *ll_idx_n2a(unsigned int idx) { + static char buf[IFNAMSIZ]; + + snprintf(buf, sizeof(buf), "if%u", idx); + return buf; +} + +unsigned int ll_idx_a2n(const char *name) +{ + unsigned int idx; + + if (sscanf(name, "if%u", &idx) != 1) + return 0; + return idx; +} + +const char *ll_index_to_name(unsigned int idx) +{ + static char buf[IFNAMSIZ]; const struct ll_cache *im; if (idx == 0) @@ -148,18 +166,11 @@ const char *ll_idx_n2a(unsigned idx, char *buf) return im->name; if (if_indextoname(idx, buf) == NULL) - snprintf(buf, IFNAMSIZ, "if%d", idx); + snprintf(buf, IFNAMSIZ, "if%u", idx); return buf; } -const char *ll_index_to_name(unsigned idx) -{ - static char nbuf[IFNAMSIZ]; - - return ll_idx_n2a(idx, nbuf); -} - int ll_index_to_type(unsigned idx) { const struct ll_cache *im; @@ -196,7 +207,7 @@ unsigned ll_name_to_index(const char *name) idx = if_nametoindex(name); if (idx == 0) - sscanf(name, "if%u", &idx); + idx = ll_idx_a2n(name); return idx; } -- 1.7.10.4