xref: /dragonfly/lib/libc/gen/getcwd.3 (revision 9348a738)
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.\" $FreeBSD: src/lib/libc/gen/getcwd.3,v 1.7.2.6 2001/12/14 18:33:51 ru Exp $
30.\" $DragonFly: src/lib/libc/gen/getcwd.3,v 1.3 2006/05/26 19:39:36 swildner Exp $
31.\"
32.Dd November 24, 1997
33.Dt GETCWD 3
34.Os
35.Sh NAME
36.Nm getcwd ,
37.Nm getwd
38.Nd get working directory pathname
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In 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.In 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 Er
112.It Bq Er EACCES
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 -p1003.1-90 .
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