FAT | RFS | XFS | ZFS
getcwd()
PROTOTYPE
#include <posix.h>
char *getcwd(char *buf, size_t size);
DESCRIPTION
getcwd() gets the absolute pathname of the CWD. If buf is not NULL, size is the number of bytes in the buffer it points to. The pathname is copied to this buffer, followed by a terminating null.
If buf equals NULL, size is ignored and malloc() is used to allocate a buffer of suitable size before the path is copied to it. This buffer must be freed, by calling free(), once it is no longer needed by the application.
The caller needs execute access to the current directory and both execute and read access to every directory above the current directory.
If successful, getcwd() returns a pointer to the absolute pathname of the CWD. This is either buf or a buffer allocated by getpath(). Otherwise, it sets errno and returns NULL.
ERROR CODES
| EACCES | No execute access to the current directory or no execute or read access to any parent directory. |
| EINVAL | size is zero and buf is not NULL. |
| ENOMEM | Unable to allocate memory buffer for path name. |
| ERANGE | buf is not NULL and size is negative or less than the length of the path name, including the terminating ‘\0’. |
EXAMPLE
/*-------------------------------------------------------------------*/
/* If the call to get the current dir fails, return error. */
/*-------------------------------------------------------------------*/
cwd = getcwd(NULL, 0);
if (cwd == NULL)
return -1;
/*-------------------------------------------------------------------*/
/* Output directory path name and free the path buffer. */
/*-------------------------------------------------------------------*/
printf("%s\n", cwd);
free(cwd);