Update libc implementation

This commit is contained in:
Alex
2023-04-23 21:38:36 +03:00
parent 6deb37fbfe
commit 43aad488a7
18 changed files with 478 additions and 33 deletions

View File

@@ -19,6 +19,7 @@ ASM_SOURCES = $(shell find ./ -type f -name '*.asm')
OBJ = ${C_SOURCES:.c=.o} ${CPP_SOURCES:.cpp=.o} ${ASM_SOURCES:.asm=.o} ${S_SOURCES:.S=.o}
INCLUDE = ../include
INCLUDE2 = ../../out/system/include
ifeq ($(OSARCH), amd64)
ASM_ARCH := elf64
@@ -26,7 +27,7 @@ else ifeq ($(OSARCH), i386)
ASM_ARCH := elf32
endif
CFLAGS := -fPIC -I$(INCLUDE)
CFLAGS := -fPIC -I$(INCLUDE) -I$(INCLUDE2)
build: $(OBJECT_NAME)

27
libc/src/std/ctype.c Normal file
View File

@@ -0,0 +1,27 @@
#include <ctype.h>
int tolower(int c)
{
if (c >= 'A' && c <= 'Z')
{
c -= 'A';
c += 'a';
}
return c;
}
int toupper(int c)
{
if (c >= 'a' && c <= 'z')
{
c -= 'a';
c += 'A';
}
return c;
}
int isspace(int c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v';
}

6
libc/src/std/errno.c Normal file
View File

@@ -0,0 +1,6 @@
#include <errno.h>
int *__errno_location(void)
{
return 0;
}

View File

@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdarg.h>
#include <sys/syscalls.h>
FILE *stdin;
FILE *stdout;
@@ -54,17 +55,47 @@ void setbuf(FILE *stream, char *buf)
{
}
// int vfprintf(FILE *stream, const char *format, va_list arg)
// {
// return 0;
// }
int puts(const char *s)
int vfprintf(FILE *stream, const char *format, va_list arg)
{
return 0;
}
int vsscanf(const char *s, const char *format, va_list arg)
{
}
int sscanf(const char *s, const char *format, ...)
{
va_list args;
va_start(args, format);
const int ret = vsscanf(s, format, args);
va_end(args);
return ret;
}
int fputc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int putc(int c, FILE *stream)
{
syscall1(_Print, c);
}
int fputs(const char *s, FILE *stream)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stream);
}
int putchar(int c)
{
return 0;
syscall1(_Print, c);
}
int puts(const char *s)
{
for (int i = 0; s[i] != '\0'; i++)
fputc(s[i], stdout);
}

View File

@@ -23,8 +23,7 @@ void exit(int status)
int atoi(const char *nptr)
{
// uint64_t Length = strlen((char *)nptr);
uint64_t Length = 0;
uint64_t Length = strlen((char *)nptr);
if (nptr)
while (nptr[Length] != '\0')
++Length;
@@ -62,3 +61,25 @@ void free(void *Address)
PREFIX(free)
(Address);
}
int system(const char *command)
{
return 0;
}
double atof(const char *nptr)
{
// FIXME: This is a very bad implementation of atof.
uint64_t Length = strlen((char *)nptr);
if (nptr)
while (nptr[Length] != '\0')
++Length;
double OutBuffer = 0;
double Power = 1;
for (uint64_t i = Length; i > 0; --i)
{
OutBuffer += (nptr[i - 1] - 48) * Power;
Power *= 10;
}
return OutBuffer;
}

142
libc/src/std/string.c Normal file
View File

@@ -0,0 +1,142 @@
#include <string.h>
#include "../mem/liballoc_1_1.h"
size_t strlen(const char *str)
{
long unsigned i = 0;
if (str)
while (str[i] != '\0')
++i;
return i;
}
int strcmp(const char *l, const char *r)
{
for (; *l == *r && *l; l++, r++)
;
return *(unsigned char *)l - *(unsigned char *)r;
}
int strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++)
{
char c1 = s1[i], c2 = s2[i];
if (c1 != c2)
return c1 - c2;
if (!c1)
return 0;
}
return 0;
}
int strcasecmp(const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
int result;
if (p1 == p2)
return 0;
while ((result = tolower(*p1) - tolower(*p2++)) == 0)
if (*p1++ == '\0')
break;
return result;
}
int strncasecmp(const char *string1, const char *string2, size_t count)
{
if (count)
{
const unsigned char *s1 = (const unsigned char *)string1;
const unsigned char *s2 = (const unsigned char *)string2;
int result;
do
{
if ((result = tolower(*s1) - tolower(*s2++)) != 0 || !*s1++)
break;
} while (--count);
return result;
}
return 0;
}
char *strstr(const char *haystack, const char *needle)
{
const char *a = haystack, *b = needle;
while (1)
{
if (!*b)
return (char *)haystack;
if (!*a)
return NULL;
if (*a++ != *b++)
{
a = ++haystack;
b = needle;
}
}
}
char *strncpy(char *destination, const char *source, unsigned long num)
{
if (destination == NULL)
return NULL;
char *ptr = destination;
while (*source && num--)
{
*destination = *source;
destination++;
source++;
}
*destination = '\0';
return ptr;
}
char *strdup(const char *s)
{
char *buf = (char *)__malloc(strlen((char *)s) + 1);
strncpy(buf, s, strlen(s) + 1);
return buf;
}
char *strchr(char const *s, int c)
{
size_t len = strlen(s);
for (size_t i = 0; i < len; i++)
if (s[i] == c)
return (char *)s + i;
return NULL;
}
char *strrchr(char const *s, int c)
{
size_t len = strlen(s);
size_t pos = len;
while (s[pos] != c && pos-- != 0)
;
if (pos == len)
return NULL;
return (char *)s + pos;
}
void *memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;
if (d < s)
while (n--)
*d++ = *s++;
else
{
const char *lasts = s + (n - 1);
char *lastd = d + (n - 1);
while (n--)
*lastd-- = *lasts--;
}
return dest;
}

16
libc/src/std/sys_stat.c Normal file
View File

@@ -0,0 +1,16 @@
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode)
{
return 0;
}
int remove(const char *pathname)
{
return 0;
}
int rename(const char *oldpath, const char *newpath)
{
return 0;
}

View File

@@ -1,14 +0,0 @@
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++)
{
char c1 = s1[i], c2 = s2[i];
if (c1 != c2)
return c1 - c2;
if (!c1)
return 0;
}
return 0;
}