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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 #include <ipxe/vsprintf.h>
29 #include <ipxe/efi/efi_strings.h>
31 /** Context used by efi_vsnprintf() and friends */
32 struct efi_sputc_context {
34 struct printf_context ctx;
35 /** Buffer for formatted string (used by efi_printf_sputc()) */
37 /** Buffer length (used by efi_printf_sputc())
39 * Note that this is a number of wide characters, not a number
46 * Write wide character to buffer
51 static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
52 struct efi_sputc_context * sctx =
53 container_of ( ctx, struct efi_sputc_context, ctx );
55 if ( ctx->len < sctx->max_wlen )
56 sctx->buf[ctx->len] = c;
60 * Write a formatted string to a wide-character buffer
62 * @v wbuf Buffer into which to write the string
63 * @v wsize Size of buffer (in wide characters)
64 * @v fmt Format string
65 * @v args Arguments corresponding to the format string
66 * @ret wlen Length of formatted string (in wide characters)
68 * If the buffer is too small to contain the string, the returned
69 * length is the length that would have been written had enough space
72 int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
74 struct efi_sputc_context sctx;
78 /* Hand off to vcprintf */
79 sctx.ctx.handler = efi_printf_sputc;
81 sctx.max_wlen = wsize;
82 wlen = vcprintf ( &sctx.ctx, fmt, args );
84 /* Add trailing NUL */
96 * Write a formatted string to a buffer
98 * @v wbuf Buffer into which to write the string
99 * @v wsize Size of buffer (in wide characters)
100 * @v fmt Format string
101 * @v ... Arguments corresponding to the format string
102 * @ret wlen Length of formatted string (in wide characters)
104 int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
108 va_start ( args, fmt );
109 i = efi_vsnprintf ( wbuf, wsize, fmt, args );
115 * Version of efi_vsnprintf() that accepts a signed buffer size
117 * @v wbuf Buffer into which to write the string
118 * @v swsize Size of buffer (in wide characters)
119 * @v fmt Format string
120 * @v args Arguments corresponding to the format string
121 * @ret wlen Length of formatted string (in wide characters)
123 int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
126 /* Treat negative buffer size as zero buffer size */
130 /* Hand off to vsnprintf */
131 return efi_vsnprintf ( wbuf, swsize, fmt, args );
135 * Version of efi_vsnprintf() that accepts a signed buffer size
137 * @v wbuf Buffer into which to write the string
138 * @v swsize Size of buffer (in wide characters)
139 * @v fmt Format string
140 * @v ... Arguments corresponding to the format string
141 * @ret wlen Length of formatted string (in wide characters)
143 int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
147 /* Hand off to vssnprintf */
148 va_start ( args, fmt );
149 len = efi_vssnprintf ( wbuf, swsize, fmt, args );