1.\" Copyright (c) 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)getcwd.3 8.2 (Berkeley) 12/11/93 33.\" 34.Dd December 11, 1993 35.Dt GETCWD 3 36.Os BSD 4.2 37.Sh NAME 38.Nm getcwd , 39.Nm getwd 40.Nd get working directory pathname 41.Sh SYNOPSIS 42.Fd #include <unistd.h> 43.Ft char * 44.Fn getcwd "char *buf" "size_t size" 45.Ft char * 46.Fn getwd "char *buf" 47.Sh DESCRIPTION 48The 49.Fn getcwd 50function copies the absolute pathname of the current working directory 51into the memory referenced by 52.Fa buf 53and returns a pointer to 54.Fa buf . 55The 56.Fa size 57argument is the size, in bytes, of the array referenced by 58.Fa buf . 59.Pp 60If 61.Fa buf 62is 63.Dv NULL , 64space is allocated as necessary to store the pathname. 65This space may later be 66.Xr free 3 Ns 'd. 67.Pp 68The function 69.Fn getwd 70is a compatibility routine which calls 71.Fn getcwd 72with its 73.Fa buf 74argument and a size of 75.Dv MAXPATHLEN 76(as defined in the include 77file 78.Aq Pa sys/param.h ) . 79Obviously, 80.Fa buf 81should be at least 82.Dv MAXPATHLEN 83bytes in length. 84.Pp 85These routines have traditionally been used by programs to save the 86name of a working directory for the purpose of returning to it. 87A much faster and less error-prone method of accomplishing this is to 88open the current directory 89.Pq Ql \&. 90and use the 91.Xr fchdir 2 92function to return. 93.Sh RETURN VALUES 94Upon successful completion, a pointer to the pathname is returned. 95Otherwise a 96.Dv NULL 97pointer is returned and the global variable 98.Va errno 99is set to indicate the error. 100In addition, 101.Fn getwd 102copies the error message associated with 103.Va errno 104into the memory referenced by 105.Fa buf . 106.Sh ERRORS 107The 108.Fn getcwd 109function 110will fail if: 111.Bl -tag -width [EACCESS] 112.It Bq Er EACCESS 113Read or search permission was denied for a component of the pathname. 114.It Bq Er EINVAL 115The 116.Fa size 117argument is zero. 118.It Bq Er ENOENT 119A component of the pathname no longer exists. 120.It Bq Er ENOMEM 121Insufficient memory is available. 122.It Bq Er ERANGE 123The 124.Fa size 125argument is greater than zero but smaller than the length of the pathname 126plus 1. 127.El 128.Sh SEE ALSO 129.Xr chdir 2 , 130.Xr fchdir 2 , 131.Xr malloc 3 , 132.Xr strerror 3 133.Sh STANDARDS 134The 135.Fn getcwd 136function 137conforms to 138.St -ansiC . 139The ability to specify a 140.Dv NULL 141pointer and have 142.Fn getcwd 143allocate memory as necessary is an extension. 144.Sh HISTORY 145The 146.Fn getwd 147function appeared in 148.Bx 4.0 . 149.Sh BUGS 150The 151.Fn getwd 152function 153does not do sufficient error checking and is not able to return very 154long, but valid, paths. 155It is provided for compatibility. 156