added dir_setCurrent, dir_getCurrent

This commit is contained in:
2026-06-09 23:59:41 +05:00
parent 649c2c0968
commit 9db2d003f5
2 changed files with 44 additions and 0 deletions

View File

@@ -31,6 +31,8 @@
#define path_notseps "\\" #define path_notseps "\\"
#endif #endif
#define path_max_len 1024
// @brief replaces path_notsep with path_sep // @brief replaces path_notsep with path_sep
void path_fix_separators(str* path); void path_fix_separators(str* path);
@@ -162,3 +164,7 @@ Result(bool) dir_create(cstr path);
/// EXAMPLE: dir_createParent("a/b/c") -> creates "a", "a/b" /// EXAMPLE: dir_createParent("a/b/c") -> creates "a", "a/b"
/// @return false if directory was present already, true if it has been created /// @return false if directory was present already, true if it has been created
Result(bool) dir_createParent(cstr path); Result(bool) dir_createParent(cstr path);
/// @return heap-allocated char array
Result(char*) dir_getCurrent();
Result(void) dir_setCurrent(cstr path);

View File

@@ -64,3 +64,41 @@ Result(bool) dir_createParent(cstr path){
try(bool result, i, dir_create(parent_dir_cstr)); try(bool result, i, dir_create(parent_dir_cstr));
Return RESULT_VALUE(i, result); Return RESULT_VALUE(i, result);
} }
Result(void) dir_setCurrent(cstr path){
int r =
#if TLIBC_FS_USE_WINDOWS_H
_chdir(path);
#else
chdir(path);
#endif
if(r != 0)
{
char* errno_s = strerror_malloc(errno);
char* err = sprintf_malloc(
"Can't set current dicectory to '%s': %s",
path, errno_s);
free(errno_s);
return RESULT_ERROR_HEAP(err);
}
return RESULT_VOID;
}
Result(char*) dir_getCurrent(){
char* buf =
#if TLIBC_FS_USE_WINDOWS_H
_getcwd(NULL, path_max_len);
#else
getcwd(NULL, path_max_len);
#endif
if(buf == NULL)
{
char* errno_s = strerror_malloc(errno);
char* err = sprintf_malloc(
"Can't get current dicectory: %s",
errno_s);
free(errno_s);
return RESULT_ERROR_HEAP(err);
}
return RESULT_VALUE(p, buf);
}