diff --git a/kernel/src/Api/Process.hpp b/kernel/src/Api/Process.hpp index f8de325..1e10ce1 100644 --- a/kernel/src/Api/Process.hpp +++ b/kernel/src/Api/Process.hpp @@ -165,10 +165,15 @@ namespace montauk::abi { if (proc == nullptr || buf == nullptr || maxLen == 0) return -1; const char* cwd = proc->cwd[0] ? proc->cwd : "0:/"; - int i = 0; - for (; i < (int)maxLen - 1 && cwd[i]; i++) buf[i] = cwd[i]; - buf[i] = '\0'; - return i; + uint64_t cwdLen = 0; + while (cwd[cwdLen] != '\0') cwdLen++; + + // Reserve -1 for non-size failures so libc can distinguish them + // from a buffer that can be retried with a larger allocation. + if (maxLen <= cwdLen) return -2; + + for (uint64_t i = 0; i <= cwdLen; i++) buf[i] = cwd[i]; + return (int)cwdLen; } // ==================================================================== diff --git a/programs/lib/libc/libc.c b/programs/lib/libc/libc.c index 85c01f2..b6344fa 100644 --- a/programs/lib/libc/libc.c +++ b/programs/lib/libc/libc.c @@ -2286,8 +2286,9 @@ char *getcwd(char *buf, size_t size) { errno = EINVAL; return NULL; } - if ((int)_zos_syscall2(SYS_GETCWD, (long)buf, (long)size) < 0) { - errno = ERANGE; + int result = (int)_zos_syscall2(SYS_GETCWD, (long)buf, (long)size); + if (result < 0) { + errno = result == -2 ? ERANGE : EIO; return NULL; } return buf; diff --git a/programs/lib/libc/obj/libc.o b/programs/lib/libc/obj/libc.o index f9ef7a7..14b8f35 100644 Binary files a/programs/lib/libc/obj/libc.o and b/programs/lib/libc/obj/libc.o differ diff --git a/programs/man/syscalls.2 b/programs/man/syscalls.2 index 43f1000..5d4a3a8 100644 --- a/programs/man/syscalls.2 +++ b/programs/man/syscalls.2 @@ -64,7 +64,9 @@ int montauk::chdir(const char* path); .B SYS_GETCWD (95) - Get the calling process's current working directory. + Get the calling process's current working directory. Returns the path + length on success, -2 if maxLen cannot hold the path and terminating NUL, + or -1 for another failure. The buffer is not modified when it is too small. int montauk::getcwd(char* buf, uint64_t maxLen); .B SYS_SETUSER (92)