1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
6 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
7 .\" Corrected description of getwd().
8 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
9 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
10 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
12 .TH GETCWD 3 2021-03-22 "GNU" "Linux Programmer's Manual"
14 getcwd, getwd, get_current_dir_name \- get current working directory
17 .RI ( libc ", " \-lc )
20 .B #include <unistd.h>
22 .BI "char *getcwd(char *" buf ", size_t " size );
23 .BI "char *getwd(char *" buf );
24 .B "char *get_current_dir_name(void);"
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
32 .BR get_current_dir_name ():
40 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
41 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
42 || /* Glibc <= 2.19: */ _BSD_SOURCE
44 _BSD_SOURCE || _XOPEN_SOURCE >= 500
45 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
48 These functions return a null-terminated string containing an
49 absolute pathname that is the current working directory of
51 The pathname is returned as the function result and via the argument
57 function copies an absolute pathname of the current working directory
58 to the array pointed to by
63 If the length of the absolute pathname of the current working directory,
64 including the terminating null byte, exceeds
66 bytes, NULL is returned, and
70 an application should check for this error, and allocate a larger
73 As an extension to the POSIX.1-2001 standard, glibc's
75 allocates the buffer dynamically using
80 In this case, the allocated buffer has the length
86 is allocated as big as necessary.
91 .BR get_current_dir_name ()
94 an array big enough to hold the absolute pathname of
95 the current working directory.
99 is set, and its value is correct, then that value will be returned.
110 argument should be a pointer to an array at least
113 If the length of the absolute pathname of the current working directory,
114 including the terminating null byte, exceeds
116 bytes, NULL is returned, and
120 (Note that on some systems,
122 may not be a compile-time constant;
123 furthermore, its value may depend on the filesystem, see
125 For portability and security reasons, use of
129 On success, these functions return a pointer to a string containing
130 the pathname of the current working directory.
135 this is the same value as
138 On failure, these functions return NULL, and
140 is set to indicate the error.
141 The contents of the array pointed to by
143 are undefined on error.
147 Permission to read or search a component of the filename was denied.
151 points to a bad address.
158 is not a null pointer.
167 The size of the null-terminated absolute pathname string exceeds
172 The current working directory has been unlinked.
180 argument is less than the length of the absolute pathname of the
181 working directory, including the terminating null byte.
182 You need to allocate a bigger array and try again.
184 For an explanation of the terms used in this section, see
192 Interface Attribute Value
196 T} Thread safety MT-Safe
198 .BR get_current_dir_name ()
199 T} Thread safety MT-Safe env
206 conforms to POSIX.1-2001.
207 Note however that POSIX.1-2001 leaves the behavior of
214 is present in POSIX.1-2001, but marked LEGACY.
215 POSIX.1-2008 removes the specification of
221 does not define any errors for
224 .BR get_current_dir_name ()
227 Under Linux, these functions make use of the
229 system call (available since Linux 2.1.92).
230 On older systems they would query
232 If both system call and proc filesystem are missing, a
233 generic implementation is called.
234 Only in that case can
235 these calls fail under Linux with
238 These functions are often used to save the location of the current working
239 directory for the purpose of returning to it later.
241 directory (".") and calling
243 to return is usually a faster and more reliable alternative when sufficiently
244 many file descriptors are available, especially on platforms other than Linux.
246 .SS C library/kernel differences
247 On Linux, the kernel provides a
249 system call, which the functions described in this page will use if possible.
250 The system call takes the same arguments as the library function
251 of the same name, but is limited to returning at most
255 .\" commit 3272c544da48f8915a0e34189182aed029bd0f2b
256 the limit on the size of the returned pathname was the system page size.
257 On many architectures,
259 and the system page size are both 4096 bytes,
260 but a few architectures have a larger page size.)
261 If the length of the pathname of the current working directory
262 exceeds this limit, then the system call fails with the error
264 In this case, the library functions fall back to
265 a (slower) alternative implementation that returns the full pathname.
267 Following a change in Linux 2.6.36,
268 .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
269 the pathname returned by the
271 system call will be prefixed with the string "(unreachable)"
272 if the current directory is not below the root directory of the current
273 process (e.g., because the process set a new filesystem root using
275 without changing its current directory into the new root).
276 Such behavior can also be caused by an unprivileged user by changing
277 the current directory into another mount namespace.
278 When dealing with pathname from untrusted sources, callers of the
279 functions described in this page
280 should consider checking whether the returned pathname starts
281 with '/' or '(' to avoid misinterpreting an unreachable path
282 as a relative pathname.
284 Since the Linux 2.6.36 change that added "(unreachable)" in the
285 circumstances described above, the glibc implementation of
287 has failed to conform to POSIX and returned a relative pathname when the API
288 contract requires an absolute pathname.
289 With glibc 2.27 onwards this is corrected;
292 from such a pathname will now result in failure with