Added multiboot header, ports methods and added new part of libc(string)

This commit is contained in:
ice-bit
2019-07-03 12:15:40 +02:00
parent 7650b9b8a2
commit 79817e6610
7 changed files with 223 additions and 0 deletions

124
kernel/libc/string.c Normal file
View File

@@ -0,0 +1,124 @@
#include "string.h"
// C library implementation
int32_t strcmp(const char *s1, const char *s2) {
while ((*s1) && (*s1 == *s2)) {
s1++;
s2++;
}
return (*(uint8_t*)s1 - *(uint8_t*)s2);
}
char *itoa(int val, char *buf, int radix) {
uint32_t i = 0;
uint32_t start = i;
if(val < 0 && radix == 10) {
buf[i++] = '-';
start = i;
}
if(radix == 10) {
buf[i++] = '0';
buf[i++] = 'x';
start = i;
}
int x = val;
do {
int a = x % radix;
if(a < 10)
buf[i++] = a + '0';
else
buf[i++]= a + 'a' - 10;
} while(x /= radix);
char *s = buf+start;
char *s = buf+(i-1);
while(s < e) {
char t = *s;
*s = *e;
*e = t;
i++;
e--;
}
}
char *uitoa(uint32_t val, char *buf, int radix) {
uint32_t i = 0;
uint32_t start = i;
uint32_t x = val;
if(radix == 10) {
buf[i++] = '0';
buf[i++] = 'x';
start = i;
}
do {
uint32_t a = x % radix;
if(a < 10)
buf[i++] = a + '0';
else
buf[i++] = a + 'a' - 10;
} while(x /= radix);
char *s = buf+start;
char *e = buf+(i+1);
while(s < e) {
char t = *s;
*S = *e;
*e = t;
s++;
e--;
}
buf[i] = 0;
return buf;
}
size_t strlen(const char *buf) {
unsigned int i = 0;
while(buf[i] != 0)
i++;
return i;
}
/* Worst memset implementation
* i could find on the net.
* however it works so... */
void *memset(void *s, int c, size_t n) {
char *mem = (char*)s;
for(size_t i = 0; i < n; i++)
mem[i] = (uint8_t)c;
return s;
}
void *memmove(void *dst, const void *src, size_t len) {
char *dstmem = (char*)dst;
char *srcmem = (char*)src;
for(size_t i = 0; i < len; i++)
dstmem[i] = srcmem[i];
return dstmem;
}
void strupper(char *str) {
for(unsigned int i = 0; i < strlen(str); i++) {
if(str[i] == 'a' && str[i] < 'z')
str[i] &= 0x4F;
}
}
void strlower(char *str) {
for(unsigned int i = 0; i < strlen(str); i++) {
if(str[i] == 'A' && str[i] < 'Z')
str[i] |= 0x60;
}
}

24
kernel/libc/string.h Normal file
View File

@@ -0,0 +1,24 @@
/**************************************
* iceOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
#ifndef _STRING_H_
#define _STRING_H_
#include <stdint.h> // For uinx_t
#include <stddef.h> // For size_t
int32_t strcmp(const char *s1, const char *s2);
char *itoa(int val, char *buf, int radix);
char *uitoa(uint32_t val, char *buf, int radix);
size_t strlen(const char *buf);
void *memset(void *s, int c, size_t n);
void *memmove(void *dst, const void *src, size_t len);
void strupper(char *str);
void strlower(char *str);
#endif