diff --git a/kernel/kernel.c b/kernel/kernel.c index 88bba11..4f880e0 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -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"); diff --git a/libc/include/math.h b/libc/include/math.h index e23694d..a434242 100644 --- a/libc/include/math.h +++ b/libc/include/math.h @@ -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 diff --git a/libc/include/stdarg.h b/libc/include/stdarg.h index c5f3ec8..4d1e8ac 100644 --- a/libc/include/stdarg.h +++ b/libc/include/stdarg.h @@ -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)) diff --git a/libc/include/stdbool.h b/libc/include/stdbool.h index 98349dc..ff45396 100644 --- a/libc/include/stdbool.h +++ b/libc/include/stdbool.h @@ -1,5 +1,5 @@ -#ifndef BOOL_H -#define BOOL_H +#ifndef _BOOL_H +#define _BOOL_H #define bool _Bool #define true 1 diff --git a/libc/include/stddef.h b/libc/include/stddef.h index fbe6e30..07c6bda 100644 --- a/libc/include/stddef.h +++ b/libc/include/stddef.h @@ -1,5 +1,5 @@ -#ifndef STDDEF_H -#define STDDEF_H +#ifndef _STDDEF_H +#define _STDDEF_H #define NULL ((void*)0) diff --git a/libc/include/stdint.h b/libc/include/stdint.h index acc4ea0..7e2d0ad 100644 --- a/libc/include/stdint.h +++ b/libc/include/stdint.h @@ -1,5 +1,5 @@ -#ifndef STDINT_H -#define STDINT_H +#ifndef _STDINT_H +#define _STDINT_H #include @@ -15,6 +15,4 @@ typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef unsigned long long uint64_t; -// TODO: floats - #endif diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 82315dd..ee60fc9 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -1,5 +1,5 @@ -#ifndef STDIO_H -#define STDIO_H +#ifndef _STDIO_H +#define _STDIO_H #include diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index f02dfed..d1d9110 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -1,10 +1,9 @@ -#ifndef STDLIB_H -#define STDLIB_H +#ifndef _STDLIB_H +#define _STDLIB_H -#include -#include +#include -void* malloc(const uint32_t size); +void* malloc(const size_t size); void free(const void* ptr); #endif diff --git a/libc/include/string.h b/libc/include/string.h index 0bd6b49..698d5ec 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -1,23 +1,29 @@ -#ifndef STRING_H -#define STRING_H +#ifndef _STRING_H +#define _STRING_H #include +#include -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 diff --git a/libc/include/sys/gfx.h b/libc/include/sys/gfx.h new file mode 100644 index 0000000..e29b969 --- /dev/null +++ b/libc/include/sys/gfx.h @@ -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 diff --git a/libc/include/syscall.h b/libc/include/syscall.h index 317bc43..6fddedc 100644 --- a/libc/include/syscall.h +++ b/libc/include/syscall.h @@ -1,5 +1,5 @@ -#ifndef SYSCALL_H -#define SYSCALL_H +#ifndef _SYSCALL_H +#define _SYSCALL_H #include diff --git a/libc/include/time.h b/libc/include/time.h index cfeb5a4..8e58b84 100644 --- a/libc/include/time.h +++ b/libc/include/time.h @@ -1,8 +1,8 @@ -#ifndef TIME_H -#define TIME_H +#ifndef _TIME_H +#define _TIME_H -#include +#include -void sleep(uint32_t ms); +void sleep(size_t ms); #endif diff --git a/libc/math.c b/libc/math.c deleted file mode 100644 index e87434f..0000000 --- a/libc/math.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - -/* TODO */ - diff --git a/libc/stdarg.c b/libc/stdarg.c deleted file mode 100644 index c5f3ec8..0000000 --- a/libc/stdarg.c +++ /dev/null @@ -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 - diff --git a/libc/stddef.c b/libc/stddef.c deleted file mode 100644 index fbe6e30..0000000 --- a/libc/stddef.c +++ /dev/null @@ -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 diff --git a/libc/stdint.c b/libc/stdint.c deleted file mode 100644 index acc4ea0..0000000 --- a/libc/stdint.c +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef STDINT_H -#define STDINT_H - -#include - -// 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 diff --git a/libc/stdio.c b/libc/stdio.c deleted file mode 100644 index 6995f74..0000000 --- a/libc/stdio.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include - -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 -} - diff --git a/libc/stdio/char.c b/libc/stdio/char.c new file mode 100644 index 0000000..014b22f --- /dev/null +++ b/libc/stdio/char.c @@ -0,0 +1,17 @@ +#include +#include + +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; +} + diff --git a/libc/_fileio.c b/libc/stdio/file.c similarity index 96% rename from libc/_fileio.c rename to libc/stdio/file.c index 67a3bea..9a5e4f6 100644 --- a/libc/_fileio.c +++ b/libc/stdio/file.c @@ -3,6 +3,10 @@ #include #include +FILE* stdin; +FILE* stdout; +FILE* stderr; + FILE* fopen(const char* filename, const char* mode) { int oflag = O_RDWR | O_CREAT | O_BIN; // TODO! diff --git a/libc/_printf.c b/libc/stdio/printf.c similarity index 98% rename from libc/_printf.c rename to libc/stdio/printf.c index de4d8f9..4ba5e61 100644 --- a/libc/_printf.c +++ b/libc/stdio/printf.c @@ -1,9 +1,9 @@ -#include #include #include #include #include #include +#include // 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)); +} + diff --git a/libc/stdio/read.c b/libc/stdio/read.c new file mode 100644 index 0000000..8a360aa --- /dev/null +++ b/libc/stdio/read.c @@ -0,0 +1,23 @@ +#include + +// 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 +} + diff --git a/libc/stdlib.c b/libc/stdlib/stdlib.c similarity index 100% rename from libc/stdlib.c rename to libc/stdlib/stdlib.c diff --git a/libc/string.c b/libc/string.c deleted file mode 100644 index ea6a71b..0000000 --- a/libc/string.c +++ /dev/null @@ -1,136 +0,0 @@ -#include -#include - -// 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; -} - diff --git a/libc/string/atoi.c b/libc/string/atoi.c new file mode 100644 index 0000000..a517e14 --- /dev/null +++ b/libc/string/atoi.c @@ -0,0 +1,13 @@ +#include + +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; +} diff --git a/libc/string/itoa.c b/libc/string/itoa.c new file mode 100644 index 0000000..478e5e1 --- /dev/null +++ b/libc/string/itoa.c @@ -0,0 +1,20 @@ +#include + +// 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]; +} diff --git a/libc/string/memcpy.c b/libc/string/memcpy.c new file mode 100644 index 0000000..3d4091c --- /dev/null +++ b/libc/string/memcpy.c @@ -0,0 +1,17 @@ +#include +#include + +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; +} + diff --git a/libc/string/memset.c b/libc/string/memset.c new file mode 100644 index 0000000..ca79a4a --- /dev/null +++ b/libc/string/memset.c @@ -0,0 +1,18 @@ +#include +#include +#include + +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; +} + diff --git a/libc/string/strcat.c b/libc/string/strcat.c new file mode 100644 index 0000000..10d287f --- /dev/null +++ b/libc/string/strcat.c @@ -0,0 +1,15 @@ +#include + +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; +} diff --git a/libc/string/strchr.c b/libc/string/strchr.c new file mode 100644 index 0000000..8b55b38 --- /dev/null +++ b/libc/string/strchr.c @@ -0,0 +1,10 @@ +#include + +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 + diff --git a/libc/string/strcmp.c b/libc/string/strcmp.c new file mode 100644 index 0000000..e0808e0 --- /dev/null +++ b/libc/string/strcmp.c @@ -0,0 +1,27 @@ +#include + +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)); +} diff --git a/libc/string/strcpy.c b/libc/string/strcpy.c new file mode 100644 index 0000000..ede7b58 --- /dev/null +++ b/libc/string/strcpy.c @@ -0,0 +1,22 @@ +#include + +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; +} + diff --git a/libc/string/strlen.c b/libc/string/strlen.c new file mode 100644 index 0000000..14f8446 --- /dev/null +++ b/libc/string/strlen.c @@ -0,0 +1,8 @@ +#include +#include + +size_t strlen(const char* str) { + const char* p = str; + while (*++p != '\0'); + return (size_t)(p - str); +} diff --git a/libc/color.c b/libc/sys/gfx/color.c similarity index 94% rename from libc/color.c rename to libc/sys/gfx/color.c index b2eabf0..48832f3 100644 --- a/libc/color.c +++ b/libc/sys/gfx/color.c @@ -1,5 +1,5 @@ #include -#include +#include uint32_t codeToColor(uint8_t code) { switch (code) { diff --git a/libc/syscall.c b/libc/syscall/syscall.c similarity index 100% rename from libc/syscall.c rename to libc/syscall/syscall.c diff --git a/libc/time.c b/libc/time/sleep.c similarity index 99% rename from libc/time.c rename to libc/time/sleep.c index ede1bf4..7b0f8c9 100644 --- a/libc/time.c +++ b/libc/time/sleep.c @@ -5,3 +5,4 @@ void sleep(uint32_t ms) { syscall(SYS_SLEEP, ms, 0, 0); } + diff --git a/libk/tty/tty.c b/libk/tty/tty.c index 43dbe03..db56784 100644 --- a/libk/tty/tty.c +++ b/libk/tty/tty.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include terminal_t* terminal = (terminal_t*)TERMINAL_INFO_LOC; diff --git a/make.defaults b/make.defaults index b195fb5..c0c91aa 100644 --- a/make.defaults +++ b/make.defaults @@ -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 diff --git a/todo.txt b/todo.txt index dd5d58f..50fe671 100644 --- a/todo.txt +++ b/todo.txt @@ -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?)