Added new libc method(printf)

This commit is contained in:
ice-bit 2019-07-03 12:37:16 +02:00
parent 79817e6610
commit f11c690284
3 changed files with 63 additions and 2 deletions

51
kernel/libc/stdio.c Normal file
View File

@ -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;
}

10
kernel/libc/stdio.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _STDIO_H_
#define _STDIO_H
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
int printf(const char *format, ...);
#endif

View File

@ -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--;