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.Nd get working directory pathname 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Ft char * 43.Fn getcwd "char *buf" "size_t size" 44.Ft char * 45.Fn getwd "char *buf" 46.Sh DESCRIPTION 47The 48.Fn getcwd 49function copies the absolute pathname of the current working directory 50into the memory referenced by 51.Fa buf 52and returns a pointer to 53.Fa buf . 54The 55.Fa size 56argument is the size, in bytes, of the array referenced by 57.Fa buf . 58.Pp 59If 60.Fa buf 61is 62.Dv NULL , 63space is allocated as necessary to store the pathname. 64This space may later be 65.Xr free 3 Ns 'd. 66.Pp 67The function 68.Fn getwd 69is a compatibility routine which calls 70.Fn getcwd 71with its 72.Fa buf 73argument and a size of 74.Dv MAXPATHLEN 75(as defined in the include 76file 77.Aq Pa sys/param.h ) . 78Obviously, 79.Fa buf 80should be at least 81.Dv MAXPATHLEN 82bytes in length. 83.Pp 84These routines have traditionally been used by programs to save the 85name of a working directory for the purpose of returning to it. 86A much faster and less error-prone method of accomplishing this is to 87open the current directory 88.Pq Ql \&. 89and use the 90.Xr fchdir 2 91function to return. 92.Sh RETURN VALUES 93Upon successful completion, a pointer to the pathname is returned. 94Otherwise a 95.Dv NULL 96pointer is returned and the global variable 97.Va errno 98is set to indicate the error. 99In addition, 100.Fn getwd 101copies the error message associated with 102.Va errno 103into the memory referenced by 104.Fa buf . 105.Sh ERRORS 106The 107.Fn getcwd 108function 109will fail if: 110.Bl -tag -width [EACCESS] 111.It Bq Er EACCESS 112Read or search permission was denied for a component of the pathname. 113.It Bq Er EINVAL 114The 115.Fa size 116argument is zero. 117.It Bq Er ENOENT 118A component of the pathname no longer exists. 119.It Bq Er ENOMEM 120Insufficient memory is available. 121.It Bq Er ERANGE 122The 123.Fa size 124argument is greater than zero but smaller than the length of the pathname 125plus 1. 126.El 127.Sh SEE ALSO 128.Xr chdir 2 , 129.Xr fchdir 2 , 130.Xr malloc 3 , 131.Xr strerror 3 132.Sh STANDARDS 133The 134.Fn getcwd 135function 136conforms to 137.St -ansiC . 138The ability to specify a 139.Dv NULL 140pointer and have 141.Fn getcwd 142allocate memory as necessary is an extension. 143.Sh HISTORY 144The 145.Fn getwd 146function appeared in 147.Bx 4.0 . 148.Sh BUGS 149The 150.Fn getwd 151function 152does not do sufficient error checking and is not able to return very 153long, but valid, paths. 154It is provided for compatibility. 155