getcwd(3)
Name
getcwd − get pathname of working directory
Syntax
char *getcwd (buf, size)
char *buf;
int size;
Description
The getcwd subroutine returns a pointer to the current directory pathname. The value of size must be at least two greater than the length of the pathname to be returned.
If buf is a NULL pointer, getcwd will obtain size bytes of space using malloc(3) In this case, the pointer returned by getcwd may be used as the argument in a subsequent call to free.
The function is implemented by using popen(3) to pipe the output of the pwd(1) command into the specified string space.
Examples
char ∗cwd, ∗getcwd();
.
.
.
if ((cwd = getcwd((char ∗)NULL, 64)) == NULL) {
perror("pwd");
exit(1);
}
printf("%s\n", cwd);
Return Value
Returns NULL with errno set if size is not large enough, or if an error occurs in a lower-level function.
Diagnostics
[EINVAL] The size argument is zero or negative.
[ERANGE] The size argument is greater than zero, but is smaller than the length of the pathname+1;
[EACCES] Read or search permission is denied for a component of the pathname.
[ENOMEM] Insufficient storage space is available.