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