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