-6
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?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 3
    If you have a question about the exact semantics of a function, look at the relevant documentation spelling it out: The language-standard, the POSIX-standard, man-pages, MSDN, ... Asking someone to quote the page for you is lazy and not very useful. Besides, it takes longer. – Deduplicator Jan 06 '15 at 12:51

2 Answers2

5

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.
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • 1
    POSIX.1-2008 (2013 Edition) => http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html (your link is to the 2004 edition of POSIX -- differences should be minimal anyway) – pmg Jan 06 '15 at 13:09
1
 If the length of the absolute pathname of the  current  working  direc‐
       tory,  including the terminating null byte, exceeds size bytes, NULL is
       returned, and errno is set to ERANGE; an application should  check  for
       this error, and allocate a larger buffer if necessary.

source: man getcwd

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93