From 1fb67608b309bd7f49842fbdfb1dc2b18a250965 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 14 May 2016 14:02:52 -0600 Subject: tiny-printf: Tidy up a few nits - Rename 'w' to 'width' to make it more obvious what it is used for - Use bool and int types instead of char to avoid register-masking on 32-bit machines Signed-off-by: Simon Glass Reviewed-by: Stefan Roese --- lib/tiny-printf.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib/tiny-printf.c') diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index a06abed4959..fbd5368c0b3 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -52,8 +52,8 @@ int vprintf(const char *fmt, va_list va) if (ch != '%') { putc(ch); } else { - char lz = 0; - char w = 0; + bool lz = false; + int width = 0; ch = *(fmt++); if (ch == '0') { @@ -62,9 +62,9 @@ int vprintf(const char *fmt, va_list va) } if (ch >= '0' && ch <= '9') { - w = 0; + width = 0; while (ch >= '0' && ch <= '9') { - w = (w * 10) + ch - '0'; + width = (width * 10) + ch - '0'; ch = *fmt++; } } @@ -73,7 +73,7 @@ int vprintf(const char *fmt, va_list va) zs = 0; switch (ch) { - case 0: + case '\0': goto abort; case 'u': case 'd': @@ -112,9 +112,9 @@ int vprintf(const char *fmt, va_list va) *bf = 0; bf = p; - while (*bf++ && w > 0) - w--; - while (w-- > 0) + while (*bf++ && width > 0) + width--; + while (width-- > 0) putc(lz ? '0' : ' '); if (p) { while ((ch = *p++)) -- cgit v1.2.3 From 5c411d88be8df5f6a8a1ea0c961f7c35ba82c064 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 14 May 2016 14:02:53 -0600 Subject: tiny-printf: Support snprintf() Add a simple version of this function for SPL. It does not check the buffer size as this would add to the code size. Signed-off-by: Simon Glass Reviewed-by: Stefan Roese --- lib/tiny-printf.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'lib/tiny-printf.c') diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index fbd5368c0b3..4b70263df7d 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -16,6 +16,9 @@ static char *bf; static char zs; +/* Current position in sprintf() output string */ +static char *outstr; + static void out(char c) { *bf++ = c; @@ -40,7 +43,7 @@ static void div_out(unsigned int *num, unsigned int div) out_dgt(dgt); } -int vprintf(const char *fmt, va_list va) +int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch)) { char ch; char *p; @@ -133,8 +136,28 @@ int printf(const char *fmt, ...) int ret; va_start(va, fmt); - ret = vprintf(fmt, va); + ret = _vprintf(fmt, va, putc); + va_end(va); + + return ret; +} + +static void putc_outstr(char ch) +{ + *outstr++ = ch; +} + +/* Note that size is ignored */ +int snprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list va; + int ret; + + va_start(va, fmt); + outstr = buf; + ret = _vprintf(fmt, va, putc_outstr); va_end(va); + *outstr = '\0'; return ret; } -- cgit v1.2.3 From abeb272d22217481c214495818c3ceabad57b9c0 Mon Sep 17 00:00:00 2001 From: Marek Vasut Date: Tue, 31 May 2016 23:12:46 +0200 Subject: tiny-printf: Support sprintf() Add a simple version of this function for SPL. It does not check the buffer size as this would add to the code size. Signed-off-by: Marek Vasut Cc: Simon Glass Cc: Stefan Roese Cc: Tom Rini Cc: lesne@alse-fr.com Reviewed-by: Tom Rini Reviewed-by: Sylvain Lesne Tested-by: Sylvain Lesne --- lib/tiny-printf.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'lib/tiny-printf.c') diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index 4b70263df7d..5ea2555280b 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -147,8 +147,7 @@ static void putc_outstr(char ch) *outstr++ = ch; } -/* Note that size is ignored */ -int snprintf(char *buf, size_t size, const char *fmt, ...) +int sprintf(char *buf, const char *fmt, ...) { va_list va; int ret; @@ -161,3 +160,16 @@ int snprintf(char *buf, size_t size, const char *fmt, ...) return ret; } + +/* Note that size is ignored */ +int snprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list va; + int ret; + + va_start(va, fmt); + ret = sprintf(buf, fmt, va); + va_end(va); + + return ret; +} -- cgit v1.2.3 From 3191d8408053674c1b9bb79a82e3973add48830c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 8 Jun 2016 20:55:15 -0600 Subject: tiny-printf: Correct the snprintf() implementation This current code passes the variable arguments list to sprintf(). This is not correct. Fix it by calling _vprintf() directly. This makes firefly-rk3288 boot again. Fixes: abeb272 ("tiny-printf: Support snprintf()") Reviewed-by: Stefan Roese Acked-by: Marek Vasut Signed-off-by: Simon Glass --- lib/tiny-printf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/tiny-printf.c') diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c index 5ea2555280b..3c65fc90bf2 100644 --- a/lib/tiny-printf.c +++ b/lib/tiny-printf.c @@ -168,8 +168,10 @@ int snprintf(char *buf, size_t size, const char *fmt, ...) int ret; va_start(va, fmt); - ret = sprintf(buf, fmt, va); + outstr = buf; + ret = _vprintf(fmt, va, putc_outstr); va_end(va); + *outstr = '\0'; return ret; } -- cgit v1.2.3