libstd: refine dynamic memory allocations in os::getcwd() and os::change_dir()

`getcwd(NULL, ...)` is indeed a widely available extension, but still, POSIX states the result is unspecified, and musl-libc which is a popular alternative libc for Linux returns an allocated buffer *only if* the cwd is less than 4096 bytes.  So I decided not to rely on it.