getcwd(3C)
NAME
getcwd − get pathname of current working directory
SYNOPSIS
#include <unistd.h>
extern char ∗getcwd(char ∗buf, size_t size);
DESCRIPTION
The getcwd() function returns a pointer to the current directory pathname. The value of size must be at least one greater than the length of the pathname to be returned.
If buf is not NULL, the pathname will be stored in the space pointed to by buf.
If buf is a null pointer, getcwd() will obtain size bytes of space using malloc(3C). In this case, the pointer returned by getcwd() may be used as the argument in a subsequent call to free().
RETURN VALUES
The getcwd() function returns NULL with errno set if size is not large enough, or if an error occurs in a lower-level function.
ERRORS
The getcwd() function will fail if one or more of the following are true:
EACCES A parent directory cannot be read to get its name.
EINVAL The size argument is equal to 0.
ERANGE The size argument is greater than 0 and less than the length of the pathname plus 1.
EXAMPLES
Here is a program that prints the current working directory.
#include <unistd.h>
#include <stdio.h>
main()
{
char ∗cwd;
if ((cwd = getcwd(NULL, 64)) == NULL) {
perror("pwd");
exit(2);
}
(void)printf("%s\n", cwd);
return(0);
}
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
| MT-Level | MT-Safe |
SEE ALSO
chdir(2), malloc(3C), attributes(5)
NOTES
Applications should exercise care when using chdir(2) in conjunction with getcwd(). The current working directory is global to all threads within a process. If more than one thread calls chdir() to change the working directory, a subsequent call to getcwd() could produce results that are unexpected.
SunOS 5.6 — Last change: 27 Jan 1997