]> git.baikalelectronics.ru Git - arm-tf.git/commitdiff
feat(snprintf): add support for length specifiers
authorkadabi <kadabi@google.com>
Mon, 28 Feb 2022 22:21:09 +0000 (14:21 -0800)
committerMadhukar Pappireddy <madhukar.pappireddy@arm.com>
Mon, 9 May 2022 23:01:12 +0000 (01:01 +0200)
Add long, long long and size_t length specifiers to
snprintf similar to vprintf. This will help capturing
all the UART logs into a logbuffer and makes snprintf
functionally equivalent to vprintf.

Change-Id: Ib9bd20e2b040c9b8755cf7ed7c9b4da555604810
Signed-off-by: Channagoud kadabi <kadabi@google.com>
lib/libc/snprintf.c

index 675d2430d3b896c1199975e61c6b912112c294ac..12f51c07fc9db2ef44b42a2e3feee212b68eadc5 100644 (file)
 #include <common/debug.h>
 #include <plat/common/platform.h>
 
+#define get_num_va_args(_args, _lcount)                                \
+       (((_lcount) > 1)  ? va_arg(_args, long long int) :      \
+       (((_lcount) == 1) ? va_arg(_args, long int) :           \
+                           va_arg(_args, int)))
+
+#define get_unum_va_args(_args, _lcount)                               \
+       (((_lcount) > 1)  ? va_arg(_args, unsigned long long int) :     \
+       (((_lcount) == 1) ? va_arg(_args, unsigned long int) :          \
+                           va_arg(_args, unsigned int)))
+
 #define CHECK_AND_PUT_CHAR(buf, size, chars_printed, ch)       \
        do {                                            \
                if ((chars_printed) < (size)) {         \
@@ -80,6 +90,11 @@ static void unsigned_num_print(char **s, size_t n, size_t *chars_printed,
  * %u - unsigned decimal format
  * %p - pointer format
  *
+ * The following length specifiers are supported by this print
+ * %l - long int
+ * %ll - long long int
+ * %z - size_t sized integer formats
+ *
  * The following padding specifiers are supported by this print
  * %0NN - Left-pad the number with 0s (NN is a decimal number)
  * %NN - Left-pad the number or string with spaces (NN is a decimal number)
@@ -101,6 +116,7 @@ int vsnprintf(char *s, size_t n, const char *fmt, va_list args)
        bool left;
        bool capitalise;
        size_t chars_printed = 0U;
+       unsigned int l_count;
 
        if (n == 0U) {
                /* There isn't space for anything. */
@@ -118,6 +134,7 @@ int vsnprintf(char *s, size_t n, const char *fmt, va_list args)
                padc ='\0';
                padn = 0;
                capitalise = false;
+               l_count = 0;
 
                if (*fmt == '%') {
                        fmt++;
@@ -152,7 +169,7 @@ loop:
 
                        case 'i':
                        case 'd':
-                               num = va_arg(args, int);
+                               num = get_num_va_args(args, l_count);
 
                                if (num < 0) {
                                        CHECK_AND_PUT_CHAR(s, n, chars_printed,
@@ -170,10 +187,18 @@ loop:
                                string_print(&s, n, &chars_printed, str);
                                break;
                        case 'u':
-                               unum = va_arg(args, unsigned int);
+                               unum = get_unum_va_args(args, l_count);
                                unsigned_num_print(&s, n, &chars_printed,
                                                   unum, 10, padc, padn, false);
                                break;
+                       case 'z':
+                               l_count = 1;
+                               fmt++;
+                               goto loop;
+                       case 'l':
+                               l_count++;
+                               fmt++;
+                               goto loop;
                        case 'p':
                                unum = (uintptr_t)va_arg(args, void *);
                                if (unum > 0U) {
@@ -186,7 +211,7 @@ loop:
                        case 'X':
                                capitalise = true;
                        case 'x':
-                               unum = va_arg(args, unsigned int);
+                               unum = get_unum_va_args(args, l_count);
                                unsigned_num_print(&s, n, &chars_printed,
                                                   unum, 16, padc, padn,
                                                   capitalise);