#include <unistd.h>
extern char *getcwd(char *buf, size_t size);
Safe
getcwd() 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().
getcwd() will fail if one or more of the following are true:
#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); }