chdir (nr 80)
Linux Signature
int chdir(const char *path);
Description
Changes the current working directory to path.
Current Implementation
- Reads a null-terminated path string from user space (max 4096 bytes). Returns
-EFAULT(-14) if the pointer is invalid. - Resolves the path relative to the process’s current
cwd. Normalises.and..components. - Validates that the resolved path is an existing directory by calling
devices::vfs::list_dir()(throughosl::blocking::blocking()). This blocks the calling thread while the async VFS operation completes. - On success, updates the process’s
cwdfield to the resolved path and returns 0. - On failure, returns the error from the VFS (typically
-ENOENTor-ENOTDIR).
Source: osl/src/syscalls/fs.rs — sys_chdir
Usage from C (musl)
#include <unistd.h>
if (chdir("/some/path") < 0) {
/* error */
}
Errors
| Errno | Condition |
|---|---|
-EFAULT (-14) | Invalid path pointer |
-ENOENT (-2) | Path does not exist |
-ENOTDIR (-20) | A component of the path is not a directory |
-EIO (-5) | VFS I/O error |
Future Work
- Support
fchdir(fd)to change directory via an open directory fd.