getcwd (nr 79)
Linux Signature
char *getcwd(char *buf, size_t size);
Description
Copies the absolute pathname of the current working directory into buf. On success, returns buf. On failure, returns -1 and sets errno.
Current Implementation
- Validates that
bufis within user address space. Returns-EFAULT(-14) if not. - Reads the
cwdfield from the current process’sProcessstruct. - Checks that
sizeis large enough to hold the cwd string plus a null terminator. Returns-ERANGE(-34) if too small. - Copies the cwd string and null terminator into the user buffer.
- Returns
buf(the pointer value) on success — matching Linux’s behaviour where the return value is the buffer address.
Each process has its own cwd field (default "/"), updated by chdir.
Source: osl/src/syscalls/fs.rs — sys_getcwd
Usage from C (musl)
#include <unistd.h>
char buf[256];
if (getcwd(buf, sizeof(buf)) != NULL) {
/* buf contains the current working directory */
}
Or via raw syscall:
#include <sys/syscall.h>
char buf[256];
long ret = syscall(SYS_getcwd, buf, sizeof(buf));
/* ret > 0 on success (pointer to buf) */
Future Work
- Support
getcwd(NULL, 0)which auto-allocates a buffer (musl handles this in userspace).