diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2021-04-09 10:29:23 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2021-04-09 10:29:23 +0200 |
| commit | f6323421e22c3585c58b54658a11e1e4706cc8fa (patch) | |
| tree | f317b7129dc4350fd088b59a43c1b6e80cd5c39b /src/libc | |
| parent | 8fee35522dee033863f68c1d2b45f5fe988de9eb (diff) | |
Cleaning code and provide minimal libc
Diffstat (limited to 'src/libc')
| -rw-r--r-- | src/libc/math.c | 14 | ||||
| -rw-r--r-- | src/libc/math.h | 6 | ||||
| -rw-r--r-- | src/libc/stdio.c | 25 | ||||
| -rw-r--r-- | src/libc/stdio.h | 10 | ||||
| -rw-r--r-- | src/libc/string.c | 29 | ||||
| -rw-r--r-- | src/libc/string.h | 6 |
6 files changed, 90 insertions, 0 deletions
diff --git a/src/libc/math.c b/src/libc/math.c new file mode 100644 index 0000000..2051dc1 --- /dev/null +++ b/src/libc/math.c @@ -0,0 +1,14 @@ +#include "math.h" + +int pow(int x,int n){ + if(n<0) + return -1; + else if(n==0) + return 1; + else if(n==1) + return x; + int ret=x; + for(int i=0;i<(n-1);i++) + ret*=x; + return ret; +}
\ No newline at end of file diff --git a/src/libc/math.h b/src/libc/math.h new file mode 100644 index 0000000..56f1788 --- /dev/null +++ b/src/libc/math.h @@ -0,0 +1,6 @@ +#ifndef MATH_H +#define MATH_H + +int pow(int x,int n); + +#endif
\ No newline at end of file diff --git a/src/libc/stdio.c b/src/libc/stdio.c new file mode 100644 index 0000000..6e0063b --- /dev/null +++ b/src/libc/stdio.c @@ -0,0 +1,25 @@ +#include "stdio.h" +#include "string.h" + +extern VIDEO_STATE VS; + +void print(char *str){ + int i=0; + while(str[i]!='\0'){ + putchar(str[i]); + i++; + } +} + +void printc(char* str,VIDEO_COLORS c){ + VIDEO_COLORS backup=VS.fg; + VS.fg=c; + print(str); + VS.fg=backup; +} + +void printi(int i){ + char str[12]; + itoa(i,str); + print(str); +}
\ No newline at end of file diff --git a/src/libc/stdio.h b/src/libc/stdio.h new file mode 100644 index 0000000..ece8df5 --- /dev/null +++ b/src/libc/stdio.h @@ -0,0 +1,10 @@ +#ifndef STDIO_H +#define STDIO_H + +#include "utils/framebuffer.h" + +void print(char*); +void printc(char*,VIDEO_COLORS c); +void printi(int i); + +#endif
\ No newline at end of file diff --git a/src/libc/string.c b/src/libc/string.c new file mode 100644 index 0000000..93a9e63 --- /dev/null +++ b/src/libc/string.c @@ -0,0 +1,29 @@ +#include "string.h" +#include "math.h" + +void itoa(int i, char *a){ + // Check if lower than 0 + char neg=0; + if(i<0){ + neg=1; + i=-i; + a[0]='-'; + } + + // Count number of digits + int len=1; + while(i/pow(10,len)>=1) + { + len++; + } + + // Build string + int max_pow=len-1; + for(int j=0;j<=max_pow;j++){ + int cur_pow=pow(10,max_pow-j); + char digit=i/cur_pow; + a[j+neg]='0'+digit; + i=i-digit*cur_pow; // Remove first digits (most significant) + } + a[len+neg]='\0'; +}
\ No newline at end of file diff --git a/src/libc/string.h b/src/libc/string.h new file mode 100644 index 0000000..82106b2 --- /dev/null +++ b/src/libc/string.h @@ -0,0 +1,6 @@ +#ifndef STRING_H +#define STRING_H + +void itoa(int i, char *a); + +#endif
\ No newline at end of file |
