aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/std/printf.c
blob: 0bb5506769fab4f253207e1468e10e3ffbdd3fde (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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <limits.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

static bool print(const char* data, size_t length) {
	const unsigned char* bytes = (const unsigned char*) data;
	for (size_t i = 0; i < length; i++)
		if (putchar(bytes[i]) == EOF)
			return false;
	return true;
}

// for %d and %x.
static const char* itoa(unsigned int num, char* str, unsigned int base) {
	int cou = 0; // nt

    if (num == 0) { // 0
		str[cou] = '0';
		str[++cou] = '\0';
		return str;
	}

	while (num)
	{
		unsigned int rem = (num % base);
		// brings the number up to ascii 0 + the digit
		if (rem > 9) {
			str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65
		} else {
			str[cou] = rem + '0';
		}
		num /= base;
		++cou;
	}
	
	// reverse the string
	size_t start = 0;
	size_t end = cou - 1;
	while (start < end) {
		char temp = str[start];
		str[start] = str[end];
		str[end] = temp;
		++start;
		--end;
	}

	str[cou] = '\0';
	return str;
}

// have no idea if there's a better way to do this. for %lx
static const char* itoa_u64(unsigned long long num, char* str, int base) {
	size_t cou = 0; // nt

    if (num == 0) { // 0
		str[cou] = '0';
		str[++cou] = '\0';
		return str;
	}

	while (num)
	{
		unsigned long long rem = num % base;
		// brings the number up to ascii 0 + the digit
		if (rem > 9) {
			str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65
		} else {
			str[cou] = rem + '0';
		}
		num /= base;
		++cou;
	}
	
	// reverse the string
	size_t start = 0;
	size_t end = cou - 1;
	while (start < end) {
		char temp = str[start];
		str[start] = str[end];
		str[end] = temp;
		++start;
		--end;
	}

	str[cou] = '\0';
	return str;
}

int printf(const char* restrict format, ...) {
	va_list parameters;
	va_start(parameters, format);
 
	int written = 0;

	while (*format != '\0') {
		size_t maxrem = INT_MAX - written;
 
		if (format[0] != '%' || format[1] == '%') {
			if (format[0] == '%')
				format++;
			size_t amount = 1;
			while (format[amount] && format[amount] != '%')
				amount++;
			if (maxrem < amount) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(format, amount))
				return -1;
			format += amount;
			written += amount;
			continue;
		}

		const char* format_begun_at = format++;

		if (*format == 'c') {
			format++;
			char c = (char) va_arg(parameters, int /* char promotes to int */);
			if (!maxrem) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(&c, sizeof(c)))
				return -1;
			written++;
		} else if (*format == 'd') {
			format++;
			char str[20]; // probably a long enough buffer
			unsigned int d = va_arg(parameters, unsigned int);
			itoa(d, str, 10);
			size_t len = strlen(str);
			if (maxrem < len) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(str, len))
				return -1;
			written += len;
		} else if (*format == 'x') {
			format++;
			char str[20]; // probably a long enough buffer
			unsigned int d = va_arg(parameters, unsigned int);
			itoa(d, str, 16);
			size_t len = strlen(str);
			if (maxrem < len) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(str, len))
				return -1;
			written += len;
		} else if (*format == 'l') { // for %lx and others that start with l
			format++;
			if (*format == 'x') {
				format++;
				char str[20]; // probably a long enough buffer
				unsigned long long d = va_arg(parameters, unsigned long long);
				itoa_u64(d, str, 16);
				size_t len = strlen(str);
				if (maxrem < len) {
					// TODO: Set errno to EOVERFLOW.
					return -1;
				}
				if (!print(str, len))
					return -1;
				written += len;
			}
		} else if (*format == 's') {
			format++;
			const char* str = va_arg(parameters, const char*);
			size_t len = strlen(str);
			if (maxrem < len) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(str, len))
				return -1;
			written += len;
		} else {
			format = format_begun_at;
			size_t len = strlen(format);
			if (maxrem < len) {
				// TODO: Set errno to EOVERFLOW.
				return -1;
			}
			if (!print(format, len))
				return -1;
			written += len;
			format += len;
		}
	}

	va_end(parameters);
	return written;
}