This commit is contained in:
nub31
2025-09-03 13:13:16 +02:00
parent c338e05648
commit 87d1a291f7
26 changed files with 353 additions and 287 deletions

10
src/stdlib/mem.c Normal file
View File

@@ -0,0 +1,10 @@
#include "mem.h"
#include <stdint.h>
void memset(void* destination, uint8_t value, size_t length)
{
for (size_t i = 0; i < length; i++)
{
((uint8_t*)destination)[i] = value;
}
}

6
src/stdlib/mem.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
void memset(void* destination, uint8_t value, size_t length);

84
src/stdlib/stdio.c Normal file
View File

@@ -0,0 +1,84 @@
#include "stdio.h"
#include "../arch/arch.h"
#include "string.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
void printf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
bool should_format = false;
for (size_t i = 0; fmt[i] != '\0'; i++)
{
if (should_format)
{
should_format = false;
if (fmt[i] == '%')
{
put_char('%');
}
else if (fmt[i] == 's')
{
const char* str = va_arg(args, const char*);
for (size_t j = 0; str[j] != '\0'; j++)
{
put_char(str[j]);
}
}
else if (fmt[i] == 'c')
{
char character = (char)va_arg(args, int64_t);
put_char(character);
}
else if (fmt[i] == 'd')
{
int64_t val = va_arg(args, int64_t);
char buf[21];
itoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else if (fmt[i] == 'u')
{
uint64_t val = va_arg(args, uint64_t);
char buf[21];
uitoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else if (fmt[i] == 'x')
{
uint64_t val = va_arg(args, uint64_t);
char buf[17];
uitoa64_hex(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else
{
put_char(fmt[i]);
}
}
else if (fmt[i] == '%')
{
should_format = true;
}
else
{
put_char(fmt[i]);
}
}
va_end(args);
}

3
src/stdlib/stdio.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void printf(const char* fmt, ...);

127
src/stdlib/string.c Normal file
View File

@@ -0,0 +1,127 @@
#include "string.h"
int strcmp(const char* a, const char* b)
{
while ((*a != '\0' && *b != '\0') && *a == *b)
{
a++;
b++;
}
return (*a == *b) ? 0 : (*a > *b) ? 1 : -1;
}
void reverse(char* str, size_t length)
{
int start = 0;
int end = length - 1;
while (start < end)
{
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
void itoa64(int64_t value, char* buffer)
{
char temp[21];
int i = 0, j = 0;
int negative = 0;
if (value < 0)
{
negative = 1;
if (value == INT64_MIN)
{
const char* min_str = "9223372036854775808";
for (i = 0; min_str[i] != '\0'; i++)
temp[i] = min_str[i];
i = 19;
}
else
{
value = -value;
}
}
if (value == 0 && !negative)
{
buffer[0] = '0';
buffer[1] = '\0';
return;
}
if (!(negative && value == INT64_MIN))
{
while (value > 0)
{
temp[i++] = '0' + (value % 10);
value /= 10;
}
}
if (negative)
{
temp[i++] = '-';
}
while (i > 0)
{
buffer[j++] = temp[--i];
}
buffer[j] = '\0';
}
void uitoa64(uint64_t value, char* buffer)
{
char temp[21];
int i = 0, j = 0;
if (value == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
return;
}
while (value > 0)
{
temp[i++] = '0' + (value % 10);
value /= 10;
}
while (i > 0)
{
buffer[j++] = temp[--i];
}
buffer[j] = '\0';
}
void uitoa64_hex(uint64_t value, char* buffer)
{
const char* digits = "0123456789abcdef";
char temp[17];
int pos = 0;
if (value == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
return;
}
while (value > 0 && pos < 16)
{
temp[pos++] = digits[value & 0xF];
value >>= 4;
}
for (int i = 0; i < pos; i++)
{
buffer[i] = temp[pos - i - 1];
}
buffer[pos] = '\0';
}

10
src/stdlib/string.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "stddef.h"
#include "stdint.h"
int strcmp(const char* a, const char* b);
void reverse(char* str, size_t length);
void uitoa64(uint64_t value, char* buffer);
void itoa64(int64_t value, char* buffer);
void uitoa64_hex(uint64_t value, char* buffer);