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

首先想到getcwd()可能会返回NULL,当cwd不够大时。还有其他情况吗?getcwd()在什么情况下返回NULL?

+3

如果您有关于函数的精确语义问题,看看相关的文档,拼写出来:语言标准,POSIX标准,man-pages,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(你的链接是2004年版的POSIX--差异应该是最小的) – 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

相关问题