xref: /csrg-svn/lib/libc/gen/getcwd.3 (revision 65099)
161111Sbostic.\" Copyright (c) 1991, 1993
261111Sbostic.\"	The Regents of the University of California.  All rights reserved.
320532Smckusick.\"
446483Sbostic.\" %sccs.include.redist.man%
520532Smckusick.\"
6*65099Smckusick.\"     @(#)getcwd.3	8.2 (Berkeley) 12/11/93
746483Sbostic.\"
848352Scael.Dd
948352Scael.Dt GETCWD 3
1048352Scael.Os BSD 4.2
1148352Scael.Sh NAME
12*65099Smckusick.Nm getcwd
1348352Scael.Nd get working directory pathname
1448352Scael.Sh SYNOPSIS
1548352Scael.Fd #include <stdio.h>
1648352Scael.Ft char *
1748352Scael.Fn getcwd "char *buf" "size_t size"
1848352Scael.Ft char *
1948352Scael.Fn getwd "char *buf"
2048352Scael.Sh DESCRIPTION
2146483SbosticThe
2248352Scael.Fn getcwd
2346483Sbosticfunction copies the absolute pathname of the current working directory
2446483Sbosticinto the memory referenced by
2548352Scael.Fa buf
2646483Sbosticand returns a pointer to
2748352Scael.Fa buf .
2846483SbosticThe
2948352Scael.Fa size
3046483Sbosticargument is the size, in bytes, of the array referenced by
3148352Scael.Fa buf .
3248352Scael.Pp
3346483SbosticIf
3448352Scael.Fa buf
3548352Scaelis
3648352Scael.Dv NULL ,
3748352Scaelspace is allocated as necessary to store the pathname.
3846483SbosticThis space may later be
3948352Scael.Xr free 3 Ns 'd.
4048352Scael.Pp
4146483SbosticThe function
4248352Scael.Fn getwd
4346483Sbosticis a compatibility routine which calls
4448352Scael.Fn getcwd
4546483Sbosticwith its
4648352Scael.Fa buf
4748352Scaelargument and a size of
4848352Scael.Dv MAXPATHLEN
4948352Scael(as defined in the include
5048352Scaelfile
5148352Scael.Aq Pa sys/param.h ) .
5246483SbosticObviously,
5348352Scael.Fa buf
5448352Scaelshould be at least
5548352Scael.Dv MAXPATHLEN
5648352Scaelbytes in length.
5748352Scael.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
6148352Scaelopen the current directory
6248352Scael.Pq Ql \&.
6348352Scaeland use the
6448352Scael.Xr fchdir 2
6546483Sbosticfunction to return.
6648352Scael.Sh RETURN VALUES
6746483SbosticUpon successful completion, a pointer to the pathname is returned.
6848352ScaelOtherwise a
6948352Scael.Dv NULL
7048352Scaelpointer is returned and the global variable
7148352Scael.Va errno
7246483Sbosticis set to indicate the error.
7346483SbosticIn addition,
7448352Scael.Fn getwd
7546483Sbosticcopies the error message associated with
7648352Scael.Va errno
7746483Sbosticinto the memory referenced by
7848352Scael.Fa buf .
7948352Scael.Sh ERRORS
8048352ScaelThe
8148352Scael.Fn getcwd
8248352Scaelfunction
8346483Sbosticwill fail if:
8448352Scael.Bl -tag -width [EACCESS]
8548352Scael.It Bq Er EACCESS
8646483SbosticRead or search permission was denied for a component of the pathname.
8748352Scael.It Bq Er EINVAL
8846483SbosticThe
8948352Scael.Fa size
9046483Sbosticargument is zero.
9148352Scael.It Bq Er ENOENT
9246483SbosticA component of the pathname no longer exists.
9348352Scael.It Bq Er ENOMEM
9446483SbosticInsufficient memory is available.
9548352Scael.It Bq Er ERANGE
9646483SbosticThe
9748352Scael.Fa size
9846483Sbosticargument is greater than zero but smaller than the length of the pathname
9946483Sbosticplus 1.
10048352Scael.El
10148352Scael.Sh SEE ALSO
10248352Scael.Xr chdir 2 ,
10348352Scael.Xr fchdir 2 ,
10448352Scael.Xr malloc 3 ,
10548352Scael.Xr strerror 3
10648352Scael.Sh STANDARDS
10748352ScaelThe
10848352Scael.Fn getcwd
10948352Scaelfunction
11048352Scaelconforms to
11148352Scael.St -ansiC .
11248352ScaelThe ability to specify a
11348352Scael.Dv NULL
11448352Scaelpointer and have
11548352Scael.Fn getcwd
11648352Scaelallocate memory as necessary is an extension.
11748352Scael.Sh HISTORY
11848352ScaelThe
11948352Scael.Fn getwd
12048352Scaelfunction appeared in
12148352Scael.Bx 4.0 .
12248352Scael.Sh BUGS
12348352ScaelThe
12448352Scael.Fn getwd
12548352Scaelfunction
12646483Sbosticdoes not do sufficient error checking and is not able to return very
12746483Sbosticlong, but valid, paths.
12846483SbosticIt is provided for compatibility.
129