diff --git a/CMakeLists.txt b/CMakeLists.txt index b41029d..c012c1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ set(SOURCES_COMMON src/rtc/rtc.c src/rtc/rtc_ticks.c src/spu/spu.c + src/std/aprint.c src/std/memcmp.s src/std/memcpy.s src/std/memmove.s diff --git a/include/gint/std/stdio.h b/include/gint/std/stdio.h index c94a86b..4fe37cb 100644 --- a/include/gint/std/stdio.h +++ b/include/gint/std/stdio.h @@ -17,7 +17,7 @@ * Parameter length (hh, h, l, ll, z) * Limiting the size of the output and still returning the whole length * If kprint_enable_fp() from is called: floating-point - formats (%e, %E, %f, %F, %g, %G) (disabled by default for space) + formats (%e, %E, %f, %F, %g, %G) (disabled by default to save space) They do not support: * Hexadecimal floating-point (%a, %A) @@ -41,5 +41,9 @@ int vsprintf(char *str, char const *format, va_list args); int snprintf(char *str, size_t n, char const *format, ...); /* Print to string with limited size from va_list */ int vsnprintf(char *str, size_t n, char const *format, va_list args); +/* Print to auto-allocated string */ +int asprintf(char **strp, char const *format, ...); +/* Print to auto-allocated string from va_list */ +int vasprintf(char **strp, char const *format, va_list args); #endif /* GINT_STD_STDIO */ diff --git a/src/std/aprint.c b/src/std/aprint.c new file mode 100644 index 0000000..9808122 --- /dev/null +++ b/src/std/aprint.c @@ -0,0 +1,38 @@ +//--- +// gint:src:aprint - Allocating extensions to formatted printing +//--- + +#include +#include +#include +#include + +/* asprintf() */ +GWEAK 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; +} + +/* vasprintf() */ +GWEAK 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; +}