vsprintf.c
1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 3 #include <console/vtxprintf.h> 4 #include <stdio.h> 5 6 struct vsnprintf_context { 7 char *str_buf; 8 size_t buf_limit; 9 }; 10 11 static void str_tx_byte(unsigned char byte, void *data) 12 { 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 } 19 } 20 21 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 22 { 23 int i; 24 struct vsnprintf_context ctx; 25 26 ctx.str_buf = buf; 27 ctx.buf_limit = size ? size - 1 : 0; 28 i = vtxprintf(str_tx_byte, fmt, args, &ctx); 29 if (size) 30 *ctx.str_buf = '\0'; 31 32 return i; 33 } 34 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); 42 va_end(args); 43 44 return i; 45 }