All of lore.kernel.org
 help / color / mirror / Atom feed
From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
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	[thread overview]
Message-ID: <1517860174-18333-4-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1517860174-18333-1-git-send-email-serhe.popovych@gmail.com>

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 <serhe.popovych@gmail.com>
---
 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

  parent reply	other threads:[~2018-02-05 19:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 19:49 [PATCH iproute2-next 0/9] ipaddress: Make print_linkinfo_brief() static Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 1/9] ipaddress: Abstract IFA_LABEL matching code Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 2/9] ipaddress: ll_map: Replace ll_idx_n2a() with ll_index_to_name() Serhey Popovych
2018-02-05 19:49 ` Serhey Popovych [this message]
2018-02-05 19:49 ` [PATCH iproute2-next 4/9] ipaddress: Improve print_linkinfo() Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 5/9] ipaddress: Simplify print_linkinfo_brief() and it's usage Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 6/9] lib: Correct object file dependencies Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 7/9] utils: Introduce and use get_ifname_rta() Serhey Popovych
2018-02-05 19:49 ` [PATCH iproute2-next 8/9] utils: Introduce and use print_name_and_link() to print name@link Serhey Popovych
2018-02-07  5:14   ` David Ahern
2018-02-07  7:36     ` Serhey Popovych
2018-02-07 15:59       ` David Ahern
2018-02-05 19:49 ` [PATCH iproute2-next 9/9] ipaddress: Make print_linkinfo_brief() static Serhey Popovych
2018-02-14 20:09 ` [PATCH iproute2-next 0/9] " Serhey Popovych
2018-02-14 20:20   ` David Ahern
2018-02-14 21:39     ` Serhey Popovych

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1517860174-18333-4-git-send-email-serhe.popovych@gmail.com \
    --to=serhe.popovych@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.