diff options
author | Raphael Herouart <rherouart@google.com> | 2023-06-12 13:00:30 +0000 |
---|---|---|
committer | Raphael Herouart <rherouart@google.com> | 2023-06-13 12:35:01 +0000 |
commit | fe29d82177f010e149cec97f22cf83b3129aebf5 (patch) | |
tree | 8da29857e439e0c8c6f5ffab5b5ebad560aa32ba /lib | |
parent | feb673846200152bec16df3b55967304f91ccb75 (diff) | |
download | common-fe29d82177f010e149cec97f22cf83b3129aebf5.tar.gz |
lib/libc: Update formatting for printf
- indirect width specification via %*s of %*d or alike was not supported
- Passing (NULL, 0, ...) to snprintf to know formatted string length
was not supported
These are necessary for running kernel Benchmarks
Bug: None
Change-Id: I3722847d33e8f51adbe474354340341fdfba5877
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/printf.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/libc/printf.c b/lib/libc/printf.c index 9723196e..a8b18ea5 100644 --- a/lib/libc/printf.c +++ b/lib/libc/printf.c @@ -26,6 +26,7 @@ #include <stdarg.h> #include <sys/types.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #ifdef UTEST_BUILD @@ -114,10 +115,13 @@ int vsnprintf(char *str, size_t len, const char *fmt, va_list ap) args.pos = 0; wlen = _printf_unfiltered_engine(&_vsnprintf_output, (void *)&args, fmt, ap); - if (args.pos >= len) - str[len-1] = '\0'; - else - str[wlen] = '\0'; + if(len > 0) { + if (args.pos >= len) + str[len-1] = '\0'; + else + str[wlen] = '\0'; + } + return wlen; } @@ -564,6 +568,16 @@ next_format: uc = va_arg(ap, unsigned int); OUTPUT_CHAR(uc); break; + case '*': + { + /* indirect format */ + int f = va_arg(ap, int); + format_num = (unsigned int) abs(f); + if(f < 0) { + flags |= LEFTFORMATFLAG; + } + goto next_format; + } case 's': s = va_arg(ap, const char *); if (s == 0) |