summaryrefslogtreecommitdiff
path: root/std/fs.zc
diff options
context:
space:
mode:
Diffstat (limited to 'std/fs.zc')
-rw-r--r--std/fs.zc62
1 files changed, 25 insertions, 37 deletions
diff --git a/std/fs.zc b/std/fs.zc
index 4547b30..a00993b 100644
--- a/std/fs.zc
+++ b/std/fs.zc
@@ -14,12 +14,20 @@ include <unistd.h>
include <stdlib.h>
include <stdio.h>
-// TODO: restructure this tomorrow (lol).
+// Direct externs for simple functions with const char* parameters
+extern fn access(pathname: const char*, mode: int) -> int;
+extern fn unlink(pathname: const char*) -> int;
+extern fn rmdir(pathname: const char*) -> int;
+extern fn malloc(size: usize) -> void*;
+extern fn free(ptr: void*);
+
+// Minimal raw block: required for opaque FILE*/DIR* types and C struct access
+// These cannot be expressed in Zen-C extern declarations without type conflicts
raw {
typedef struct DirEntry* DirEntryPtr;
- // Wrappers for FILE* handling due to opaque pointer casting
- void* _z_fs_fopen(char* path, char* mode) {
+ // FILE* wrappers - fopen/fclose/etc use FILE* which conflicts with void*
+ void* _z_fs_fopen(const char* path, const char* mode) {
return fopen(path, mode);
}
@@ -43,22 +51,8 @@ raw {
return (int64_t)ftell((FILE*)stream);
}
- // Wrappers needed because C headers declare these with 'const char*'
- // but Zen C externs generate 'char*', leading to conflicting types.
- int _z_fs_access(char* pathname, int mode) {
- return access(pathname, mode);
- }
-
- int _z_fs_unlink(char* pathname) {
- return unlink(pathname);
- }
-
- int _z_fs_rmdir(char* pathname) {
- return rmdir(pathname);
- }
-
- // Wrappers for DIR* handling
- void* _z_fs_opendir(char* name) {
+ // DIR* wrappers - opendir/closedir/readdir use DIR* which conflicts with void*
+ void* _z_fs_opendir(const char* name) {
return opendir(name);
}
@@ -66,8 +60,8 @@ raw {
return closedir((DIR*)dir);
}
- // struct stat / struct dirent helpers
- int _z_fs_get_metadata(char* path, uint64_t* size, int* is_dir, int* is_file) {
+ // struct stat access - cannot define matching Zen-C struct for stat
+ int _z_fs_get_metadata(const char* path, uint64_t* size, int* is_dir, int* is_file) {
struct stat st;
if (stat(path, &st) != 0) return -1;
*size = st.st_size;
@@ -76,6 +70,7 @@ raw {
return 0;
}
+ // struct dirent access - readdir returns struct dirent*
int _z_fs_read_entry(void* dir, char* out_name, int buf_size, int* is_dir) {
struct dirent* ent = readdir((DIR*)dir);
if (!ent) return 0;
@@ -85,7 +80,8 @@ raw {
return 1;
}
- int _z_fs_mkdir(char* path) {
+ // mkdir has different signatures on Windows vs POSIX
+ int _z_fs_mkdir(const char* path) {
#ifdef _WIN32
return mkdir(path);
#else
@@ -94,26 +90,18 @@ raw {
}
}
-// Direct externs
-extern fn malloc(size: usize) -> void*;
-extern fn free(ptr: void*);
-
-extern fn _z_fs_mkdir(path: char*) -> int;
-extern fn _z_fs_get_metadata(path: char*, size: U64*, is_dir: int*, is_file: int*) -> int;
+extern fn _z_fs_mkdir(path: const char*) -> int;
+extern fn _z_fs_get_metadata(path: const char*, size: U64*, is_dir: int*, is_file: int*) -> int;
extern fn _z_fs_read_entry(dir: void*, out_name: char*, buf_size: int, is_dir: int*) -> int;
-extern fn _z_fs_fopen(path: char*, mode: char*) -> void*;
+extern fn _z_fs_fopen(path: const char*, mode: const char*) -> void*;
extern fn _z_fs_fclose(stream: void*) -> int;
extern fn _z_fs_fread(ptr: void*, size: usize, nmemb: usize, stream: void*) -> usize;
extern fn _z_fs_fwrite(ptr: void*, size: usize, nmemb: usize, stream: void*) -> usize;
extern fn _z_fs_fseek(stream: void*, offset: I64, whence: int) -> int;
extern fn _z_fs_ftell(stream: void*) -> I64;
-extern fn _z_fs_opendir(name: char*) -> void*;
+extern fn _z_fs_opendir(name: const char*) -> void*;
extern fn _z_fs_closedir(dir: void*) -> int;
-extern fn _z_fs_access(pathname: char*, mode: int) -> int;
-extern fn _z_fs_unlink(pathname: char*) -> int;
-extern fn _z_fs_rmdir(pathname: char*) -> int;
-
struct File {
handle: void*;
@@ -203,7 +191,7 @@ impl File {
}
fn exists(path: char*) -> bool {
- return _z_fs_access(path, Z_F_OK) == 0;
+ return access(path, Z_F_OK) == 0;
}
fn metadata(path: char*) -> Result<Metadata> {
@@ -230,14 +218,14 @@ impl File {
}
fn remove_file(path: char*) -> Result<bool> {
- if (_z_fs_unlink(path) != 0) {
+ if (unlink(path) != 0) {
return Result<bool>::Err("Failed to remove file");
}
return Result<bool>::Ok(true);
}
fn remove_dir(path: char*) -> Result<bool> {
- if (_z_fs_rmdir(path) != 0) {
+ if (rmdir(path) != 0) {
return Result<bool>::Err("Failed to remove directory");
}
return Result<bool>::Ok(true);