4848#include <stddef.h>
4949#include <stdio.h>
5050#include <limits.h>
51+ #include <stdlib.h> /* malloc */
5152
5253// Pick reasonable defaults if nothing's been configured.
5354#if !defined(NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS ) && \
@@ -640,6 +641,10 @@ static void npf_putc_std(int c, void *ctx) {
640641 outchar (c );
641642}
642643
644+ static void npf_fputc_std (int c , void * ctx ) {
645+ fputc (c , (FILE * )ctx );
646+ }
647+
643648typedef struct npf_cnt_putc_ctx {
644649 npf_putc pc ;
645650 void * ctx ;
@@ -978,10 +983,10 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
978983#undef NPF_WRITEBACK
979984
980985int _printf_c (char const * format , ...) {
981- va_list val ;
982- va_start (val , format );
983- int const rv = vprintf (format , val );
984- va_end (val );
986+ va_list va ;
987+ va_start (va , format );
988+ int const rv = vprintf (format , va );
989+ va_end (va );
985990 return rv ;
986991}
987992
@@ -1005,10 +1010,10 @@ int _vsnprintf_c(char *buffer, size_t bufsz, char const *format, va_list vlist)
10051010}
10061011
10071012int _snprintf_c (char * buffer , size_t bufsz , const char * format , ...) {
1008- va_list val ;
1009- va_start (val , format );
1010- int const rv = vsnprintf (buffer , bufsz , format , val );
1011- va_end (val );
1013+ va_list va ;
1014+ va_start (va , format );
1015+ int const rv = vsnprintf (buffer , bufsz , format , va );
1016+ va_end (va );
10121017 return rv ;
10131018}
10141019
@@ -1031,6 +1036,49 @@ int _sprintf_c(char *buffer, const char *format, ...)
10311036 return ret ;
10321037}
10331038
1039+ int _vasprintf_c (char * * __restrict p_str , const char * __restrict format , va_list vlist ) {
1040+ * p_str = NULL ;
1041+ int str_len = vsnprintf (NULL , 0 , format , vlist );
1042+ if (str_len <= 0 ) {
1043+ return str_len ;
1044+ }
1045+ size_t buf_len = (size_t )str_len + 1 ;
1046+ char * buf = (char * )malloc (buf_len );
1047+ if (buf == NULL ) {
1048+ // malloc failure
1049+ return -1 ;
1050+ }
1051+ int ret = vsnprintf (buf , buf_len , format , vlist );
1052+ if (ret <= 0 ) {
1053+ free (buf );
1054+ return ret ;
1055+ }
1056+ * p_str = buf ;
1057+ return ret ;
1058+ }
1059+
1060+ int _asprintf_c (char * * __restrict p_str , const char * __restrict format , ...) {
1061+ va_list va ;
1062+ va_start (va , format );
1063+ const int ret = vasprintf (p_str , format , va );
1064+ va_end (va );
1065+ return ret ;
1066+ }
1067+
1068+ int _vfprintf_c (FILE * __restrict stream , const char * __restrict format , va_list vlist )
1069+ {
1070+ return npf_vpprintf (npf_fputc_std , (void * )stream , format , vlist );
1071+ }
1072+
1073+ int _fprintf_c (FILE * __restrict stream , const char * __restrict format , ...)
1074+ {
1075+ va_list va ;
1076+ va_start (va , format );
1077+ const int ret = vfprintf (stream , format , va );
1078+ va_end (va );
1079+ return ret ;
1080+ }
1081+
10341082#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS
10351083 #pragma GCC diagnostic pop
10361084#endif
0 commit comments