.TH getcwd() "" "" "General Function (libc)"
.PC "Get current working directory name"
.B "#include <unistd.h>"
\fBchar *getcwd(\fIbuffer\fB\^, \fIsize\^\fB)\fR
\fBchar *\fIbuffer\fB;\fR
\fBint \fIsize\fB;\fR
.PP
The current working directory is the directory from which file-name
searches commence when a path name does not begin with `/'.
.B getcwd()
returns the name of the current working directory.
It is useful for processes
like spoolers and daemons, which must generate full path names for files.
.PP
If
.I buffer
is not NULL,
.B getcwd()
writes the path of the current working directory into it.
The expected path name must not be longer than two characters less than
.IR size .
In this case,
.B getcwd()
returns
.IR buffer .
.PP
If
.I buffer
is NULL,
.B getcwd()
.BR malloc() 's
.I size
bytes.
.B getcwd()
returns a pointer to this block of memory.
You can
.B free()
it later.
.PP
If you do not have permission to search all levels of the directory hierarchy
above the current directory,
.B getcwd()
cannot obtain the directory name for you.
.SH "See Also"
.Xr "chdir()," chdir
.Xr "libc," libc
.Xr "pwd," pwd
.Xr "unistd.h" unistd.h
.br
\*(PX \(sc5.2.2
.SH Diagnostics
.B getcwd()
returns NULL and sets
.B errno
to an appropriate value if an error occurs.
Possible
errors include the following:
.IP \fBEPERM\fR 0.8i
Could not read one of the parent directories.
.IP \fBEINVAL\fR
.I size
is zero.
.IP \fBENOMEM\fR
Memory could not be
.BR malloc() 'd
for the buffer.
.IP \fBERANGE\fR
The path name is too long to fit into
.I size
minus two bytes.
.SH Notes
If
.B getcwd()
fails, the working directory cannot be restored to its initial value.
