From 44d9706e5428d8e3588d04565c7cd738ffc1e472 Mon Sep 17 00:00:00 2001
From: Maksims Svecovs <maksims.svecovs@arm.com>
Date: Tue, 9 May 2023 12:03:31 +0100
Subject: [PATCH] feat(libc): add %c to printf/snprintf

Adds %c support for printf and snprintf to print one character. Required
by most recent MbedTLS 3.4.0.

Change-Id: I4d9b2725127a929d58946353324f99ff22b3b28b
Signed-off-by: Maksims Svecovs <maksims.svecovs@arm.com>
---
 lib/libc/printf.c   | 7 ++++++-
 lib/libc/snprintf.c | 6 +++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/lib/libc/printf.c b/lib/libc/printf.c
index e52cbed73..faccfdff4 100644
--- a/lib/libc/printf.c
+++ b/lib/libc/printf.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2021, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2014-2023, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -81,6 +81,7 @@ static int unsigned_num_print(unsigned long long int unum, unsigned int radix,
  * %x - hexadecimal format
  * %s - string format
  * %d or %i - signed decimal format
+ * %c - character format
  * %u - unsigned decimal format
  * %p - pointer format
  *
@@ -130,6 +131,10 @@ loop:
 				count += unsigned_num_print(unum, 10,
 							    padc, padn);
 				break;
+			case 'c':
+				(void)putchar(va_arg(args, int));
+				count++;
+				break;
 			case 's':
 				str = va_arg(args, char *);
 				count += string_print(str);
diff --git a/lib/libc/snprintf.c b/lib/libc/snprintf.c
index 0e3256cde..21d34168f 100644
--- a/lib/libc/snprintf.c
+++ b/lib/libc/snprintf.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -85,6 +85,7 @@ static void unsigned_num_print(char **s, size_t n, size_t *chars_printed,
  *
  * %x (or %X) - hexadecimal format
  * %d or %i - signed decimal format
+ * %c - character format
  * %s - string format
  * %u - unsigned decimal format
  * %p - pointer format
@@ -181,6 +182,9 @@ loop:
 				unsigned_num_print(&s, n, &chars_printed,
 						   unum, 10, padc, padn, false);
 				break;
+			case 'c':
+				CHECK_AND_PUT_CHAR(s, n, chars_printed, va_arg(args, int));
+				break;
 			case 's':
 				str = va_arg(args, char *);
 				string_print(&s, n, &chars_printed, str);