1 // lib/libc.cc -- Standard C-library functions
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
16 #include <kern/types.h>
17 #include <kern/libc.h>
22 // FIXME: Move printf to lib/kernel
24 static const int alt_form = 0x0001;
25 static const int zero_pad = 0x0002;
26 static const int neg_field = 0x0004;
27 static const int leave_blank = 0x0008;
28 static const int always_sign = 0x0010;
29 static const int group_thousands = 0x0020; // FIXME -- unimplemented
30 static const int long_arg = 0x0040;
31 static const int long_long_arg = 0x0080;
32 static const int short_arg = 0x0100;
33 static const int short_short_arg = 0x0200;
34 static const int intmax_arg = 0x0400;
35 static const int ptrdiff_arg = 0x0800;
36 static const int size_t_arg = 0x1000;
37 static const int capital_hex = 0x2000;
38 static const int num_signed = 0x4000;
39 static const int has_precision = 0x8000;
41 static void printf_string(char *buf, size_t &opos, size_t limit,
42 char *src, size_t len)
45 size_t olen = opos + len <= limit ? len : limit - opos;
46 memcpy(buf + opos, src, olen);
52 static void printf_fill(char *buf, size_t &opos, size_t limit,
56 size_t olen = opos + len <= limit ? len : limit - opos;
57 memset(buf + opos, ch, olen);
63 static void printf_num(char *obuf, size_t &opos, size_t limit,
64 s64 value, long radix, int fieldwidth,
65 int precision, int flags)
69 int letter = (flags & capital_hex) ? 'A' - 10 : 'a' - 10;
72 if (flags & num_signed)
73 uval = value < 0 ? -value : value;
77 // An explicit precision of 0 suppresses all output if the value
78 // is zero. Otherwise, the output size is not limited by precision
81 if (uval != 0 || !(flags & has_precision) || precision != 0) do {
82 int ch = uval % radix;
87 buf[pos] = ch + letter;
95 // length which counts against fieldwidth but not precision
98 if (flags & num_signed) {
100 printf_fill(obuf, opos, limit, '-', 1);
102 } else if (flags & always_sign) {
103 printf_fill(obuf, opos, limit, '+', 1);
105 } else if (flags & leave_blank) {
106 printf_fill(obuf, opos, limit, ' ', 1);
111 if ((flags & alt_form) && value != 0) {
112 if (radix == 8 && (!(flags & has_precision) || precision <= len)) {
113 flags |= has_precision;
118 printf_string(obuf, opos, limit, "0x", 2);
123 if ((flags & has_precision) && len < precision) {
132 if (!(flags & neg_field) && len < fieldwidth) {
133 char padchar = (flags & zero_pad) ? '0' : ' ';
134 printf_fill(obuf, opos, limit, padchar, fieldwidth - len);
138 if (precision != 0) {
139 printf_fill(obuf, opos, limit, '0', precision);
143 printf_string(obuf, opos, limit, buf + pos + 1, 64 - pos);
145 if ((flags & neg_field) && len < fieldwidth)
146 printf_fill(obuf, opos, limit, ' ', fieldwidth - len);
149 size_t vsnprintf(char *buf, size_t size, const char *str, va_list args)
151 size_t opos = 0; // position in the output string
152 unsigned int flags = 0;
158 for (size_t pos = 0; str[pos]; pos++) switch (state) {
160 if (str[pos] == '%') {
170 buf[opos] = str[pos];
175 case 1: // A percent has been seen; read in format characters
182 if (!(flags & has_precision)) {
190 if (flags & has_precision)
195 fieldwidth += str[pos++] - '0';
196 } while (str[pos] >= '0' && str[pos] <= '9');
202 if (fieldwidth || (flags & has_precision))
205 fieldwidth = va_arg(args, int);
209 flags |= has_precision;
211 if (str[pos + 1] == '*') {
213 precision = va_arg(args, int);
214 } else while (str[pos + 1] >= '0' && str[pos + 1] <= '9') {
216 precision += str[++pos] - '0';
226 flags |= leave_blank;
230 flags |= always_sign;
234 flags |= group_thousands;
238 if (flags & long_arg)
239 flags |= long_long_arg;
246 if (flags & long_arg)
247 flags |= short_short_arg;
258 flags |= ptrdiff_arg;
261 // Note that %z and other such "new" format characters are
262 // basically useless because some GCC coder actually went out
263 // of their way to make the compiler reject C99 format
264 // strings in C++ code, with no way of overriding it that I
265 // can find (the source code comments suggest the checking is
266 // only when using -pedantic, but I wasn't using -pedantic).
268 // Thus, we have the choice of either avoiding %z and friends
269 // (and possibly needing to insert hackish casts to silence
270 // the compiler's warnings if different architectures define
271 // types like size_t in different ways), or not using the
272 // format warnings at all.
274 // To mitigate this, 32-bit architectures should define
275 // pointer-sized special types as "long" rather than "int",
276 // so that %lx/%ld can always be used with them. Fixed-size
277 // 32-bit types should be declared as "int" rather than
278 // "long" for the same reason.
288 if ((flags & intmax_arg) || (flags & long_long_arg))
289 arg = va_arg(args, long long);
290 else if (flags & size_t_arg)
291 arg = va_arg(args, ssize_t);
292 else if (flags & ptrdiff_arg)
293 arg = va_arg(args, ptrdiff_t);
294 else if (flags & long_arg)
295 arg = va_arg(args, long);
296 else if (flags & short_short_arg)
297 arg = (signed char)va_arg(args, int);
298 else if (flags & short_arg)
299 arg = (short)va_arg(args, int);
301 arg = va_arg(args, int);
304 printf_num(buf, opos, size, arg, 10,
305 fieldwidth, precision, flags);
311 flags |= capital_hex;
325 if ((flags & intmax_arg) || (flags & long_long_arg))
326 arg = va_arg(args, unsigned long long);
327 else if (flags & size_t_arg)
328 arg = va_arg(args, size_t);
329 else if (flags & ptrdiff_arg)
330 arg = va_arg(args, intptr_t);
331 else if (flags & long_arg)
332 arg = va_arg(args, unsigned long);
333 else if (flags & short_short_arg)
334 arg = (unsigned char)va_arg(args, unsigned int);
335 else if (flags & short_arg)
336 arg = (unsigned short)va_arg(args, unsigned int);
337 else if (flags & short_short_arg)
338 arg = (signed char)va_arg(args, int);
339 else if (flags & short_arg)
340 arg = (short)va_arg(args, int);
342 arg = va_arg(args, unsigned int);
344 printf_num(buf, opos, size, arg, radix,
345 fieldwidth, precision, flags);
352 buf[opos] = va_arg(args, int);
359 char *arg = va_arg(args, char *);
364 size_t len = strlen(arg);
365 printf_string(buf, opos, size, arg, len);
371 void *arg = va_arg(args, void *);
373 printf_num(buf, opos, size, (ulong)arg, 16,
374 fieldwidth, precision, flags);
381 if ((flags & intmax_arg) || (flags & long_long_arg))
382 *va_arg(args, unsigned long long *) = opos;
383 else if (flags & size_t_arg)
384 *va_arg(args, ssize_t *) = opos;
385 else if (flags & ptrdiff_arg)
386 *va_arg(args, ptrdiff_t *) = opos;
387 else if (flags & long_arg)
388 *va_arg(args, long *) = opos;
389 else if (flags & short_short_arg)
390 *va_arg(args, signed char *) = opos;
391 else if (flags & short_arg)
392 *va_arg(args, short *) = opos;
394 *va_arg(args, int *) = opos;
400 default_case: // label for goto
403 buf[opos] = str[pos];
411 if (size > 0 && opos >= size)
417 size_t snprintf(char *buf, size_t size, const char *str, ...)
421 int ret = vsnprintf(buf, size, str, args);
426 size_t sprintf(char *buf, const char *str, ...)
430 int ret = vsnprintf(buf, ULONG_MAX, str, args);
435 void *memcpy(void *dest, const void *src, size_t len)
437 const char *cs = static_cast<const char *>(src);
438 char *cd = static_cast<char *>(dest);
440 for (size_t i = 0; i < len; i++)
446 void *memmove(void *dest, const void *src, size_t len)
449 return memcpy(dest, src, len);
451 const char *cs = static_cast<const char *>(src);
452 char *cd = static_cast<char *>(dest);
454 for (size_t i = len - 1; i >= 0; i--)
460 int memcmp(const void *b1, const void *b2, size_t len)
463 const char *c1 = static_cast<const char *>(b1);
464 const char *c2 = static_cast<const char *>(b2);
466 for (pos = 0; pos < len; pos++) {
467 if (c1[pos] != c2[pos])
468 return c1[pos] - c2[pos];
476 size_t strnlen(const char *s, size_t n)
479 while (pos < n && *s++)
484 size_t strlen(const char *s)
492 char *strcpy(char *dest, const char *src)
503 char *strncpy(char *dest, const char *src, size_t len)
518 void bzero(void *b, size_t len)
520 char *c = static_cast<char *>(b);
526 void *memset(void *b, int ch, size_t len)
528 char *c = static_cast<char *>(b);
536 #include <kern/pagealloc.h>
538 // Temporary hack until slab allocator is added
540 void *malloc(size_t len)
542 assert(len <= Arch::page_size - sizeof(size_t));
544 len = (len + sizeof(size_t) + Arch::page_size - 1) / Arch::page_size;
545 Mem::Page *page = Mem::PageAlloc::alloc(len);
547 size_t *ptr = (size_t *)Mem::page_to_kvirt(page);
553 void free(void *addr)
556 size_t *ptr = (size_t *)addr - 1;
558 Mem::Page *page = Mem::kvirt_to_page(addr);
559 Mem::PageAlloc::free(page, len);
563 void *operator new(size_t len)
568 void *operator new[](size_t len)
573 void operator delete(void *addr)
578 void operator delete[](void *addr)
583 extern "C" void __cxa_pure_virtual()
591 printf("abort() called in kernel\n");