filesystem.h

This commit is contained in:
2025-08-04 13:47:24 +03:00
parent 4c3933f789
commit 961d00fdb0
6 changed files with 172 additions and 0 deletions

34
src/filesystem/file.c Normal file
View File

@@ -0,0 +1,34 @@
#include "tlibc/filesystem.h"
#include "io_includes.h"
bool file_exists(cstr path){
if(path[0]=='.'){
if(path[1]==0 || (path[1]==path_sep && path[2]==0))
return false; // . or ./ is not a file
// else if(path[1]=='.' && path[2]==path_sep)
//TODO path_resolve because windows doesnt recognize .\ pattern
}
#if KFS_USE_WINDOWS_H
DWORD dwAttrib = GetFileAttributes(path);
return (bool)(
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory
#else
struct stat stats;
i32 rez=stat(path, &stats);
return (bool)(
(rez!=-1) && // file exists
!(S_ISDIR(stats.st_mode))); // file is not directory
#endif
}
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
FILE* f = fopen(file_name, fopen_mode);
if(f == NULL){
char* errmsg = sprintf_malloc(256, "can't open (%s) file '%s': %s",
fopen_mode, file_name, strerror(errno));
return RESULT_ERROR(errmsg, true);
}
return RESULT_VALUE(p, f);
}