"cwd[] should be MAXPATHLEN bytes long"
This is actually a common mistake — PATH_MAX and the like are not guaranteed to be defined. In fact, in POSIX it is literally impossible to ever know whether your buffer is long enough. Your best bet is,
#ifdef PATH_MAX
char cwd[PATH_MAX];
long len = sizeof cwd;
#else
char *cwd;
long len;
if((len = pathconf(".", _PC_PATH_MAX)) < 0)
len = 8192; /* some arbitrary value */
cwd = malloc(len);
#endif
This is probably one of the ugliest parts of POSIX.