'\"macro stdmacro
.if n .pH g3c.getcwd @(#)getcwd	40.11 of 10/25/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} getcwd 3C "C Programming Language Utilities" "\&"
.if \nX=1 .ds x} getcwd 3C "C Programming Language Utilities"
.if \nX=2 .ds x} getcwd 3C "" "\&"
.if \nX=3 .ds x} getcwd "" "" "\&"
.TH \*(x}
.SH NAME
\f4getcwd\f1 \- get pathname of current working directory
.SH SYNOPSIS
.nf
\f4#include <unistd.h>\f1
.PP
\f4char \(**getcwd (char \(**buf, int size);\f1
.fi
.SH DESCRIPTION
\f4getcwd\fP
returns a pointer to
the current directory pathname.
The value of
.I size
must be at least one greater than the length of the
pathname to be returned.
.PP
If
.I buf
is not
\f4NULL\fP,
the pathname will be stored in the space pointed to by \f2buf\f1.
.PP
If
.I buf
is a
\f4NULL\fP
pointer,
\f4getcwd\fP
will obtain
.I size
bytes of space using
\f4malloc\fP(3C).
In this case, the pointer returned by
\f4getcwd\fP
may be used as the argument in a subsequent call to
\f4free\fP.
.PP
\f4getcwd\fP will fail if one or more of the following are true:
.TP 12
\f4EACCES\fP
A parent directory cannot be read to get its name.
.TP 12
\f4EINVAL\fP
\f2size\fP is less than or equal to 0.
.TP 12
\f4ERANGE\fP
\f2size\fP is greater than 0 and less than the length of the pathname plus 1.
.SH EXAMPLE
Here is a program
that prints the current working directory.
.PP
.RS
.nf
.ft 4
#include <unistd.h>
#include <stdio.h>

main()
{
	char \(**cwd;
	if ((cwd = getcwd(NULL, 64)) == NULL)
	{
		perror("pwd");
		exit(2);
	}
	(void)printf("%s\en", cwd);
	return(0);
}
.fi
.ft 1
.RE
.SH "SEE ALSO"
\f4malloc\fP(3C).
.SH DIAGNOSTICS
Returns
\f4NULL\f1
with
\f4errno\f1
set if
.I size
is not large enough, or if an error occurs
in a lower-level function.
.ft 1
.\"	@(#)getcwd.3c	6.2 of 10/20/83
.Ee
