Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 3 | * |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; version 2 of |
| 7 | * the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 15 | #include <console/vtxprintf.h> |
Edward O'Callaghan | 0ddb826 | 2014-06-17 18:37:08 +1000 | [diff] [blame] | 16 | #include <string.h> |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 17 | #include <trace.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 18 | |
Elyes HAOUAS | a418414 | 2018-05-15 20:57:01 +0200 | [diff] [blame] | 19 | struct vsnprintf_context { |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 20 | char *str_buf; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 21 | size_t buf_limit; |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static void str_tx_byte(unsigned char byte, void *data) |
| 25 | { |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 26 | struct vsnprintf_context *ctx = data; |
| 27 | if (ctx->buf_limit) { |
| 28 | *ctx->str_buf = byte; |
| 29 | ctx->str_buf++; |
| 30 | ctx->buf_limit--; |
| 31 | } |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 32 | } |
Patrick Georgi | 736221f1 | 2009-05-26 14:31:37 +0000 | [diff] [blame] | 33 | |
David Hendricks | 6053a9c | 2018-01-28 18:01:10 -0800 | [diff] [blame] | 34 | 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] | 35 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 36 | int i; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 37 | struct vsnprintf_context ctx; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 38 | |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 39 | DISABLE_TRACE; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 40 | |
Vladimir Serbinenko | 2267658 | 2013-11-26 21:48:57 +0100 | [diff] [blame] | 41 | ctx.str_buf = buf; |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 42 | ctx.buf_limit = size ? size - 1 : 0; |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 43 | i = vtxprintf(str_tx_byte, fmt, args, &ctx); |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 44 | if (size) |
| 45 | *ctx.str_buf = '\0'; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 46 | |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 47 | ENABLE_TRACE; |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 48 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | return i; |
| 50 | } |
| 51 | |
Vladimir Serbinenko | 4b5012a | 2013-11-26 22:07:47 +0100 | [diff] [blame] | 52 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 53 | { |
| 54 | va_list args; |
| 55 | int i; |
| 56 | |
| 57 | va_start(args, fmt); |
| 58 | i = vsnprintf(buf, size, fmt, args); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 59 | va_end(args); |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 60 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | return i; |
| 62 | } |