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