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

처음에는 getcwd()이 cwd가 충분히 크지 않을 때 NULL을 반환 할 수 있다는 사실을 염두에 둡니다. 다른 경우가 있습니까?getcwd()는 어떤 상황에서 NULL을 반환합니까?

+3

을에 getcwd 사람 : 언어 표준, POSIX 표준, 맨 페이지, 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 Edition) => 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. 

소스 : 당신이 그것을 맞춤법 관련 문서를 보면, 함수의 정확한 의미에 대한 질문이있는 경우

관련 문제