diff --git a/src/stdio.c b/src/stdio.c index 6b4c60d..f3c84f1 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -6,27 +6,27 @@ #include <../src/stdio_internal.h> - static int32_t rtstring_output_handler(void* user, const char* data, size_t size) { string_descriptor_t* const str_descriptor = (string_descriptor_t*)user; - - const size_t len = size < (str_descriptor->len - 1) ? size : (str_descriptor->len - 1); - - if (len) + if (str_descriptor->ptr && str_descriptor->len) { - str_descriptor->len -= len; + const size_t len = size < (str_descriptor->len - 1) ? size : (str_descriptor->len - 1); - memcpy(str_descriptor->ptr, data, len); - str_descriptor->ptr += len; - *str_descriptor->ptr = 0; + if (len) + { + str_descriptor->len -= len; + + memcpy(str_descriptor->ptr, data, len); + str_descriptor->ptr += len; + *str_descriptor->ptr = 0; + } } return size; } - int32_t sprintf(char* str, const char* format, ...) { va_list args; @@ -42,21 +42,14 @@ int32_t snprintf(char* str, size_t n, const char* format, ...) { - if (n > 0) - { - va_list args; - va_start(args, format); + va_list args; + va_start(args, format); - string_descriptor_t str_descriptor = {str, n}; - const int32_t rv = rtprintf(rtstring_output_handler, &str_descriptor, format, args); + string_descriptor_t str_descriptor = {str, n}; + const int32_t rv = rtprintf(rtstring_output_handler, &str_descriptor, format, args); - va_end(args); - return rv; - } - else - { - return 0; - } + va_end(args); + return rv; } @@ -67,22 +60,13 @@ } - int32_t vsnprintf(char* str, size_t n, const char* format, va_list args) { - if (n > 0) - { - string_descriptor_t str_descriptor = {str, n}; - return rtprintf(rtstring_output_handler, &str_descriptor, format, args); - } - else - { - return 0; - } + string_descriptor_t str_descriptor = {str, n}; + return rtprintf(rtstring_output_handler, &str_descriptor, format, args); } - //simplified versions int32_t isprintf(char *str, const char *format, ...) {