Wrote a new stdio.h, Implemented fcntl.h

This commit is contained in:
2024-10-25 12:32:22 +02:00
parent fe2dac1361
commit bd7e00e7e8
6 changed files with 76 additions and 252 deletions
-17
View File
@@ -1,17 +0,0 @@
#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;
}
-68
View File
@@ -1,68 +0,0 @@
#include <stdio.h>
#include <syscall.h>
#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!
int fd = open(filename, oflag);
if (fd < 0) {
printf(""); // TODO: i'd prefer if i didn't need this
return NULL;
}
// TODO: Somehow get the open file table
FILE* fp = malloc(sizeof(FILE));
fp->_file = fd;
return fp;
}
int fclose(FILE* stream) {
int res = close(stream->_file);
return res;
}
size_t fread(void* ptr, size_t size, size_t count, FILE* stream) {
unsigned int offset = 0;
size_t readEl = 0;
int fd = stream->_file;
while (readEl != count) {
if (read(fd, (void*)(ptr + offset), size) != (int)size) break;
offset += size;
readEl++;
}
return readEl;
}
size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
unsigned int offset = 0;
size_t writtenEl = 0;
int fd = stream->_file;
while (writtenEl != count) {
if (write(fd, (void*)(ptr + offset), size) != (int)size) break;
offset += size;
writtenEl++;
}
return writtenEl;
}
int fseek(FILE* stream, long int origin, int offset) {
// TODO
return -1;
}
long int ftell(FILE* stream) {
// TODO
return -1;
}
-129
View File
@@ -1,129 +0,0 @@
#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) {
static char* buffer;
size_t len = 0;
size_t max = 256;
buffer = malloc(max);
size_t i = 0;
while (fmt[i] != '\0') {
if (len > max) {
max *= 2;
// Reallocate a larger buffer
char* temp = malloc(max);
strcpy(temp, buffer);
free(buffer);
buffer = temp;
}
// Formatting magic...
if (fmt[i] == '%') { i++;
/* TODO:
%p - pointer (0x########)
%n - ""
*/
int pad = -1;
char padc = ' ';
if (fmt[i] == '0') { i++; padc = '0'; }
if (fmt[i] >= '0' && fmt[i] <= '9') {
pad = atoi(fmt + i);
while (fmt[i] >= '0' && fmt[i] <= '9') { i++; }
}
while (pad > 0) { buffer[len++] = padc; pad--; }
char c = fmt[i++];
if (c == 'd' || c == 'x' || c == 'o') {
int base = 10;
if (c == 'x') base = 16;
else if (c == 'o') base = 8;
char* s = itoa(va_arg(args, int), base);
if (pad == 0) len -= strlen(s);
while ((buffer[len++] = *s++) != '\0');
len--;
} else if (c == 'c') {
if (pad == 0) len--;
buffer[len++] = va_arg(args, char);
} else if (c == 's') {
char* s = va_arg(args, char*);
if (pad == 0) len -= strlen(s);
while ((buffer[len++] = *s++) != '\0');
len--;
} else if (c == '%') {
buffer[len++] = '%';
} else {
buffer[len++] = '%';
if (padc == '0') { buffer[len++] = '0'; }
if (pad > 0) { buffer[len++] = '#'; } // TODO
buffer[len++] = c;
}
} else { buffer[len++] = fmt[i++]; }
}
buffer[len] = '\0';
return buffer;
}
int sprintf(char* str, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* buffer = _format(fmt, args);
strcpy(str, buffer);
free(buffer);
va_end(args);
return strlen(str);
}
int snprintf(char* str, size_t n, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* buffer = _format(fmt, args);
strncpy(str, buffer, n);
free(buffer);
va_end(args);
return strlen(str);
}
int fprintf(FILE* stream, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* buffer = _format(fmt, args);
size_t len = strlen(buffer);
write(stream->_file, buffer, len); // TODO: use fwrite()
free(buffer);
va_end(args);
return len;
}
int printf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* buffer = _format(fmt, args);
size_t len = strlen(buffer);
write(stdout->_file, buffer, len); // TODO: use fwrite()
free(buffer);
va_end(args);
return len;
}
void print(const char* s) {
write(1, s, strlen(s));
}
-23
View File
@@ -1,23 +0,0 @@
#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
}