kdb: do not rely on PATH_MAX for getcwd

PATH_MAX is optional in POSIX, so can not be provided; isolate the
getcwd code in an own function that uses PATH_MAX only as initial hint,
enlarging the buffer as long as it is needed.