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

Out of curiosity, is this faster than the fallback of `getcwd(NULL, ...)` ?