NAME
getcwd() — get pathname of current working directory
SYNOPSIS
#include <unistd.h>
char *getcwd(char *buf, size_t size);
DESCRIPTION
The
getcwd()
function places the absolute pathname of the current working directory
in the array pointed to by
buf,
and returns
buf.
The value of
size
must be at least one greater
than the length of the pathname to be returned.
If
buf
is a
NULL
pointer,
getcwd()
obtains
size
bytes of space using
malloc()
(see
malloc(3C)).
In this case, the pointer returned by
getcwd()
can be used as the argument in a subsequent call to
free()
(see
malloc(3C)).
Invoking
getcwd()
with
buf
as a null pointer is not recommended
because this functionality may be removed from the
HP-UX
operating system in a future release.
RETURN VALUE
Upon successful completion,
getcwd()
returns a pointer to the current directory pathname.
Otherwise, it returns
NULL
with
errno
set if
size
is not large enough,
or if an error occurs in a lower-level function.
ERRORS
getcwd()
fails if any of the following conditions are encountered:
- EINVAL
The
size
argument is zero.
- ERANGE
The
size
argument is greater than zero,
but is smaller than the length of the pathname.
- ENAMETOOLONG
The length of the specified pathname exceeds
PATH_MAX+1
bytes, or the length of a component of the pathname exceeds
NAME_MAX
bytes while
_POSIX_NO_TRUNC
is in effect.
getcwd()
may fail if any of the following conditions are encountered:
- EACCES
Read or search permission is denied for a component of pathname.
- EFAULT
buf
points outside the allocated address space of the process.
getcwd()
may not always detect this error.
- ENOMEM
malloc()
failed to provide
size
bytes of memory.
EXAMPLES
#include <unistd.h>
#include <limits.h>
char *cwd;
char buf[PATH_MAX+1];
...
if ((cwd = getcwd(buf, PATH_MAX+1)) == NULL) {
perror("pwd");
exit(1);
}
puts(cwd);
AUTHOR
getcwd()
was developed by AT&T.
STANDARDS CONFORMANCE
getcwd(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1