开发者

getcwd second parameter

What should i fill in the second parameter of the function getcwd if I am reading开发者_运维百科 the current directory?


The size of the buffer you want to fill:

char result[PATH_MAX];
char *r = getcwd(result, PATH_MAX);

Failure to set this correctly (or spot ENAMETOOLONG/ERANGE) could lead to buffer overflow problems.

Caveat: Not all platforms provide PATH_MAX. If you can be sure it's there on your platforms it is quite handy.

You can also use realpath(), (POSIX.1-2008) which will malloc() memory for you to do this more cleanly:

char *result = realpath(".", NULL);
// do stuff with result
free(result);


The length of the buffer you provide in the first parameter, so that overflow cannot occur.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜