2 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
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; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
24 #include <ipxe/vsprintf.h>
25 #include <ipxe/efi/efi_strings.h>
27 /** Context used by efi_vsnprintf() and friends */
28 struct efi_sputc_context {
30 struct printf_context ctx;
31 /** Buffer for formatted string (used by efi_printf_sputc()) */
33 /** Buffer length (used by efi_printf_sputc())
35 * Note that this is a number of wide characters, not a number
42 * Write wide character to buffer
47 static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
48 struct efi_sputc_context * sctx =
49 container_of ( ctx, struct efi_sputc_context, ctx );
51 if ( ctx->len < sctx->max_wlen )
52 sctx->buf[ctx->len] = c;
56 * Write a formatted string to a wide-character buffer
58 * @v wbuf Buffer into which to write the string
59 * @v wsize Size of buffer (in wide characters)
60 * @v fmt Format string
61 * @v args Arguments corresponding to the format string
62 * @ret wlen Length of formatted string (in wide characters)
64 * If the buffer is too small to contain the string, the returned
65 * length is the length that would have been written had enough space
68 int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
70 struct efi_sputc_context sctx;
74 /* Hand off to vcprintf */
75 sctx.ctx.handler = efi_printf_sputc;
77 sctx.max_wlen = wsize;
78 wlen = vcprintf ( &sctx.ctx, fmt, args );
80 /* Add trailing NUL */
92 * Write a formatted string to a buffer
94 * @v wbuf Buffer into which to write the string
95 * @v wsize Size of buffer (in wide characters)
96 * @v fmt Format string
97 * @v ... Arguments corresponding to the format string
98 * @ret wlen Length of formatted string (in wide characters)
100 int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
104 va_start ( args, fmt );
105 i = efi_vsnprintf ( wbuf, wsize, fmt, args );
111 * Version of efi_vsnprintf() that accepts a signed buffer size
113 * @v wbuf Buffer into which to write the string
114 * @v swsize Size of buffer (in wide characters)
115 * @v fmt Format string
116 * @v args Arguments corresponding to the format string
117 * @ret wlen Length of formatted string (in wide characters)
119 int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
122 /* Treat negative buffer size as zero buffer size */
126 /* Hand off to vsnprintf */
127 return efi_vsnprintf ( wbuf, swsize, fmt, args );
131 * Version of efi_vsnprintf() that accepts a signed buffer size
133 * @v wbuf Buffer into which to write the string
134 * @v swsize Size of buffer (in wide characters)
135 * @v fmt Format string
136 * @v ... Arguments corresponding to the format string
137 * @ret wlen Length of formatted string (in wide characters)
139 int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
143 /* Hand off to vssnprintf */
144 va_start ( args, fmt );
145 len = efi_vssnprintf ( wbuf, swsize, fmt, args );