Skip to content

Commit ee206cc

Browse files
committed
feat(libs.os): add chdir
1 parent 975cee3 commit ee206cc

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

compiler/libs/libs/os.yaka

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def cwd() -> str:
5353
free(path);
5454
return value"""
5555

56+
@nativedefine("yk__change_current_dir_path")
57+
def chdir(path: str) -> bool:
58+
# Change current working directory
59+
pass
5660

5761
@nativedefine("struct yk__process_result*")
5862
class ProcessResult:

compiler/runtime/yk__system.c

+22
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,11 @@ void yk__printlnstr(const char *str) {
132132
#if defined(_WIN32) || defined(_WIN64)
133133
#include <direct.h>
134134
#define GetCurrentDir _wgetcwd
135+
#define ChangeDirectory _wchdir
135136
#else
136137
#include <unistd.h>
137138
#define GetCurrentDir getcwd
139+
#define ChangeDirectory chdir
138140
#endif
139141
char *yk__get_current_dir_path() {
140142
#if defined(_WIN32) || defined(_WIN64)
@@ -157,6 +159,26 @@ char *yk__get_current_dir_path() {
157159
return buff;
158160
#endif
159161
}
162+
bool yk__change_current_dir_path(yk__sds path) {
163+
if (path == NULL || !yk__sdslen(path)) {
164+
yk__sdsfree(path);
165+
return false;
166+
}
167+
#if defined(_WIN32) || defined(_WIN64)
168+
wchar_t *wpath = yk__utf8_to_utf16_null_terminated(path);
169+
if (wpath == NULL) {
170+
return false;
171+
}
172+
int result = ChangeDirectory(wpath);
173+
yk__sdsfree(path);
174+
free(wpath);
175+
return result == 0;
176+
#else
177+
int result = ChangeDirectory(path);
178+
yk__sdsfree(path);
179+
return result == 0;
180+
#endif
181+
}
160182
////// File stuff ///////////
161183
bool yk__exists(yk__sds path) {
162184
if (path == NULL || !yk__sdslen(path)) {

compiler/runtime/yk__system.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void yk__printlnuint(uintmax_t to_print);
2121
void yk__printdbl(double to_print);
2222
void yk__printlndbl(double to_print);
2323
char *yk__get_current_dir_path();
24+
bool yk__change_current_dir_path(yk__sds path);
2425
bool yk__exists(yk__sds path);
2526
bool yk__readable(yk__sds path);
2627
bool yk__writable(yk__sds path);

0 commit comments

Comments
 (0)