Commit cb6dbfe3 authored by Antonio Nino Diaz's avatar Antonio Nino Diaz
Browse files

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: default avatarAntonio Nino Diaz <antonio.ninodiaz@arm.com>
parent 93c78ed2
...@@ -23,20 +23,26 @@ ...@@ -23,20 +23,26 @@
(((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \ (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \
((_lcount) ? va_arg(_args, unsigned long int) : va_arg(_args, unsigned 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); assert(str);
while (*str) while (*str) {
putchar(*str++); putchar(*str++);
count++;
}
return count;
} }
static void unsigned_num_print(unsigned long long int unum, unsigned int radix, static int unsigned_num_print(unsigned long long int unum, unsigned int radix,
char padc, int padn) char padc, int padn)
{ {
/* Just need enough space to store 64 bit decimal integer */ /* Just need enough space to store 64 bit decimal integer */
unsigned char num_buf[20]; unsigned char num_buf[20];
int i = 0, rem; int i = 0, rem, count = 0;
do { do {
rem = unum % radix; rem = unum % radix;
...@@ -49,11 +55,16 @@ static void unsigned_num_print(unsigned long long int unum, unsigned int radix, ...@@ -49,11 +55,16 @@ static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
if (padn > 0) { if (padn > 0) {
while (i < padn--) { while (i < padn--) {
putchar(padc); putchar(padc);
count++;
} }
} }
while (--i >= 0) while (--i >= 0) {
putchar(num_buf[i]); putchar(num_buf[i]);
count++;
}
return count;
} }
/******************************************************************* /*******************************************************************
...@@ -76,7 +87,7 @@ static void unsigned_num_print(unsigned long long int unum, unsigned int radix, ...@@ -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 * The print exits on all other formats specifiers other than valid
* combinations of the above specifiers. * 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; int l_count;
long long int num; long long int num;
...@@ -84,6 +95,7 @@ void tf_vprintf(const char *fmt, va_list args) ...@@ -84,6 +95,7 @@ void tf_vprintf(const char *fmt, va_list args)
char *str; char *str;
char padc = 0; /* Padding character */ char padc = 0; /* Padding character */
int padn; /* Number of characters to pad */ int padn; /* Number of characters to pad */
int count = 0; /* Number of printed characters */
while (*fmt) { while (*fmt) {
l_count = 0; l_count = 0;
...@@ -104,24 +116,27 @@ loop: ...@@ -104,24 +116,27 @@ loop:
} else } else
unum = (unsigned long long int)num; unum = (unsigned long long int)num;
unsigned_num_print(unum, 10, padc, padn); count += unsigned_num_print(unum, 10,
padc, padn);
break; break;
case 's': case 's':
str = va_arg(args, char *); str = va_arg(args, char *);
tf_string_print(str); count += tf_string_print(str);
break; break;
case 'p': case 'p':
unum = (uintptr_t)va_arg(args, void *); unum = (uintptr_t)va_arg(args, void *);
if (unum) { if (unum) {
tf_string_print("0x"); count += tf_string_print("0x");
padn -= 2; padn -= 2;
} }
unsigned_num_print(unum, 16, padc, padn); count += unsigned_num_print(unum, 16,
padc, padn);
break; break;
case 'x': case 'x':
unum = get_unum_va_args(args, l_count); unum = get_unum_va_args(args, l_count);
unsigned_num_print(unum, 16, padc, padn); count += unsigned_num_print(unum, 16,
padc, padn);
break; break;
case 'z': case 'z':
if (sizeof(size_t) == 8) if (sizeof(size_t) == 8)
...@@ -135,7 +150,8 @@ loop: ...@@ -135,7 +150,8 @@ loop:
goto loop; goto loop;
case 'u': case 'u':
unum = get_unum_va_args(args, l_count); unum = get_unum_va_args(args, l_count);
unsigned_num_print(unum, 10, padc, padn); count += unsigned_num_print(unum, 10,
padc, padn);
break; break;
case '0': case '0':
padc = '0'; padc = '0';
...@@ -152,20 +168,26 @@ loop: ...@@ -152,20 +168,26 @@ loop:
} }
default: default:
/* Exit on any other format specifier */ /* Exit on any other format specifier */
return; return -1;
} }
fmt++; fmt++;
continue; continue;
} }
putchar(*fmt++); putchar(*fmt++);
count++;
} }
return count;
} }
void tf_printf(const char *fmt, ...) int tf_printf(const char *fmt, ...)
{ {
int count;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
tf_vprintf(fmt, va); count = tf_vprintf(fmt, va);
va_end(va); va_end(va);
return count;
} }
...@@ -90,10 +90,10 @@ void __dead2 do_panic(void); ...@@ -90,10 +90,10 @@ void __dead2 do_panic(void);
void __dead2 __stack_chk_fail(void); void __dead2 __stack_chk_fail(void);
void tf_log(const char *fmt, ...) __printflike(1, 2); 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); int tf_snprintf(char *s, size_t n, const char *fmt, ...) __printflike(3, 4);
void tf_vprintf(const char *fmt, va_list args); int tf_vprintf(const char *fmt, va_list args);
void tf_string_print(const char *str); int tf_string_print(const char *str);
void tf_log_set_max_level(unsigned int log_level); void tf_log_set_max_level(unsigned int log_level);
#endif /* __ASSEMBLY__ */ #endif /* __ASSEMBLY__ */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment