【问题标题】:In what circumstances does getcwd() return NULL?getcwd()在什么情况下返回NULL?
【发布时间】:2015-01-06 12:37:46
【问题描述】:
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
    return -1;
}

首先想到getcwd() 可以返回NULL,当cwd 不够大时。还有其他情况吗?

【问题讨论】:

  • 如果您对函数的确切语义有疑问,请查看相关文档的拼写:语言标准、POSIX 标准、手册页、MSDN、...为您引用页面的人很懒惰,而且不是很有用。此外,它需要更长的时间。

标签: c getcwd


【解决方案1】:

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.

【讨论】:

【解决方案2】:
 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

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 2021-08-28
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多