From f600c79559892b73b019102478af501cf71fe6a4 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 28 May 2020 23:02:51 +0200 Subject: Added vsprintf support for serial connections --- src/kernel/lib/stdio/vsprintf.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/kernel/lib/stdio/vsprintf.c (limited to 'src/kernel/lib/stdio/vsprintf.c') diff --git a/src/kernel/lib/stdio/vsprintf.c b/src/kernel/lib/stdio/vsprintf.c new file mode 100644 index 0000000..6584ed1 --- /dev/null +++ b/src/kernel/lib/stdio/vsprintf.c @@ -0,0 +1,63 @@ +#include +#include +#include + +static void append(char *dest, char *src, int index) +{ + for (int i = index; i < strlen(src) + index; i++) + dest[i] = src[i - index]; + dest[index + strlen(src)] = 0; +} + +int vsprintf(char *str, const char *fmt, va_list args) +{ + u8 ready_to_format = 0; + + int i = 0; + char buf = 0; + char format_buffer[20] = "\0"; + + for (; *fmt; fmt++) { + if (ready_to_format) { + ready_to_format = 0; + + if (*fmt == '%') { + str[i] = '%'; + continue; + } + + buf = *fmt; + if (buf == 's') { + char *string = va_arg(args, char *); + append(str, string, i); + i = strlen(str); + } else if (buf == 'x') { + itoa_base((u32)va_arg(args, int), format_buffer, 16); + append(str, format_buffer, i); + i = strlen(str); + } else if (buf == 'd') { + itoa_base(va_arg(args, int), format_buffer, 10); + append(str, format_buffer, i); + i = strlen(str); + } else if (buf == 'o') { + itoa_base(va_arg(args, int), format_buffer, 8); + append(str, format_buffer, i); + i = strlen(str); + } else if (buf == 'c') { + str[i] = (char)va_arg(args, int); + i++; + } + } else { + if (*fmt == '%') + ready_to_format = 1; + else { + str[i] = *fmt; + i++; + } + } + + format_buffer[0] = '\0'; + } + + return strlen(str); +} \ No newline at end of file -- cgit v1.2.3