From 49ce94a6ab917b10b7829d92d68418a097380fc7 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Wed, 25 Feb 2026 08:36:40 +0100 Subject: [PATCH] os file IO fixes linux --- os.h | 4 ++-- os_linux.c | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/os.h b/os.h index dddee81..bfec7a2 100644 --- a/os.h +++ b/os.h @@ -11,8 +11,8 @@ void os_free(void *ptr, uint64 freeSize); // ### File IO ### string os_readEntireFile(Arena *arena, string filename); -bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength); -bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength); +bool os_writeEntireFile(string filename, const byte *contents, uint64 contentsLength); +bool os_fileAppend(string filename, const byte *contents, uint64 contentsLength); // ### Standard IO ### void os_print(StdStream target, const char *fmt, va_list argList); diff --git a/os_linux.c b/os_linux.c index 3ca4020..c292c7d 100644 --- a/os_linux.c +++ b/os_linux.c @@ -46,7 +46,7 @@ string os_readEntireFile(Arena *arena, string filename) { close(input); if (bytesRead == -1) { arenaPopTo(arena, readBuffer.str); - return s(""); + readBuffer = s(""); } } else { readBuffer = s(""); @@ -56,35 +56,37 @@ string os_readEntireFile(Arena *arena, string filename) { return readBuffer; } -bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength) { - Scratch temp = scratchStart(&arena, 1); +bool os_writeEntireFile(string filename, const byte *contents, uint64 contentsLength) { + Scratch temp = scratchStart(NULL, 0); bool result = false; - int output = open(cstring(temp.arena, filename), O_WRONLY); + struct stat st; + stat((char *)filename.str, &st); + int output = open(cstring(temp.arena, filename), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (output) { int64 bytesWritten = write(output, contents, contentsLength); if (bytesWritten != -1) { result = true; } - close(output); } + close(output); scratchEnd(temp); return result; } -bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength) { - Scratch temp = scratchStart(&arena, 1); +bool os_fileAppend(string filename, const byte *contents, uint64 contentsLength) { + Scratch temp = scratchStart(NULL, 0); bool result = false; - int output = open(cstring(temp.arena, filename), O_APPEND); + int output = open(cstring(temp.arena, filename), O_WRONLY | O_APPEND); if (output) { int bytesWritten = write(output, contents, contentsLength); if (bytesWritten != -1) { result = true; } - close(output); } + close(output); scratchEnd(temp); return result;