What should i fill in the second parameter of the function getcwd if I am reading the current directory?
getcwd second parameter
809 views Asked by lital maatuk At
2
There are 2 answers
2
On
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.