diff --git a/kernel/libc/stdio.c b/kernel/libc/stdio.c new file mode 100644 index 0000000..9a1fa7c --- /dev/null +++ b/kernel/libc/stdio.c @@ -0,0 +1,51 @@ +#include "stdio.h" +#include "string.h" +#include "drivers/shell.h" + + +int printf(const char *format, ...) { + char buf[20],c,*s; + int val; + int32_t uval; + va_list ap; + va_start(ap, format); + + for(size_t i = 0; i < strlen(format); i++) { + if(format[i] == '%') { + i++; + while(format[i] == ' ') + i++; + + switch(format[i]) { + case 'i': + val = va_arg(ap, int); + itoa(val, buf, 10); + printk(buf); + break; + case 'x': + uval = va_arg(ap, uint32_t); + uitoa(uval, buf, 16); + printk(buf); + break; + case 'd': + uval = va_arg(ap, uint32_t); + uitoa(uval, buf, 10); + printk(buf); + break; + case 'c': + s = va_arg(ap, char*); + printk_c(&c, 1); + break; + case 's': + s = va_arg(ap, char*); + printk(s); + break; + default: + printk_c((char*)format+1, 1); + } + } else + printk_c((char*)format+1, 1); + } + va_end(ap); + return 0; +} \ No newline at end of file diff --git a/kernel/libc/stdio.h b/kernel/libc/stdio.h new file mode 100644 index 0000000..57ed36e --- /dev/null +++ b/kernel/libc/stdio.h @@ -0,0 +1,10 @@ +#ifndef _STDIO_H_ +#define _STDIO_H + +#include +#include +#include + +int printf(const char *format, ...); + +#endif \ No newline at end of file diff --git a/kernel/libc/string.c b/kernel/libc/string.c index 3403364..599877f 100644 --- a/kernel/libc/string.c +++ b/kernel/libc/string.c @@ -35,7 +35,7 @@ char *itoa(int val, char *buf, int radix) { } while(x /= radix); char *s = buf+start; - char *s = buf+(i-1); + char *e = buf+(i-1); while(s < e) { char t = *s; @@ -70,7 +70,7 @@ char *uitoa(uint32_t val, char *buf, int radix) { while(s < e) { char t = *s; - *S = *e; + *s = *e; *e = t; s++; e--;