GETCWD(3) Library Functions Manual GETCWD(3)

getcwdget the pathname of the current working directory

#include <unistd.h>

char *getcwd(char *buf, size_t size);

The getcwd() function places an absolute pathname of the current working directory in the array pointed to by buf , and returns buf . The size argument is the size in bytes of the character array pointed to by the buf argument. If buf is a null pointer, the behaviour of getcwd() is undefined.

Upon successful completion, getcwd() returns the buf argument. Otherwise, getcwd() returns a null pointer and sets errno to indicate the error. The contents of the array pointed to by buf is then undefined.

The getcwd() function will fail if:

The size argument is 0.
The size argument is greater than 0, but is smaller than the length of the pathname +1.

The function may fail if:

Read or search permission was denied for a component of the pathname.
Insufficient storage space is available.

None.

If buf is a null pointer, getcwd() may obtain size bytes of memory using malloc(3) . In this case, the pointer returned by getcwd() may be used as the argument in a subsequent call to free(3) . Invoking getcwd() with buf as a null pointer is not recommended.

None.

malloc(3) , <unistd.h> .

Derived from Issue 1 of the SVID.

February 1997 posix.fail