2 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
7 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
8 .\" Corrected description of getwd().
9 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
10 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
11 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
13 .TH getcwd 3 (date) "Linux man-pages (unreleased)"
15 getcwd, getwd, get_current_dir_name \- get current working directory
18 .RI ( libc ", " \-lc )
21 .B #include <unistd.h>
23 .BI "char *getcwd(char " buf [. size "], size_t " size );
24 .B "char *get_current_dir_name(void);"
26 .BI "[[deprecated]] char *getwd(char " buf [PATH_MAX]);
30 Feature Test Macro Requirements for glibc (see
31 .BR feature_test_macros (7)):
34 .BR get_current_dir_name ():
42 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
43 || /* glibc >= 2.19: */ _DEFAULT_SOURCE
44 || /* glibc <= 2.19: */ _BSD_SOURCE
46 _BSD_SOURCE || _XOPEN_SOURCE >= 500
47 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
50 These functions return a null-terminated string containing an
51 absolute pathname that is the current working directory of
53 The pathname is returned as the function result and via the argument
59 function copies an absolute pathname of the current working directory
60 to the array pointed to by
65 If the length of the absolute pathname of the current working directory,
66 including the terminating null byte, exceeds
68 bytes, NULL is returned, and
72 an application should check for this error, and allocate a larger
75 As an extension to the POSIX.1-2001 standard, glibc's
77 allocates the buffer dynamically using
82 In this case, the allocated buffer has the length
88 is allocated as big as necessary.
93 .BR get_current_dir_name ()
96 an array big enough to hold the absolute pathname of
97 the current working directory.
101 is set, and its value is correct, then that value will be returned.
112 argument should be a pointer to an array at least
115 If the length of the absolute pathname of the current working directory,
116 including the terminating null byte, exceeds
118 bytes, NULL is returned, and
122 (Note that on some systems,
124 may not be a compile-time constant;
125 furthermore, its value may depend on the filesystem, see
127 For portability and security reasons, use of
131 On success, these functions return a pointer to a string containing
132 the pathname of the current working directory.
137 this is the same value as
140 On failure, these functions return NULL, and
142 is set to indicate the error.
143 The contents of the array pointed to by
145 are undefined on error.
149 Permission to read or search a component of the filename was denied.
153 points to a bad address.
160 is not a null pointer.
169 The size of the null-terminated absolute pathname string exceeds
174 The current working directory has been unlinked.
182 argument is less than the length of the absolute pathname of the
183 working directory, including the terminating null byte.
184 You need to allocate a bigger array and try again.
186 For an explanation of the terms used in this section, see
192 Interface Attribute Value
198 T} Thread safety MT-Safe
202 .BR get_current_dir_name ()
203 T} Thread safety MT-Safe env
206 POSIX.1-2001 leaves the behavior of
213 does not define any errors for
216 .SS C library/kernel differences
217 On Linux, the kernel provides a
219 system call, which the functions described in this page will use if possible.
220 The system call takes the same arguments as the library function
221 of the same name, but is limited to returning at most
225 .\" commit 3272c544da48f8915a0e34189182aed029bd0f2b
226 the limit on the size of the returned pathname was the system page size.
227 On many architectures,
229 and the system page size are both 4096 bytes,
230 but a few architectures have a larger page size.)
231 If the length of the pathname of the current working directory
232 exceeds this limit, then the system call fails with the error
234 In this case, the library functions fall back to
235 a (slower) alternative implementation that returns the full pathname.
237 Following a change in Linux 2.6.36,
238 .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
239 the pathname returned by the
241 system call will be prefixed with the string "(unreachable)"
242 if the current directory is not below the root directory of the current
243 process (e.g., because the process set a new filesystem root using
245 without changing its current directory into the new root).
246 Such behavior can also be caused by an unprivileged user by changing
247 the current directory into another mount namespace.
248 When dealing with pathname from untrusted sources, callers of the
249 functions described in this page
250 should consider checking whether the returned pathname starts
251 with '/' or '(' to avoid misinterpreting an unreachable path
252 as a relative pathname.
258 .BR get_current_dir_name ()
269 POSIX.1-2001, but marked LEGACY.
270 Removed in POSIX.1-2008.
275 Under Linux, these functions make use of the
277 system call (available since Linux 2.1.92).
278 On older systems they would query
280 If both system call and proc filesystem are missing, a
281 generic implementation is called.
282 Only in that case can
283 these calls fail under Linux with
286 These functions are often used to save the location of the current working
287 directory for the purpose of returning to it later.
289 directory (".") and calling
291 to return is usually a faster and more reliable alternative when sufficiently
292 many file descriptors are available, especially on platforms other than Linux.
294 Since the Linux 2.6.36 change that added "(unreachable)" in the
295 circumstances described above, the glibc implementation of
297 has failed to conform to POSIX and returned a relative pathname when the API
298 contract requires an absolute pathname.
299 With glibc 2.27 onwards this is corrected;
302 from such a pathname will now result in failure with