iPXE
asprintf.c
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 
7 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
8 
9 /**
10  * Write a formatted string to newly allocated memory.
11  *
12  * @v strp Pointer to hold allocated string
13  * @v fmt Format string
14  * @v args Arguments corresponding to the format string
15  * @ret len Length of formatted string
16  */
17 int vasprintf ( char **strp, const char *fmt, va_list args ) {
18  size_t len;
19  va_list args_tmp;
20 
21  /* Calculate length needed for string */
22  va_copy ( args_tmp, args );
23  len = ( vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
24  va_end ( args_tmp );
25 
26  /* Allocate and fill string */
27  *strp = malloc ( len );
28  if ( ! *strp )
29  return -ENOMEM;
30  return vsnprintf ( *strp, len, fmt, args );
31 }
32 
33 /**
34  * Write a formatted string to newly allocated memory.
35  *
36  * @v strp Pointer to hold allocated string
37  * @v fmt Format string
38  * @v ... Arguments corresponding to the format string
39  * @ret len Length of formatted string
40  */
41 int asprintf ( char **strp, const char *fmt, ... ) {
42  va_list args;
43  int len;
44 
45  va_start ( args, fmt );
46  len = vasprintf ( strp, fmt, args );
47  va_end ( args );
48  return len;
49 }
#define va_end(ap)
Definition: stdarg.h:9
Error codes.
#define va_copy(dest, src)
Definition: stdarg.h:10
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define ENOMEM
Not enough space.
Definition: errno.h:534
int vasprintf(char **strp, const char *fmt, va_list args)
Write a formatted string to newly allocated memory.
Definition: asprintf.c:17
int asprintf(char **strp, const char *fmt,...)
Write a formatted string to newly allocated memory.
Definition: asprintf.c:41
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
__builtin_va_list va_list
Definition: stdarg.h:6
int ssize_t const char * fmt
Definition: vsprintf.h:72
#define va_start(ap, last)
Definition: stdarg.h:7
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Write a formatted string to a buffer.
Definition: vsprintf.c:351