Espresso 0.0.1a
This commit is contained in:
@ -1,190 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <fs/ramfs.h>
|
||||
|
||||
|
||||
static ramfs_file_t ramfs_files[RAMFS_MAX_FILES];
|
||||
static ramfs_directory_t root_directory = { .name = "/", .file_count = 0 };
|
||||
|
||||
void ramfs_init()
|
||||
{
|
||||
memset(ramfs_files, 0, sizeof(ramfs_files));
|
||||
root_directory.file_count = 0;
|
||||
}
|
||||
|
||||
ramfs_file_t* ramfs_create_file(const char* name, uint32_t size, uint8_t permissions)
|
||||
{
|
||||
for (uint32_t i = 0; i < RAMFS_MAX_FILES; i++) {
|
||||
if (ramfs_files[i].name[0] == '\0') {
|
||||
// Create the file
|
||||
ramfs_file_t* file = &ramfs_files[i];
|
||||
strncpy(file->name, name, RAMFS_MAX_PATH_LEN);
|
||||
file->size = size;
|
||||
file->data = (size > 0) ? malloc(size) : NULL;
|
||||
file->permissions = permissions;
|
||||
|
||||
if (root_directory.file_count < RAMFS_MAX_FILES) {
|
||||
root_directory.files[root_directory.file_count++] = file;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool ramfs_delete_file(const char* name)
|
||||
{
|
||||
for (uint32_t i = 0; i < root_directory.file_count; i++) {
|
||||
if (strcmp(root_directory.files[i]->name, name) == 0) {
|
||||
free(root_directory.files[i]->data);
|
||||
root_directory.files[i] = root_directory.files[root_directory.file_count - 1];
|
||||
root_directory.file_count--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ramfs_file_t* ramfs_find_file(const char* name)
|
||||
{
|
||||
for (uint32_t i = 0; i < root_directory.file_count; i++) {
|
||||
if (strcmp(root_directory.files[i]->name, name) == 0) {
|
||||
return root_directory.files[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool ramfs_resize_file(const char* name, uint32_t new_size)
|
||||
{
|
||||
ramfs_file_t* file = ramfs_find_file(name);
|
||||
if (file)
|
||||
{
|
||||
file->data = realloc(file->data, new_size);
|
||||
file->size = new_size;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ramfs_append_to_file(const char* name, const uint8_t* data, uint32_t data_size)
|
||||
{
|
||||
ramfs_file_t* file = ramfs_find_file(name);
|
||||
if (file && (file->permissions & RAMFS_PERM_WRITE))
|
||||
{
|
||||
file->data = realloc(file->data, file->size + data_size + 1);
|
||||
if (!file->data) return false;
|
||||
|
||||
memcpy(file->data + file->size, data, data_size);
|
||||
file->size += data_size;
|
||||
file->data[file->size] = '\0';
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ramfs_create_directory(const char* name)
|
||||
{
|
||||
if (root_directory.file_count < RAMFS_MAX_FILES) {
|
||||
ramfs_directory_t* dir = (ramfs_directory_t*)malloc(sizeof(ramfs_directory_t));
|
||||
strncpy(dir->name, name, RAMFS_MAX_PATH_LEN);
|
||||
dir->file_count = 0;
|
||||
root_directory.files[root_directory.file_count++] = (ramfs_file_t*)dir;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ramfs_delete_directory(const char* name)
|
||||
{
|
||||
for (uint32_t i = 0; i < root_directory.file_count; i++) {
|
||||
if (strcmp(root_directory.files[i]->name, name) == 0) {
|
||||
// Delete the directory (along with its files)
|
||||
ramfs_directory_t* dir = (ramfs_directory_t*)root_directory.files[i];
|
||||
for (uint32_t j = 0; j < dir->file_count; j++) {
|
||||
free(dir->files[j]->data);
|
||||
}
|
||||
free(dir);
|
||||
root_directory.files[i] = root_directory.files[root_directory.file_count - 1];
|
||||
root_directory.file_count--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ramfs_check_permissions(const char* name, uint8_t required_permissions)
|
||||
{
|
||||
ramfs_file_t* file = ramfs_find_file(name);
|
||||
if (file) {
|
||||
return (file->permissions & required_permissions) == required_permissions;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ramfs_list_files()
|
||||
{
|
||||
printf("Files in RAMFS:\n");
|
||||
for (uint32_t i = 0; i < root_directory.file_count; i++) {
|
||||
printf("%s (Size: %u bytes)\n", root_directory.files[i]->name, root_directory.files[i]->size);
|
||||
}
|
||||
}
|
||||
|
||||
void ramfs_list_directories()
|
||||
{
|
||||
printf("Directories in RAMFS:\n");
|
||||
for (uint32_t i = 0; i < root_directory.file_count; i++) {
|
||||
if (root_directory.files[i]->data != NULL) {
|
||||
printf("%s\n", root_directory.files[i]->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ramfs_read_file(const char* name, uint8_t* buffer, uint32_t buffer_size)
|
||||
{
|
||||
ramfs_file_t* file = ramfs_find_file(name);
|
||||
if (file && (file->permissions & RAMFS_PERM_READ))
|
||||
{
|
||||
uint32_t bytes_to_read = (file->size < buffer_size) ? file->size : buffer_size;
|
||||
memcpy(buffer, file->data, bytes_to_read);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ramfs_write_file(const char* name, const uint8_t* data, uint32_t size)
|
||||
{
|
||||
return ramfs_append_to_file(name, data, size);
|
||||
}
|
||||
|
||||
bool ramfs_overwrite_file(const char* name, const uint8_t* data, uint32_t size)
|
||||
{
|
||||
ramfs_file_t* file = ramfs_find_file(name);
|
||||
if (!file || !(file->permissions & RAMFS_PERM_WRITE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Allocate space for data + null terminator
|
||||
uint8_t* new_data = realloc(file->data, size + 1);
|
||||
if (!new_data && size > 0) {
|
||||
return false; // realloc failed
|
||||
}
|
||||
|
||||
file->data = new_data;
|
||||
|
||||
if (size > 0 && data) {
|
||||
memcpy(file->data, data, size);
|
||||
}
|
||||
|
||||
file->data[size] = '\0'; // Ensure null-terminated
|
||||
file->size = size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ void* calloc(size_t nmemb, size_t size)
|
||||
void* ptr = malloc(total);
|
||||
if (ptr)
|
||||
{
|
||||
memset(ptr, 0, total);
|
||||
memset(ptr, 0, total);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
62
lib/stdio.c
Normal file
62
lib/stdio.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <drivers/ps2_keyboard.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern bool ps2keyboard_initialized;
|
||||
|
||||
char getchar(void)
|
||||
{
|
||||
if (ps2keyboard_initialized)
|
||||
{
|
||||
return get_char();
|
||||
}
|
||||
|
||||
return '\0';
|
||||
}
|
||||
|
||||
char* getstring(void)
|
||||
{
|
||||
if (ps2keyboard_initialized)
|
||||
{
|
||||
return get_string();
|
||||
}
|
||||
|
||||
return "HELLO\0";
|
||||
}
|
||||
|
||||
char* fgets(char* buf, int n, FILE file)
|
||||
{
|
||||
if (!buf || n <= 1 || file < 1)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int total_read = 0;
|
||||
char c;
|
||||
|
||||
while (total_read < n - 1)
|
||||
{
|
||||
int bytes = read_file(file, &c, 1);
|
||||
|
||||
if (bytes <= 0)
|
||||
{
|
||||
break; /* EOF or error */
|
||||
}
|
||||
|
||||
buf[total_read++] = c;
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
break; /* Stop at newline */
|
||||
}
|
||||
}
|
||||
|
||||
if (total_read == 0)
|
||||
{
|
||||
return NULL; /* Nothing read (e.g. EOF) */
|
||||
}
|
||||
|
||||
buf[total_read] = '\0';
|
||||
return buf;
|
||||
}
|
13
lib/stdlib.c
Normal file
13
lib/stdlib.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <drivers/pit.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
extern bool pit_initialized;
|
||||
|
||||
void sleep(uint64_t millis)
|
||||
{
|
||||
if (pit_initialized)
|
||||
{
|
||||
pit_sleep(millis);
|
||||
}
|
||||
}
|
80
lib/string.c
80
lib/string.c
@ -1,6 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <vector_extentions/sse.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <vector_extentions/sse.h>
|
||||
|
||||
extern int16_t sse_initialized;
|
||||
|
||||
@ -77,6 +79,82 @@ void strcat(char *dest, const char *src)
|
||||
*end = '\0';
|
||||
}
|
||||
|
||||
char* strdup(const char* s)
|
||||
{
|
||||
if (!s)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t len = strlen(s);
|
||||
char* copy = (char*)malloc(len + 1);
|
||||
|
||||
if (!copy)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(copy, s, len);
|
||||
copy[len] = '\0';
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
char* strtok(char* str, const char* delim)
|
||||
{
|
||||
static char* next_token = NULL;
|
||||
|
||||
if (str != NULL)
|
||||
{
|
||||
next_token = str;
|
||||
}
|
||||
else if (next_token == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (*next_token && strchr(delim, *next_token))
|
||||
{
|
||||
next_token++;
|
||||
}
|
||||
|
||||
if (*next_token == '\0')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* token_start = next_token;
|
||||
|
||||
while (*next_token && !strchr(delim, *next_token))
|
||||
{
|
||||
next_token++;
|
||||
}
|
||||
|
||||
if (*next_token)
|
||||
{
|
||||
*next_token = '\0';
|
||||
next_token++;
|
||||
}
|
||||
|
||||
return token_start;
|
||||
}
|
||||
|
||||
char* strchr(const char* s, int c)
|
||||
{
|
||||
while (*s)
|
||||
{
|
||||
if (*s == (char)c)
|
||||
{
|
||||
return (char*)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void *memset(void *dst, char c, uint32_t n)
|
||||
{
|
||||
char *temp = dst;
|
||||
|
Reference in New Issue
Block a user