From cb6dbfe3dc56088895c06b0492cfce289288444e Mon Sep 17 00:00:00 2001 From: Antonio Nino Diaz Date: Wed, 15 Aug 2018 16:52:32 +0100 Subject: [PATCH] tf_printf: Return number of printed characters The C standard says that printf() has to return the number of characters it has printed. Change-Id: I0ef50b1d6766d140724ac0a2fa2c5d023431f984 Signed-off-by: Antonio Nino Diaz --- common/tf_printf.c | 54 +++++++++++++++++++++++++++++------------- include/common/debug.h | 6 ++--- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/common/tf_printf.c b/common/tf_printf.c index 9d8333a6..ecf058ca 100644 --- a/common/tf_printf.c +++ b/common/tf_printf.c @@ -23,20 +23,26 @@ (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \ ((_lcount) ? va_arg(_args, unsigned long int) : va_arg(_args, unsigned int))) -void tf_string_print(const char *str) +int tf_string_print(const char *str) { + int count = 0; + assert(str); - while (*str) + while (*str) { putchar(*str++); + count++; + } + + return count; } -static void unsigned_num_print(unsigned long long int unum, unsigned int radix, - char padc, int padn) +static int unsigned_num_print(unsigned long long int unum, unsigned int radix, + char padc, int padn) { /* Just need enough space to store 64 bit decimal integer */ unsigned char num_buf[20]; - int i = 0, rem; + int i = 0, rem, count = 0; do { rem = unum % radix; @@ -49,11 +55,16 @@ static void unsigned_num_print(unsigned long long int unum, unsigned int radix, if (padn > 0) { while (i < padn--) { putchar(padc); + count++; } } - while (--i >= 0) + while (--i >= 0) { putchar(num_buf[i]); + count++; + } + + return count; } /******************************************************************* @@ -76,7 +87,7 @@ static void unsigned_num_print(unsigned long long int unum, unsigned int radix, * The print exits on all other formats specifiers other than valid * combinations of the above specifiers. *******************************************************************/ -void tf_vprintf(const char *fmt, va_list args) +int tf_vprintf(const char *fmt, va_list args) { int l_count; long long int num; @@ -84,6 +95,7 @@ void tf_vprintf(const char *fmt, va_list args) char *str; char padc = 0; /* Padding character */ int padn; /* Number of characters to pad */ + int count = 0; /* Number of printed characters */ while (*fmt) { l_count = 0; @@ -104,24 +116,27 @@ loop: } else unum = (unsigned long long int)num; - unsigned_num_print(unum, 10, padc, padn); + count += unsigned_num_print(unum, 10, + padc, padn); break; case 's': str = va_arg(args, char *); - tf_string_print(str); + count += tf_string_print(str); break; case 'p': unum = (uintptr_t)va_arg(args, void *); if (unum) { - tf_string_print("0x"); + count += tf_string_print("0x"); padn -= 2; } - unsigned_num_print(unum, 16, padc, padn); + count += unsigned_num_print(unum, 16, + padc, padn); break; case 'x': unum = get_unum_va_args(args, l_count); - unsigned_num_print(unum, 16, padc, padn); + count += unsigned_num_print(unum, 16, + padc, padn); break; case 'z': if (sizeof(size_t) == 8) @@ -135,7 +150,8 @@ loop: goto loop; case 'u': unum = get_unum_va_args(args, l_count); - unsigned_num_print(unum, 10, padc, padn); + count += unsigned_num_print(unum, 10, + padc, padn); break; case '0': padc = '0'; @@ -152,20 +168,26 @@ loop: } default: /* Exit on any other format specifier */ - return; + return -1; } fmt++; continue; } putchar(*fmt++); + count++; } + + return count; } -void tf_printf(const char *fmt, ...) +int tf_printf(const char *fmt, ...) { + int count; va_list va; va_start(va, fmt); - tf_vprintf(fmt, va); + count = tf_vprintf(fmt, va); va_end(va); + + return count; } diff --git a/include/common/debug.h b/include/common/debug.h index f60dfc17..3c99ce50 100644 --- a/include/common/debug.h +++ b/include/common/debug.h @@ -90,10 +90,10 @@ void __dead2 do_panic(void); void __dead2 __stack_chk_fail(void); void tf_log(const char *fmt, ...) __printflike(1, 2); -void tf_printf(const char *fmt, ...) __printflike(1, 2); +int tf_printf(const char *fmt, ...) __printflike(1, 2); int tf_snprintf(char *s, size_t n, const char *fmt, ...) __printflike(3, 4); -void tf_vprintf(const char *fmt, va_list args); -void tf_string_print(const char *str); +int tf_vprintf(const char *fmt, va_list args); +int tf_string_print(const char *str); void tf_log_set_max_level(unsigned int log_level); #endif /* __ASSEMBLY__ */ -- 2.30.2