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 FILE_SECBOOT ( PERMITTED );
9 
10 /**
11  * Write a formatted string to newly allocated memory.
12  *
13  * @v strp Pointer to hold allocated string
14  * @v fmt Format string
15  * @v args Arguments corresponding to the format string
16  * @ret len Length of formatted string
17  */
18 int vasprintf ( char **strp, const char *fmt, va_list args ) {
19  size_t len;
20  va_list args_tmp;
21 
22  /* Calculate length needed for string */
23  va_copy ( args_tmp, args );
24  len = ( vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
25  va_end ( args_tmp );
26 
27  /* Allocate and fill string */
28  *strp = malloc ( len );
29  if ( ! *strp )
30  return -ENOMEM;
31  return vsnprintf ( *strp, len, fmt, args );
32 }
33 
34 /**
35  * Write a formatted string to newly allocated memory.
36  *
37  * @v strp Pointer to hold allocated string
38  * @v fmt Format string
39  * @v ... Arguments corresponding to the format string
40  * @ret len Length of formatted string
41  */
42 int asprintf ( char **strp, const char *fmt, ... ) {
43  va_list args;
44  int len;
45 
46  va_start ( args, fmt );
47  len = vasprintf ( strp, fmt, args );
48  va_end ( args );
49  return len;
50 }
#define va_end(ap)
Definition: stdarg.h:10
Error codes.
#define va_copy(dest, src)
Definition: stdarg.h:11
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define ENOMEM
Not enough space.
Definition: errno.h:535
ring len
Length.
Definition: dwmac.h:231
int vasprintf(char **strp, const char *fmt, va_list args)
Write a formatted string to newly allocated memory.
Definition: asprintf.c:18
int asprintf(char **strp, const char *fmt,...)
Write a formatted string to newly allocated memory.
Definition: asprintf.c:42
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:621
__builtin_va_list va_list
Definition: stdarg.h:7
int ssize_t const char * fmt
Definition: vsprintf.h:73
#define va_start(ap, last)
Definition: stdarg.h:8
FILE_SECBOOT(PERMITTED)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Write a formatted string to a buffer.
Definition: vsprintf.c:352