Skip to content

Commit 988127f

Browse files
committed
add C functions
1 parent 7056a36 commit 988127f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/stdlib_system.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <limits.h>
12
#include <stddef.h>
3+
#include <stdlib.h>
24
#include <sys/stat.h>
35
#include <sys/types.h>
46
#include <string.h>
@@ -44,3 +46,43 @@ int stdlib_remove_directory(const char* path){
4446

4547
return errno;
4648
}
49+
50+
char* stdlib_get_cwd(size_t* len, int* stat){
51+
*stat = 0;
52+
#ifdef _WIN32
53+
char* buffer;
54+
buffer = _getcwd(NULL, 0);
55+
56+
if (buffer == NULL) {
57+
*stat = errno;
58+
return NULL;
59+
}
60+
61+
*len = strlen(buffer)
62+
return buffer;
63+
#else
64+
char buffer[PATH_MAX + 1];
65+
if (!getcwd(buffer, sizeof(buffer))) {
66+
*stat = errno;
67+
}
68+
69+
*len = strlen(buffer);
70+
71+
char* res = malloc(*len);
72+
strncpy(res, buffer, *len);
73+
74+
return res;
75+
#endif /* ifdef _WIN32 */
76+
}
77+
78+
int stdlib_set_cwd(char* path) {
79+
int code;
80+
#ifdef _WIN32
81+
code = _chdir(path);
82+
#else
83+
code = chdir(path);
84+
#endif /* ifdef _WIN32 */
85+
86+
if (code == -1) return errno;
87+
return 0;
88+
}

0 commit comments

Comments
 (0)