146483Sbostic.\" Copyright (c) 1991 The Regents of the University of California. 246483Sbostic.\" All rights reserved. 320532Smckusick.\" 446483Sbostic.\" %sccs.include.redist.man% 520532Smckusick.\" 6*48352Scael.\" @(#)getcwd.3 6.4 (Berkeley) 04/19/91 746483Sbostic.\" 8*48352Scael.Dd 9*48352Scael.Dt GETCWD 3 10*48352Scael.Os BSD 4.2 11*48352Scael.Sh NAME 12*48352Scael.Nm getwd 13*48352Scael.Nd get working directory pathname 14*48352Scael.Sh SYNOPSIS 15*48352Scael.Fd #include <stdio.h> 16*48352Scael.Ft char * 17*48352Scael.Fn getcwd "char *buf" "size_t size" 18*48352Scael.Ft char * 19*48352Scael.Fn getwd "char *buf" 20*48352Scael.Sh DESCRIPTION 2146483SbosticThe 22*48352Scael.Fn getcwd 2346483Sbosticfunction copies the absolute pathname of the current working directory 2446483Sbosticinto the memory referenced by 25*48352Scael.Fa buf 2646483Sbosticand returns a pointer to 27*48352Scael.Fa buf . 2846483SbosticThe 29*48352Scael.Fa size 3046483Sbosticargument is the size, in bytes, of the array referenced by 31*48352Scael.Fa buf . 32*48352Scael.Pp 3346483SbosticIf 34*48352Scael.Fa buf 35*48352Scaelis 36*48352Scael.Dv NULL , 37*48352Scaelspace is allocated as necessary to store the pathname. 3846483SbosticThis space may later be 39*48352Scael.Xr free 3 Ns 'd. 40*48352Scael.Pp 4146483SbosticThe function 42*48352Scael.Fn getwd 4346483Sbosticis a compatibility routine which calls 44*48352Scael.Fn getcwd 4546483Sbosticwith its 46*48352Scael.Fa buf 47*48352Scaelargument and a size of 48*48352Scael.Dv MAXPATHLEN 49*48352Scael(as defined in the include 50*48352Scaelfile 51*48352Scael.Aq Pa sys/param.h ) . 5246483SbosticObviously, 53*48352Scael.Fa buf 54*48352Scaelshould be at least 55*48352Scael.Dv MAXPATHLEN 56*48352Scaelbytes in length. 57*48352Scael.Pp 5846483SbosticThese routines have traditionally been used by programs to save the 5946483Sbosticname of a working directory for the purpose of returning to it. 6046483SbosticA much faster and less error-prone method of accomplishing this is to 61*48352Scaelopen the current directory 62*48352Scael.Pq Ql \&. 63*48352Scaeland use the 64*48352Scael.Xr fchdir 2 6546483Sbosticfunction to return. 66*48352Scael.Sh RETURN VALUES 6746483SbosticUpon successful completion, a pointer to the pathname is returned. 68*48352ScaelOtherwise a 69*48352Scael.Dv NULL 70*48352Scaelpointer is returned and the global variable 71*48352Scael.Va errno 7246483Sbosticis set to indicate the error. 7346483SbosticIn addition, 74*48352Scael.Fn getwd 7546483Sbosticcopies the error message associated with 76*48352Scael.Va errno 7746483Sbosticinto the memory referenced by 78*48352Scael.Fa buf . 79*48352Scael.Sh ERRORS 80*48352ScaelThe 81*48352Scael.Fn getcwd 82*48352Scaelfunction 8346483Sbosticwill fail if: 84*48352Scael.Bl -tag -width [EACCESS] 85*48352Scael.It Bq Er EACCESS 8646483SbosticRead or search permission was denied for a component of the pathname. 87*48352Scael.It Bq Er EINVAL 8846483SbosticThe 89*48352Scael.Fa size 9046483Sbosticargument is zero. 91*48352Scael.It Bq Er ENOENT 9246483SbosticA component of the pathname no longer exists. 93*48352Scael.It Bq Er ENOMEM 9446483SbosticInsufficient memory is available. 95*48352Scael.It Bq Er ERANGE 9646483SbosticThe 97*48352Scael.Fa size 9846483Sbosticargument is greater than zero but smaller than the length of the pathname 9946483Sbosticplus 1. 100*48352Scael.El 101*48352Scael.Sh SEE ALSO 102*48352Scael.Xr chdir 2 , 103*48352Scael.Xr fchdir 2 , 104*48352Scael.Xr malloc 3 , 105*48352Scael.Xr strerror 3 106*48352Scael.Sh STANDARDS 107*48352ScaelThe 108*48352Scael.Fn getcwd 109*48352Scaelfunction 110*48352Scaelconforms to 111*48352Scael.St -ansiC . 112*48352ScaelThe ability to specify a 113*48352Scael.Dv NULL 114*48352Scaelpointer and have 115*48352Scael.Fn getcwd 116*48352Scaelallocate memory as necessary is an extension. 117*48352Scael.Sh HISTORY 118*48352ScaelThe 119*48352Scael.Fn getwd 120*48352Scaelfunction appeared in 121*48352Scael.Bx 4.0 . 122*48352Scael.Sh BUGS 123*48352ScaelThe 124*48352Scael.Fn getwd 125*48352Scaelfunction 12646483Sbosticdoes not do sufficient error checking and is not able to return very 12746483Sbosticlong, but valid, paths. 12846483SbosticIt is provided for compatibility. 129