asprintf.c
Go to the documentation of this file.00001 #include <stdint.h>
00002 #include <stddef.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <errno.h>
00006
00007 FILE_LICENCE ( GPL2_OR_LATER );
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 int vasprintf ( char **strp, const char *fmt, va_list args ) {
00018 size_t len;
00019 va_list args_tmp;
00020
00021
00022 va_copy ( args_tmp, args );
00023 len = ( vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
00024 va_end ( args_tmp );
00025
00026
00027 *strp = malloc ( len );
00028 if ( ! *strp )
00029 return -ENOMEM;
00030 return vsnprintf ( *strp, len, fmt, args );
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 int asprintf ( char **strp, const char *fmt, ... ) {
00042 va_list args;
00043 int len;
00044
00045 va_start ( args, fmt );
00046 len = vasprintf ( strp, fmt, args );
00047 va_end ( args );
00048 return len;
00049 }