1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
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>
32 .TH GETCWD 3 2017-09-15 "GNU" "Linux Programmer's Manual"
34 getcwd, getwd, get_current_dir_name \- get current working directory
37 .B #include <unistd.h>
39 .BI "char *getcwd(char *" buf ", size_t " size );
41 .BI "char *getwd(char *" buf );
43 .B "char *get_current_dir_name(void);"
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
51 .BR get_current_dir_name ():
63 (_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L)
64 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
65 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
69 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
70 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 These functions return a null-terminated string containing an
76 absolute pathname that is the current working directory of
78 The pathname is returned as the function result and via the argument
82 If the current directory is not below the root directory of the current
83 process (e.g., because the process set a new filesystem root using
85 without changing its current directory into the new root),
86 then, since Linux 2.6.36,
87 .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
88 the returned path will be prefixed with the string "(unreachable)".
89 Such behavior can also be caused by an unprivileged user by changing
90 the current directory into another mount namespace.
91 When dealing with paths from untrusted sources, callers of these
92 functions should consider checking whether the returned path starts
93 with '/' or '(' to avoid misinterpreting an unreachable path
95 This is no longer true under some C libraries; see
100 function copies an absolute pathname of the current working directory
101 to the array pointed to by
106 If the length of the absolute pathname of the current working directory,
107 including the terminating null byte, exceeds
109 bytes, NULL is returned, and
113 an application should check for this error, and allocate a larger
116 As an extension to the POSIX.1-2001 standard, glibc's
118 allocates the buffer dynamically using
123 In this case, the allocated buffer has the length
129 is allocated as big as necessary.
134 .BR get_current_dir_name ()
137 an array big enough to hold the absolute pathname of
138 the current working directory.
142 is set, and its value is correct, then that value will be returned.
153 argument should be a pointer to an array at least
156 If the length of the absolute pathname of the current working directory,
157 including the terminating null byte, exceeds
159 bytes, NULL is returned, and
163 (Note that on some systems,
165 may not be a compile-time constant;
166 furthermore, its value may depend on the filesystem, see
168 For portability and security reasons, use of
172 On success, these functions return a pointer to a string containing
173 the pathname of the current working directory.
178 this is the same value as
181 On failure, these functions return NULL, and
183 is set to indicate the error.
184 The contents of the array pointed to by
186 are undefined on error.
190 Permission to read or search a component of the filename was denied.
194 points to a bad address.
201 is not a null pointer.
210 The size of the null-terminated absolute pathname string exceeds
215 The current working directory has been unlinked.
223 argument is less than the length of the absolute pathname of the
224 working directory, including the terminating null byte.
225 You need to allocate a bigger array and try again.
227 For an explanation of the terms used in this section, see
233 Interface Attribute Value
237 T} Thread safety MT-Safe
239 .BR get_current_dir_name ()
240 T} Thread safety MT-Safe env
244 conforms to POSIX.1-2001.
245 Note however that POSIX.1-2001 leaves the behavior of
252 is present in POSIX.1-2001, but marked LEGACY.
253 POSIX.1-2008 removes the specification of
259 does not define any errors for
262 .BR get_current_dir_name ()
265 Under Linux, the function
267 is a system call (since 2.1.92).
268 On older systems it would query
270 If both system call and proc filesystem are missing, a
271 generic implementation is called.
272 Only in that case can
273 these calls fail under Linux with
276 Since a Linux 2.6.36 change that added "(unreachable)", the glibc
278 has failed to conform to POSIX and returned a relative path when the API
279 contract requires an absolute path.
280 With glibc 2.27 onwards this is corrected;
283 from such a path will now result in failure 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.