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

Первое приходит на ум, что getcwd() может вернуть NULL, когда cwd недостаточно велик. Есть ли другие случаи?В каких обстоятельствах getcwd() возвращает NULL?

+3

Если у вас есть вопросы относительно точной семантики функции, смотрите в соответствующей документации по буквам это: Стандарт языка, стандарт POSIX, man-страницы, MSDN, ... Просить кого-то процитировать страницу для вас - ленивый и не очень полезный. Кроме того, это занимает больше времени. – Deduplicator

ответ

5

Its documentation состояния:

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. 
+1

POSIX.1-2008 (выпуск 2013 г.) => http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html (ваша ссылка на выпуск POSIX 2004 года - различия должны быть минимальными) – pmg

+0

@pmg Спасибо за ссылку! – glglgl

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. 

источник: человек getcwd