summaryrefslogtreecommitdiff
path: root/src/init/stdlib.c
blob: 82410d6c3a001f358408bff035481a465dbcd248 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <init/stdlib.h>
#include <shared/syscalls.h>
#include <stdarg.h>

int __stdin  = -1;
int __stdout = -1;

static void backend_file(void *arg, const char *buf, size_t len) {
	_syscall_write(*(handle_t*)arg, buf, len, -1);
}

static int __printf_internal(const char *fmt, va_list argp,
		void (*back)(void*, const char*, size_t), void *back_arg)
{
	const char *seg = fmt; // beginning of the current segment
	int total = 0;

	for (;;) {
		char c = *fmt++;
		switch (c) {
			case '\0':
				back(back_arg, seg, fmt - seg - 1);
				return total + (fmt - seg - 1);

			case '%':
				back(back_arg, seg, fmt - seg - 1);
				total += fmt - seg - 1;
				size_t pad_len = 0;

				c = *fmt++;
				while (1) {
					switch (c) {
						case '0':
							pad_len = *fmt++ - '0'; // can skip over the null byte, idc
							break;
						default:
							goto modifier_break;
					}
					c = *fmt++;
				}
modifier_break:
				switch (c) {
					case 'c':
						char c = va_arg(argp, int);
						back(back_arg, &c, 1);
						total += 1;
						break;

					case 's':
						const char *s = va_arg(argp, char*);
						if (s) {
							back(back_arg, s, strlen(s));
							total += strlen(s);
						}
						break;

					case 'x':
						unsigned int n = va_arg(argp, int);
						size_t i = 4; // nibbles * 4
						while (n >> i && i < (sizeof(int) * 8))
							i += 4;

						if (i < pad_len * 4)
							i = pad_len * 4;

						while (i > 0) {
							i -= 4;
							char h = '0' + ((n >> i) & 0xf);
							if (h > '9') h += 'a' - '9' - 1;
							back(back_arg, &h, 1);
							total++;
						}
						break;
				}
				seg = fmt;
				break;
		}
	}
}

int printf(const char *fmt, ...) {
	int ret = 0;
	va_list argp;
	va_start(argp, fmt);
	if (__stdout < 0) goto end;
	__printf_internal(fmt, argp, backend_file, (void*)&__stdout);
end:
	va_end(argp);
	return ret;
}