stdio: add asprintf and vasprintf

pull/2/head
Lephenixnoir 2 years ago
parent f52e0923bc
commit 625a6af459
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495

@ -114,6 +114,7 @@ set(SOURCES
src/libc/signal/signal.c
src/libc/signal/raise.c
# stdio
src/libc/stdio/asprintf.c
src/libc/stdio/dprintf.c
src/libc/stdio/fprintf.c
src/libc/stdio/printf.c
@ -124,6 +125,7 @@ set(SOURCES
src/libc/stdio/puts.c
src/libc/stdio/snprintf.c
src/libc/stdio/sprintf.c
src/libc/stdio/vasprintf.c
src/libc/stdio/vdprintf.c
src/libc/stdio/vfprintf.c
src/libc/stdio/vprintf.c

@ -81,4 +81,11 @@ extern int dprintf(int __fd, char const * restrict __format, ...);
/* Formatted print to file descriptor (variable argument list). */
extern int vdprintf(int __fd, char const * restrict __format, va_list __args);
/* Allocating sprintf(). */
extern int asprintf(char ** restrict __str,char const * restrict __format,...);
/* Allocating vsprintf(). */
extern int vasprintf(char ** restrict __str, char const * restrict __format,
va_list __args);
#endif /*__STDIO_H__*/

@ -0,0 +1,13 @@
#include <stdio.h>
int asprintf(char **strp, char const *format, ...)
{
va_list args;
va_start(args, format);
int count = vasprintf(strp, format, args);
va_end(args);
return count;
}

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
int vasprintf(char **strp, char const *format, va_list args1)
{
va_list args2;
va_copy(args2, args1);
int count = vsnprintf(NULL, 0, format, args1);
va_end(args1);
char *str = malloc(count + 1);
if(str) count = vsnprintf(str, count + 1, format, args2);
va_end(args2);
if(!str) return -1;
*strp = str;
return count;
}
Loading…
Cancel
Save