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

cwd が十分に大きくない場合、最初に をgetcwd()返す可能性があることが頭に浮かびます。NULL他に事例はありますか?

4

2 に答える 2

5

そのドキュメントには次のように記載されています。

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.
于 2015-01-06T12:40:36.513 に答える
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.

ソース: man getcwd

于 2015-01-06T12:40:39.273 に答える