Search code examples
cgetcwd

In what circumstances does getcwd() return NULL?


char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
    return -1;
}

First comes to mind that getcwd() could return NULL, when cwd is not large enough. Is there any other cases?


Solution

  • Its documentation states:

    ERRORS
    
        The getcwd() function shall fail if:
    
        [EINVAL]
            The size argument is 0.
        [ERANGE]
            The size argument is greater than 0, but is smaller than the length of the pathname +1.
    
        The getcwd() function may fail if:
    
        [EACCES]
            Read or search permission was denied for a component of the pathname.
        [ENOMEM]
            Insufficient storage space is available.