From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/2] vsprintf: Add support for printing ipv4 addresses with %pI4
Date: Thu, 15 Sep 2016 12:52:16 +0200 [thread overview]
Message-ID: <1473936737-4919-1-git-send-email-s.hauer@pengutronix.de> (raw)
Can be used conveniently in places that currently use ip_to_string().
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
lib/vsprintf.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index f3885a8..fa9fb75 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -191,6 +191,27 @@ static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int
}
static noinline_for_stack
+char *ip4_addr_string(char *buf, char *end, const u8 *addr, int field_width,
+ int precision, int flags, const char *fmt)
+{
+ char ip4_addr[sizeof("255.255.255.255")];
+ char *pos;
+ int i;
+
+ pos = ip4_addr;
+
+ for (i = 0; i < 4; i++) {
+ pos = number(pos, pos + 3, addr[i], 10, 0, 0, LEFT);
+ if (i < 3)
+ *pos++ = '.';
+ }
+
+ *pos = 0;
+
+ return string(buf, end, ip4_addr, field_width, precision, flags);
+}
+
+static noinline_for_stack
char *uuid_string(char *buf, char *end, const u8 *addr, int field_width,
int precision, int flags, const char *fmt)
{
@@ -267,6 +288,8 @@ char *address_val(char *buf, char *end, const void *addr,
*
* Right now we handle:
*
+ * - 'I' [4] for IPv4 addresses printed in the usual way
+ * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
* - 'S' For symbolic direct pointers
* - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@@ -297,6 +320,12 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field
break;
case 'a':
return address_val(buf, end, ptr, field_width, precision, flags, fmt);
+ case 'I':
+ switch (fmt[1]) {
+ case '4':
+ return ip4_addr_string(buf, end, ptr, field_width, precision, flags, fmt);
+ }
+ break;
}
flags |= SMALL;
if (field_width == -1) {
--
2.8.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next reply other threads:[~2016-09-15 10:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-15 10:52 Sascha Hauer [this message]
2016-09-15 10:52 ` [PATCH 2/2] convert users to %pI4 Sascha Hauer
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=1473936737-4919-1-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox