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.
精彩评论