aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/stdio/vprintf.c
blob: 37723b2c7dd70e28c7819fa72a3d8ca2d6454b90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <lib/stdio.h>
#include <lib/stdlib.h>
#include <lib/string.h>
#include <memory/alloc.h>
#include <stdarg.h>
#include <stdint.h>

void _puts(const char *data)
{
	for (u32 i = 0; i < strlen(data); i++)
		putch(data[i]);
}

void vprintf(const char *fmt, va_list args)
{
	u8 readyToFormat = 0;

	char buff = 0;

	for (; *fmt; fmt++) {
		if (readyToFormat) {
			if (*fmt == '%') {
				putch('%');
				readyToFormat = 0;
				continue;
			}

			buff = *fmt;
			if (buff == 's') {
				const char *str = va_arg(args, const char *);
				_puts(str);
				readyToFormat = 0;
			} else if (buff == 'x') {
				char *p = htoa((u32)va_arg(args, int));
				_puts(p);
				kfree(p);
				readyToFormat = 0;
			} else if (buff == 'd') {
				char *p = itoa(va_arg(args, int));
				_puts(p);
				kfree(p);
				readyToFormat = 0;
			} else if (buff == 'c') {
				putch((char)va_arg(args, int));
				readyToFormat = 0;
			}
		} else {
			if (*fmt == '%')
				readyToFormat = 1;
			else
				putch(*fmt);
		}
	}
}