Fix getcwd to not rely on MAXPATHLEN

Dynamically allocate the buffer for getcwd, instead of assuming it is at most some arbitrarly-fixed size. Also, optionally use PATH_MAX (which is optional in POSIX) as starting size for the buffer, instead of MAXPATHLEN (which is not even POSIX).