]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getcwd.3
dlinfo.3: ATTRIBUTES: Note function that is thread-safe
[thirdparty/man-pages.git] / man3 / getcwd.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
27 .\" Corrected description of getwd().
28 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
29 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
30 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
31 .\"
32 .TH GETCWD 3 2015-04-19 "GNU" "Linux Programmer's Manual"
33 .SH NAME
34 getcwd, getwd, get_current_dir_name \- get current working directory
35 .SH SYNOPSIS
36 .nf
37 .B #include <unistd.h>
38 .sp
39 .BI "char *getcwd(char *" buf ", size_t " size );
40 .sp
41 .BI "char *getwd(char *" buf );
42 .sp
43 .B "char *get_current_dir_name(void);"
44 .fi
45 .sp
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .sp
50 .in
51 .BR get_current_dir_name ():
52 .RS
53 _GNU_SOURCE
54 .RE
55 .sp
56 .BR getwd ():
57 .ad l
58 .RS 4
59 .PD 0
60 .TP 4
61 Since glibc 2.12:
62 .nf
63 _BSD_SOURCE ||
64 (_XOPEN_SOURCE\ >=\ 500 ||
65 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
66 !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
67 .TP 4
68 .fi
69 Before glibc 2.12:
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
71 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
72 .PD
73 .RE
74 .ad b
75 .SH DESCRIPTION
76 These functions return a null-terminated string containing an
77 absolute pathname that is the current working directory of
78 the calling process.
79 The pathname is returned as the function result and via the argument
80 .IR buf ,
81 if present.
82
83 If the current directory is not below the root directory of the current
84 process (e.g., because the process set a new filesystem root using
85 .BR chroot (2)
86 without changing its current directory into the new root),
87 then, since Linux 2.6.36,
88 .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
89 the returned path will be prefixed with the string "(unreachable)".
90 Such behavior can also be caused by an unprivileged user by changing
91 the current directory into another mount namespace.
92 When dealing with paths from untrusted sources, callers of these
93 functions should consider checking whether the returned path starts
94 with '/' or '(' to avoid misinterpreting an unreachable path
95 as a relative path.
96
97 The
98 .BR getcwd ()
99 function copies an absolute pathname of the current working directory
100 to the array pointed to by
101 .IR buf ,
102 which is of length
103 .IR size .
104 .PP
105 If the length of the absolute pathname of the current working directory,
106 including the terminating null byte, exceeds
107 .I size
108 bytes, NULL is returned, and
109 .I errno
110 is set to
111 .BR ERANGE ;
112 an application should check for this error, and allocate a larger
113 buffer if necessary.
114 .PP
115 As an extension to the POSIX.1-2001 standard, glibc's
116 .BR getcwd ()
117 allocates the buffer dynamically using
118 .BR malloc (3)
119 if
120 .I buf
121 is NULL.
122 In this case, the allocated buffer has the length
123 .I size
124 unless
125 .I size
126 is zero, when
127 .I buf
128 is allocated as big as necessary.
129 The caller should
130 .BR free (3)
131 the returned buffer.
132
133 .BR get_current_dir_name ()
134 will
135 .BR malloc (3)
136 an array big enough to hold the absolute pathname of
137 the current working directory.
138 If the environment
139 variable
140 .B PWD
141 is set, and its value is correct, then that value will be returned.
142 The caller should
143 .BR free (3)
144 the returned buffer.
145
146 .BR getwd ()
147 does not
148 .BR malloc (3)
149 any memory.
150 The
151 .I buf
152 argument should be a pointer to an array at least
153 .B PATH_MAX
154 bytes long.
155 If the length of the absolute pathname of the current working directory,
156 including the terminating null byte, exceeds
157 .B PATH_MAX
158 bytes, NULL is returned, and
159 .I errno
160 is set to
161 .BR ENAMETOOLONG .
162 (Note that on some systems,
163 .B PATH_MAX
164 may not be a compile-time constant;
165 furthermore, its value may depend on the filesystem, see
166 .BR pathconf (3).)
167 For portability and security reasons, use of
168 .BR getwd ()
169 is deprecated.
170 .SH RETURN VALUE
171 On success, these functions return a pointer to a string containing
172 the pathname of the current working directory.
173 In the case
174 .BR getcwd ()
175 and
176 .BR getwd ()
177 this is the same value as
178 .IR buf .
179
180 On failure, these functions return NULL, and
181 .I errno
182 is set to indicate the error.
183 The contents of the array pointed to by
184 .I buf
185 are undefined on error.
186 .SH ERRORS
187 .TP
188 .B EACCES
189 Permission to read or search a component of the filename was denied.
190 .TP
191 .B EFAULT
192 .I buf
193 points to a bad address.
194 .TP
195 .B EINVAL
196 The
197 .I size
198 argument is zero and
199 .I buf
200 is not a null pointer.
201 .TP
202 .B EINVAL
203 .BR getwd ():
204 .I buf
205 is NULL.
206 .TP
207 .B ENAMETOOLONG
208 .BR getwd ():
209 The size of the null-terminated absolute pathname string exceeds
210 .B PATH_MAX
211 bytes.
212 .TP
213 .B ENOMEM
214 Out of memory.
215 .TP
216 .B ENOENT
217 The current working directory has been unlinked.
218 .TP
219 .B ERANGE
220 The
221 .I size
222 argument is less than the length of the absolute pathname of the
223 working directory, including the terminating null byte.
224 You need to allocate a bigger array and try again.
225 .SH ATTRIBUTES
226 For an explanation of the terms used in this section, see
227 .BR attributes (7).
228 .TS
229 allbox;
230 lbw22 lb lb
231 l l l.
232 Interface Attribute Value
233 T{
234 .BR getcwd (),
235 .BR getwd ()
236 T} Thread safety MT-Safe
237 T{
238 .BR get_current_dir_name ()
239 T} Thread safety MT-Safe env
240 .TE
241 .SH CONFORMING TO
242 .BR getcwd ()
243 conforms to POSIX.1-2001.
244 Note however that POSIX.1-2001 leaves the behavior of
245 .BR getcwd ()
246 unspecified if
247 .I buf
248 is NULL.
249
250 .BR getwd ()
251 is present in POSIX.1-2001, but marked LEGACY.
252 POSIX.1-2008 removes the specification of
253 .BR getwd ().
254 Use
255 .BR getcwd ()
256 instead.
257 POSIX.1-2001
258 does not define any errors for
259 .BR getwd ().
260
261 .BR get_current_dir_name ()
262 is a GNU extension.
263 .SH NOTES
264 Under Linux, the function
265 .BR getcwd ()
266 is a system call (since 2.1.92).
267 On older systems it would query
268 .IR /proc/self/cwd .
269 If both system call and proc filesystem are missing, a
270 generic implementation is called.
271 Only in that case can
272 these calls fail under Linux with
273 .BR EACCES .
274 .LP
275 These functions are often used to save the location of the current working
276 directory for the purpose of returning to it later.
277 Opening the current
278 directory (".") and calling
279 .BR fchdir (2)
280 to return is usually a faster and more reliable alternative when sufficiently
281 many file descriptors are available, especially on platforms other than Linux.
282 .SH SEE ALSO
283 .BR chdir (2),
284 .BR fchdir (2),
285 .BR open (2),
286 .BR unlink (2),
287 .BR free (3),
288 .BR malloc (3)