Changed libc structure, shouldn't've added anything new

This commit is contained in:
2024-10-19 15:11:00 +02:00
parent e2e24d3853
commit 64b4e47d73
38 changed files with 264 additions and 276 deletions
+4 -4
View File
@@ -92,7 +92,7 @@ void main() {
terminal->fg = FG_COLOR;
terminal->bg = BG_COLOR;
clear();
printf("\x1b[J"); // Clear
printf("kern.\n\n");
// Run Interactive Shell Program
@@ -123,7 +123,7 @@ void main() {
}
if (strcmp(argv[0], "clear") == 0) {
clear();
printf("\x1b[J");
} else if (strcmp(argv[0], "echo") == 0) {
if (argc < 2) continue;
@@ -151,7 +151,7 @@ void main() {
} else if (strcmp(argv[0], "exit") == 0) {
break;
} else if (strcmp(argv[0], "gfx") == 0) {
clear();
printf("\x1b[J");
// Draw some stuff :)
for (size_t i = 0; i < 120; i++) {
@@ -182,7 +182,7 @@ void main() {
fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9));
getc(); // Wait until we get a keystroke
clear();
printf("\x1b[J");
} else if (strcmp(argv[0], "help") == 0) {
printf("Available Commands:\n");
printf(" clear | Clears the screen\n");
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef MATH_H
#define MATH_H
#ifndef _MATH_H
#define _MATH_H
// Constants
#define PI 3.141592653589 // Should be good enough
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef STDARG_H
#define STDARG_H
#ifndef _STDARG_H
#define _STDARG_H
typedef char* va_list;
#define va_start(ap, paramn) (void)((ap) = (char*)(&(paramn) + 1))
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef BOOL_H
#define BOOL_H
#ifndef _BOOL_H
#define _BOOL_H
#define bool _Bool
#define true 1
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef STDDEF_H
#define STDDEF_H
#ifndef _STDDEF_H
#define _STDDEF_H
#define NULL ((void*)0)
+2 -4
View File
@@ -1,5 +1,5 @@
#ifndef STDINT_H
#define STDINT_H
#ifndef _STDINT_H
#define _STDINT_H
#include <stddef.h>
@@ -15,6 +15,4 @@ typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
// TODO: floats
#endif
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef STDIO_H
#define STDIO_H
#ifndef _STDIO_H
#define _STDIO_H
#include <stddef.h>
+4 -5
View File
@@ -1,10 +1,9 @@
#ifndef STDLIB_H
#define STDLIB_H
#ifndef _STDLIB_H
#define _STDLIB_H
#include <stdint.h>
#include <syscall.h>
#include <stddef.h>
void* malloc(const uint32_t size);
void* malloc(const size_t size);
void free(const void* ptr);
#endif
+14 -8
View File
@@ -1,23 +1,29 @@
#ifndef STRING_H
#define STRING_H
#ifndef _STRING_H
#define _STRING_H
#include <stdint.h>
#include <stddef.h>
void* memcpy(void* src, void* dst, uint32_t length);
void* memcpy32(void* src, void* dst, uint32_t length);
void* memset(void* dst, uint8_t value, uint32_t length);
void* memcpy(void* src, void* dst, size_t length);
void* memcpy32(void* src, void* dst, size_t length);
void* memset(void* dst, uint8_t value, size_t length);
void* memset32(void* dst, uint32_t value, size_t length);
char* itoa(uint32_t val, uint8_t base);
char* itoa(size_t val, int base);
uint32_t atoi(const char* str);
int isSpace(char c);
char* strcpy(char* str1, const char* str2);
char* strncpy(char* str1, const char* str2, size_t n);
char* strcat(char* str1, const char* str2);
char* strncat(char* str1, const char* str2, size_t n);
char* strchr(const char* str, char c);
char* strrchr(const char* str, char c);
int strcmp(const char* str1, const char* str2);
int strncmp(const char* str1, const char* str2, size_t n);
uint32_t strlen(const char* str);
size_t strlen(const char* str);
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef _SYS_GFX_H
#define _SYS_GFX_H
#define BLACK 0x00000000 // 0
#define RED 0x00ff0000 // 1
#define GREEN 0x0000ff00 // 2
#define YELLOW 0x00ffff00 // 3
#define BLUE 0x000000ff // 4
#define MAGENTA 0x00ff00ff // 5
#define CYAN 0x0000ffff // 6
#define WHITE 0x00ffffff // 7
uint32_t codeToColor(uint8_t code);
#endif
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef SYSCALL_H
#define SYSCALL_H
#ifndef _SYSCALL_H
#define _SYSCALL_H
#include <stddef.h>
+4 -4
View File
@@ -1,8 +1,8 @@
#ifndef TIME_H
#define TIME_H
#ifndef _TIME_H
#define _TIME_H
#include <stdint.h>
#include <stddef.h>
void sleep(uint32_t ms);
void sleep(size_t ms);
#endif
-4
View File
@@ -1,4 +0,0 @@
#include <math.h>
/* TODO */
-10
View File
@@ -1,10 +0,0 @@
#ifndef STDARG_H
#define STDARG_H
typedef char* va_list;
#define va_start(ap, paramn) (void)((ap) = (char*)(&(paramn) + 1))
#define va_end(ap) (void)((ap) = 0)
#define va_arg(ap, type) (((type*)((ap) = ((ap) + sizeof(type))))[-1])
#endif
-9
View File
@@ -1,9 +0,0 @@
#ifndef STDDEF_H
#define STDDEF_H
#define NULL ((void*)0)
typedef unsigned int size_t;
typedef signed int ssize_t;
#endif
-20
View File
@@ -1,20 +0,0 @@
#ifndef STDINT_H
#define STDINT_H
#include <stddef.h>
// Signed ints
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int64_t;
// Unsigned ints
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
// TODO: floats
#endif
-53
View File
@@ -1,53 +0,0 @@
#include <stdio.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
FILE* stdin;
FILE* stdout;
FILE* stderr;
void putc(char c) {
// write(stdout, &c, 1); TODO: This doesn't work and we need to use a static variable
static char _c;
_c = c;
write(1, &_c, 1);
}
char getc() {
char* c = (char*)0x2100;
*c= '\0';
while (*c == '\0') { __asm__ volatile ("hlt"); }
return *c;
}
void print(const char* s) {
write(1, s, strlen(s));
}
void clear() {
print("\x1b[J");
}
// Reads until a newline
// TODO: Use Read() Syscall
void reads(char* buffer) {
char c;
int len = 0;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (len < 1) continue; // Can we backspace?
len--;
putc(c);
} else {
buffer[len++] = c;
putc(c);
}
}
putc(c); // Print the newline
buffer[len] = '\0'; // Null terminate
}
+17
View File
@@ -0,0 +1,17 @@
#include <syscall.h>
#include <stdio.h>
void putc(char c) {
// write(stdout, &c, 1); TODO: This doesn't work and we need to use a static variable
static char _c;
_c = c;
write(1, &_c, 1);
}
char getc() {
char* c = (char*)0x2100; // TODO: Don't use a random looking memory address -_('-')_-
*c= '\0';
while (*c == '\0') { __asm__ volatile ("hlt"); }
return *c;
}
+4
View File
@@ -3,6 +3,10 @@
#include <stdlib.h>
#include <stddef.h>
FILE* stdin;
FILE* stdout;
FILE* stderr;
FILE* fopen(const char* filename, const char* mode) {
int oflag = O_RDWR | O_CREAT | O_BIN; // TODO!
+5 -1
View File
@@ -1,9 +1,9 @@
#include <stdio.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
// Warning: returns an allocated buffer, must be freed!
char* _format(const char* fmt, va_list args) {
@@ -123,3 +123,7 @@ int printf(const char* fmt, ...) {
return len;
}
void print(const char* s) {
write(1, s, strlen(s));
}
+23
View File
@@ -0,0 +1,23 @@
#include <stdio.h>
// Reads until a newline
// TODO: Use Read() Syscall
void reads(char* buffer) {
char c;
int len = 0;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (len < 1) continue; // Can we backspace?
len--;
putc(c);
} else {
buffer[len++] = c;
putc(c);
}
}
putc(c); // Print the newline
buffer[len] = '\0'; // Null terminate
}
-136
View File
@@ -1,136 +0,0 @@
#include <string.h>
#include <stdint.h>
// Source: http://www.strudel.org.uk/itoa/
char* itoa(uint32_t val, uint8_t base) {
static char buf[32] = { 0 };
if (base == 0 || base > 16) { return NULL; }
if (val == 0) {
buf[0] = '0';
return buf;
}
int i;
for (i = 30; val && i; i--, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
return &buf[i + 1];
}
uint32_t atoi(const char* str) {
uint32_t res = 0;
// TODO: Add error checking
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str++ - '0');
}
return res;
}
int isSpace(char c) {
if (c == ' ' || c == '\t' || c == '\n') return 1;
else return 0;
}
// Some implementations taken from https://en.wikibooks.org/wiki/C_Programming/String_manipulation
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
char* strncpy(char* str1, const char* str2, size_t n) {
char* dst = str1;
const char* src = str2;
while (n > 0) {
n--;
if ((*dst++ = *src++) == '\0') {
memset(dst, '\0', n);
break;
}
}
return str1;
}
char* strcat(char* str1, const char* str2) {
char* s = str1;
while (*s != '\0') { s++; }
strcpy(s, str2);
return str1;
}
char* strncat(char* str1, const char* str2, size_t n) {
char* s = str1;
while (*s != '\0') { s++; }
strncpy(s, str2, n);
return str1;
}
char* strchr(const char* str, char c) {
const char* s = str;
while (*s != '\0' && *s != c) { s++; }
return (*s == c) ? (char*)s : NULL;
}
char* strrchr(const char* str, char c) { return NULL; } // TODO
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
int strncmp(const char* str1, const char* str2, size_t n) {
if (n == 0) { return 0; }
while (n-- > 0 && *str1 == *str2) {
if (n == 0 || *str1 == '\0') { return 0; }
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
uint32_t strlen(const char* str) {
const char* p = str;
while (*++p != '\0');
return (uint32_t)(p - str);
}
// Memory functions
void* memcpy(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*((uint8_t*)dst + i) = *((uint8_t*)src + i);
}
return dst;
}
void* memcpy32(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i * 4 < length; i++) {
*((uint32_t*)dst + i) = *((uint32_t*)src + i);
}
return dst;
}
void* memset(void* dst, uint8_t value, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*(uint8_t*)(dst + i) = value;
}
return dst;
}
+13
View File
@@ -0,0 +1,13 @@
#include <string.h>
unsigned int atoi(const char* str) {
unsigned int res = 0;
// TODO: Add error checking
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str++ - '0');
}
return res;
}
+20
View File
@@ -0,0 +1,20 @@
#include <string.h>
// Source: http://www.strudel.org.uk/itoa/
char* itoa(size_t val, int base) {
static char buf[32] = { 0 };
if (base == 0 || base > 16) { return NULL; }
if (val == 0) {
buf[0] = '0';
return buf;
}
int i;
for (i = 30; val && i; i--, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
return &buf[i + 1];
}
+17
View File
@@ -0,0 +1,17 @@
#include <string.h>
#include <stdint.h>
void* memcpy(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*((uint8_t*)dst + i) = *((uint8_t*)src + i);
}
return dst;
}
void* memcpy32(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i * 4 < length; i++) {
*((uint32_t*)dst + i) = *((uint32_t*)src + i);
}
return dst;
}
+18
View File
@@ -0,0 +1,18 @@
#include <string.h>
#include <stdint.h>
#include <stddef.h>
void* memset(void* dst, uint8_t value, size_t length) {
for (size_t i = 0; i < length; i++) {
*(uint8_t*)(dst + i) = value;
}
return dst;
}
void* memset32(void* dst, uint32_t value, size_t length) {
for (size_t i = 0; i < length; i++) {
*(uint32_t*)(dst + i) = value;
}
return dst;
}
+15
View File
@@ -0,0 +1,15 @@
#include <string.h>
char* strcat(char* str1, const char* str2) {
char* s = str1;
while (*s != '\0') { s++; }
strcpy(s, str2);
return str1;
}
char* strncat(char* str1, const char* str2, size_t n) {
char* s = str1;
while (*s != '\0') { s++; }
strncpy(s, str2, n);
return str1;
}
+10
View File
@@ -0,0 +1,10 @@
#include <string.h>
char* strchr(const char* str, char c) {
const char* s = str;
while (*s != '\0' && *s != c) { s++; }
return (*s == c) ? (char*)s : NULL;
}
char* strrchr(const char* str, char c) { return NULL; } // TODO
+27
View File
@@ -0,0 +1,27 @@
#include <string.h>
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
int strncmp(const char* str1, const char* str2, size_t n) {
if (n == 0) { return 0; }
while (n-- > 0 && *str1 == *str2) {
if (n == 0 || *str1 == '\0') { return 0; }
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
+22
View File
@@ -0,0 +1,22 @@
#include <string.h>
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
char* strncpy(char* str1, const char* str2, size_t n) {
char* dst = str1;
const char* src = str2;
while (n > 0) {
n--;
if ((*dst++ = *src++) == '\0') {
memset(dst, '\0', n);
break;
}
}
return str1;
}
+8
View File
@@ -0,0 +1,8 @@
#include <string.h>
#include <stddef.h>
size_t strlen(const char* str) {
const char* p = str;
while (*++p != '\0');
return (size_t)(p - str);
}
+1 -1
View File
@@ -1,5 +1,5 @@
#include <stdint.h>
#include <color.h>
#include <sys/gfx.h>
uint32_t codeToColor(uint8_t code) {
switch (code) {
+1
View File
@@ -5,3 +5,4 @@
void sleep(uint32_t ms) {
syscall(SYS_SLEEP, ms, 0, 0);
}
+1 -1
View File
@@ -5,7 +5,7 @@
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <color.h>
#include <sys/gfx.h>
terminal_t* terminal = (terminal_t*)TERMINAL_INFO_LOC;
+2 -2
View File
@@ -10,8 +10,8 @@ AS = nasm
MAKE = make
# Flags
CFLAGS = -Wall -Wextra -Wpedantic -Wno-pointer-sign -Wno-pointer-arith\
-Os -std=c17 -nostdinc -g\
CFLAGS = -Wall -Wextra -Wno-pointer-sign -Wno-pointer-arith -Wno-unused-parameter\
-Os -std=c17 -nostdinc -g -Werror\
-ffreestanding -fno-stack-protector -fno-builtin -fno-pie\
-m32 -mgeneral-regs-only -march=i386
LDFLAGS = -z notext -melf_i386 -nostdlib
+5 -2
View File
@@ -25,6 +25,7 @@ Legend:
- [x] makefile
- [?] probably a full rewrite
- [ ] filesystem
- [ ] use Ext1
- [ ] sysroot
- [ ] good buildutil
- [ ] proper file organization
@@ -32,8 +33,9 @@ Legend:
- [ ] LIBC
- [ ] assert.h
- [ ] ctype.h
- [ ] errno.h
- [ ] complex.h (idk why we would need this)
- [x] ctype.h
- [x] errno.h
- [ ] fcntl.h
- [ ] float.h
- [ ] limits.h
@@ -45,6 +47,7 @@ Legend:
- [ ] stddef.h
- [ ] stdint.h
- [ ] stdio.h
- [ ] syscall.h
- [ ] time.h
- [ ] unistd.h (do we do POSIX?)
- [ ] wchar.h (we would need text support for wide chars ig?)