Angel Pons | b706ab3 | 2020-04-02 23:48:09 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 2 | |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 3 | #include <console/vtxprintf.h> |
Edward O'Callaghan | 0ddb826 | 2014-06-17 18:37:08 +1000 | [diff] [blame] | 4 | #include <string.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 5 | |
Elyes HAOUAS | a418414 | 2018-05-15 20:57:01 +0200 | [diff] [blame] | 6 | struct vsnprintf_context { |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 7 | char *str_buf; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 8 | size_t buf_limit; |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 9 | }; |
| 10 | |
| 11 | static void str_tx_byte(unsigned char byte, void *data) |
| 12 | { |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 13 | struct vsnprintf_context *ctx = data; |
| 14 | if (ctx->buf_limit) { |
| 15 | *ctx->str_buf = byte; |
| 16 | ctx->str_buf++; |
| 17 | ctx->buf_limit--; |
| 18 | } |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 19 | } |
Patrick Georgi | 736221f1 | 2009-05-26 14:31:37 +0000 | [diff] [blame] | 20 | |
David Hendricks | 6053a9c | 2018-01-28 18:01:10 -0800 | [diff] [blame] | 21 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 22 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | int i; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 24 | struct vsnprintf_context ctx; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 25 | |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 26 | ctx.str_buf = buf; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 27 | ctx.buf_limit = size ? size - 1 : 0; |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 28 | i = vtxprintf(str_tx_byte, fmt, args, &ctx); |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 29 | if (size) |
| 30 | *ctx.str_buf = '\0'; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 31 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | return i; |
| 33 | } |
| 34 | |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 35 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 36 | { |
| 37 | va_list args; |
| 38 | int i; |
| 39 | |
| 40 | va_start(args, fmt); |
| 41 | i = vsnprintf(buf, size, fmt, args); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 42 | va_end(args); |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 43 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 44 | return i; |
| 45 | } |