diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-11 22:31:35 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-11 22:32:31 +0200 |
commit | 974eafbb07578742ac6aa965c73b62792865ebd7 (patch) | |
tree | 810cebb00b5fee50b834efa1e9b12893ec14b5eb | |
parent | c06bab1cf39d378e4d99ffe89e8079ab817c51c0 (diff) |
fs: implement getcwd() and chdir()
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | lib/fs.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -622,6 +622,55 @@ ut_fs_unlink(struct ut_state *s, uint32_t off, struct json_object *args) return json_object_new_boolean(true); } +static struct json_object * +ut_fs_getcwd(struct ut_state *s, uint32_t off, struct json_object *args) +{ + struct json_object *res; + char *buf = NULL, *tmp; + size_t buflen = 0; + + do { + buflen += 128; + tmp = realloc(buf, buflen); + + if (!tmp) { + free(buf); + err_return(ENOMEM); + } + + buf = tmp; + + if (getcwd(buf, buflen) != NULL) + break; + + if (errno == ERANGE) + continue; + + err_return(errno); + } + while (true); + + res = json_object_new_string(buf); + + free(buf); + + return res; +} + +static struct json_object * +ut_fs_chdir(struct ut_state *s, uint32_t off, struct json_object *args) +{ + struct json_object *path = json_object_array_get_idx(args, 0); + + if (!json_object_is_type(path, json_type_string)) + err_return(EINVAL); + + if (chdir(json_object_get_string(path)) == -1) + err_return(errno); + + return json_object_new_boolean(true); +} + static const struct { const char *name; ut_c_fn *func; } proc_fns[] = { { "read", ut_fs_pread }, { "write", ut_fs_pwrite }, @@ -655,6 +704,8 @@ static const struct { const char *name; ut_c_fn *func; } global_fns[] = { { "rmdir", ut_fs_rmdir }, { "symlink", ut_fs_symlink }, { "unlink", ut_fs_unlink }, + { "getcwd", ut_fs_getcwd }, + { "chdir", ut_fs_chdir }, }; |