mirror of
https://github.com/ipxe/ipxe
synced 2025-12-28 02:28:57 +03:00
[efi] Add efi_asprintf() and efi_vasprintf()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -25,6 +25,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <ipxe/vsprintf.h>
|
||||
#include <ipxe/efi/efi_strings.h>
|
||||
|
||||
@@ -150,3 +152,45 @@ int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
|
||||
va_end ( args );
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a formatted string to newly allocated memory
|
||||
*
|
||||
* @v wstrp Pointer to hold allocated string
|
||||
* @v fmt Format string
|
||||
* @v args Arguments corresponding to the format string
|
||||
* @ret len Length of formatted string (in wide characters)
|
||||
*/
|
||||
int efi_vasprintf ( wchar_t **wstrp, const char *fmt, va_list args ) {
|
||||
size_t len;
|
||||
va_list args_tmp;
|
||||
|
||||
/* Calculate length needed for string */
|
||||
va_copy ( args_tmp, args );
|
||||
len = ( efi_vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
|
||||
va_end ( args_tmp );
|
||||
|
||||
/* Allocate and fill string */
|
||||
*wstrp = malloc ( len * sizeof ( **wstrp ) );
|
||||
if ( ! *wstrp )
|
||||
return -ENOMEM;
|
||||
return efi_vsnprintf ( *wstrp, len, fmt, args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a formatted string to newly allocated memory
|
||||
*
|
||||
* @v wstrp Pointer to hold allocated string
|
||||
* @v fmt Format string
|
||||
* @v ... Arguments corresponding to the format string
|
||||
* @ret len Length of formatted string (in wide characters)
|
||||
*/
|
||||
int efi_asprintf ( wchar_t **wstrp, const char *fmt, ... ) {
|
||||
va_list args;
|
||||
int len;
|
||||
|
||||
va_start ( args, fmt );
|
||||
len = efi_vasprintf ( wstrp, fmt, args );
|
||||
va_end ( args );
|
||||
return len;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user