From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marek Vasut Date: Thu, 26 May 2016 18:00:24 +0200 Subject: [U-Boot] [PATCH 1/2] [RFC] lib: Implement support for tiny sprintf() Message-ID: <1464278425-4904-1-git-send-email-marex@denx.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Tweak the tiny printf code to also provide similarly tiny sprintf() implementation. This is not comformant with POSIX for sure, but it keeps the size down while still behaving rather reasonably. Signed-off-by: Marek Vasut Cc: Simon Glass Cc: Tom Rini --- lib/tiny-printf.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index a06abed..b9fba97 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -40,7 +40,17 @@ static void div_out(unsigned int *num, unsigned int div) out_dgt(dgt); } -int vprintf(const char *fmt, va_list va) +#ifdef CONFIG_USE_TINY_SPRINTF +#define local_putc(pbuf, ch) \ + if (pbuf) \ + *(pbuf)++ = (ch); \ + else \ + putc(ch); +#else + #define local_putc(pbuf, ch) putc(ch) +#endif + +static int local_xprintf(char *pbuf, const char *fmt, va_list va) { char ch; char *p; @@ -50,7 +60,7 @@ int vprintf(const char *fmt, va_list va) while ((ch = *(fmt++))) { if (ch != '%') { - putc(ch); + local_putc(pbuf, ch); } else { char lz = 0; char w = 0; @@ -115,10 +125,10 @@ int vprintf(const char *fmt, va_list va) while (*bf++ && w > 0) w--; while (w-- > 0) - putc(lz ? '0' : ' '); + local_putc(pbuf, lz ? '0' : ' '); if (p) { while ((ch = *p++)) - putc(ch); + local_putc(pbuf, ch); } } } @@ -127,13 +137,31 @@ abort: return 0; } + +int vprintf(const char *fmt, va_list va) +{ + return local_xprintf(NULL, fmt, va); +} + int printf(const char *fmt, ...) { va_list va; int ret; va_start(va, fmt); - ret = vprintf(fmt, va); + ret = local_xprintf(NULL, fmt, va); + va_end(va); + + return ret; +} + +int sprintf(char *buf, const char *fmt, ...) +{ + va_list va; + int ret; + + va_start(va, fmt); + ret = local_xprintf(buf, fmt, va); va_end(va); return ret; -- 2.7.0