mount_setattr.2: Further tweaks after feedback from Christian Brauner
[man-pages.git] / man3 / getcwd.3
blobb256e4dfe2b476af314ecdaf4016f1086836f21c
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 2021-03-22 "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 .PP
39 .BI "char *getcwd(char *" buf ", size_t " size );
40 .BI "char *getwd(char *" buf );
41 .B "char *get_current_dir_name(void);"
42 .fi
43 .PP
44 .RS -4
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .RE
48 .PP
49 .BR get_current_dir_name ():
50 .nf
51     _GNU_SOURCE
52 .fi
53 .PP
54 .BR getwd ():
55 .nf
56     Since glibc 2.12:
57         (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
58             || /* Glibc since 2.19: */ _DEFAULT_SOURCE
59             || /* Glibc <= 2.19: */ _BSD_SOURCE
60     Before glibc 2.12:
61         _BSD_SOURCE || _XOPEN_SOURCE >= 500
62 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
63 .fi
64 .SH DESCRIPTION
65 These functions return a null-terminated string containing an
66 absolute pathname that is the current working directory of
67 the calling process.
68 The pathname is returned as the function result and via the argument
69 .IR buf ,
70 if present.
71 .PP
72 The
73 .BR getcwd ()
74 function copies an absolute pathname of the current working directory
75 to the array pointed to by
76 .IR buf ,
77 which is of length
78 .IR size .
79 .PP
80 If the length of the absolute pathname of the current working directory,
81 including the terminating null byte, exceeds
82 .I size
83 bytes, NULL is returned, and
84 .I errno
85 is set to
86 .BR ERANGE ;
87 an application should check for this error, and allocate a larger
88 buffer if necessary.
89 .PP
90 As an extension to the POSIX.1-2001 standard, glibc's
91 .BR getcwd ()
92 allocates the buffer dynamically using
93 .BR malloc (3)
95 .I buf
96 is NULL.
97 In this case, the allocated buffer has the length
98 .I size
99 unless
100 .I size
101 is zero, when
102 .I buf
103 is allocated as big as necessary.
104 The caller should
105 .BR free (3)
106 the returned buffer.
108 .BR get_current_dir_name ()
109 will
110 .BR malloc (3)
111 an array big enough to hold the absolute pathname of
112 the current working directory.
113 If the environment
114 variable
115 .B PWD
116 is set, and its value is correct, then that value will be returned.
117 The caller should
118 .BR free (3)
119 the returned buffer.
121 .BR getwd ()
122 does not
123 .BR malloc (3)
124 any memory.
126 .I buf
127 argument should be a pointer to an array at least
128 .B PATH_MAX
129 bytes long.
130 If the length of the absolute pathname of the current working directory,
131 including the terminating null byte, exceeds
132 .B PATH_MAX
133 bytes, NULL is returned, and
134 .I errno
135 is set to
136 .BR ENAMETOOLONG .
137 (Note that on some systems,
138 .B PATH_MAX
139 may not be a compile-time constant;
140 furthermore, its value may depend on the filesystem, see
141 .BR pathconf (3).)
142 For portability and security reasons, use of
143 .BR getwd ()
144 is deprecated.
145 .SH RETURN VALUE
146 On success, these functions return a pointer to a string containing
147 the pathname of the current working directory.
148 In the case of
149 .BR getcwd ()
151 .BR getwd ()
152 this is the same value as
153 .IR buf .
155 On failure, these functions return NULL, and
156 .I errno
157 is set to indicate the error.
158 The contents of the array pointed to by
159 .I buf
160 are undefined on error.
161 .SH ERRORS
163 .B EACCES
164 Permission to read or search a component of the filename was denied.
166 .B EFAULT
167 .I buf
168 points to a bad address.
170 .B EINVAL
172 .I size
173 argument is zero and
174 .I buf
175 is not a null pointer.
177 .B EINVAL
178 .BR getwd ():
179 .I buf
180 is NULL.
182 .B ENAMETOOLONG
183 .BR getwd ():
184 The size of the null-terminated absolute pathname string exceeds
185 .B PATH_MAX
186 bytes.
188 .B ENOENT
189 The current working directory has been unlinked.
191 .B ENOMEM
192 Out of memory.
194 .B ERANGE
196 .I size
197 argument is less than the length of the absolute pathname of the
198 working directory, including the terminating null byte.
199 You need to allocate a bigger array and try again.
200 .SH ATTRIBUTES
201 For an explanation of the terms used in this section, see
202 .BR attributes (7).
203 .ad l
206 allbox;
207 lbx lb lb
208 l l l.
209 Interface       Attribute       Value
211 .BR getcwd (),
212 .BR getwd ()
213 T}      Thread safety   MT-Safe
215 .BR get_current_dir_name ()
216 T}      Thread safety   MT-Safe env
220 .sp 1
221 .SH CONFORMING TO
222 .BR getcwd ()
223 conforms to POSIX.1-2001.
224 Note however that POSIX.1-2001 leaves the behavior of
225 .BR getcwd ()
226 unspecified if
227 .I buf
228 is NULL.
230 .BR getwd ()
231 is present in POSIX.1-2001, but marked LEGACY.
232 POSIX.1-2008 removes the specification of
233 .BR getwd ().
235 .BR getcwd ()
236 instead.
237 POSIX.1-2001
238 does not define any errors for
239 .BR getwd ().
241 .BR get_current_dir_name ()
242 is a GNU extension.
243 .SH NOTES
244 Under Linux, these functions make use of the
245 .BR getcwd ()
246 system call (available since Linux 2.1.92).
247 On older systems they would query
248 .IR /proc/self/cwd .
249 If both system call and proc filesystem are missing, a
250 generic implementation is called.
251 Only in that case can
252 these calls fail under Linux with
253 .BR EACCES .
255 These functions are often used to save the location of the current working
256 directory for the purpose of returning to it later.
257 Opening the current
258 directory (".") and calling
259 .BR fchdir (2)
260 to return is usually a faster and more reliable alternative when sufficiently
261 many file descriptors are available, especially on platforms other than Linux.
263 .SS C library/kernel differences
264 On Linux, the kernel provides a
265 .BR getcwd ()
266 system call, which the functions described in this page will use if possible.
267 The system call takes the same arguments as the library function
268 of the same name, but is limited to returning at most
269 .BR PATH_MAX
270 bytes.
271 (Before Linux 3.12,
272 .\" commit 3272c544da48f8915a0e34189182aed029bd0f2b
273 the limit on the size of the returned pathname was the system page size.
274 On many architectures,
275 .BR PATH_MAX
276 and the system page size are both 4096 bytes,
277 but a few architectures have a larger page size.)
278 If the length of the pathname of the current working directory
279 exceeds this limit, then the system call fails with the error
280 .BR ENAMETOOLONG .
281 In this case, the library functions fall back to
282 a (slower) alternative implementation that returns the full pathname.
284 Following a change in Linux 2.6.36,
285 .\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
286 the pathname returned by the
287 .BR getcwd ()
288 system call will be prefixed with the string "(unreachable)"
289 if the current directory is not below the root directory of the current
290 process (e.g., because the process set a new filesystem root using
291 .BR chroot (2)
292 without changing its current directory into the new root).
293 Such behavior can also be caused by an unprivileged user by changing
294 the current directory into another mount namespace.
295 When dealing with pathname from untrusted sources, callers of the
296 functions described in this page
297 should consider checking whether the returned pathname starts
298 with '/' or '(' to avoid misinterpreting an unreachable path
299 as a relative pathname.
300 .SH BUGS
301 Since the Linux 2.6.36 change that added "(unreachable)" in the
302 circumstances described above, the glibc implementation of
303 .BR getcwd ()
304 has failed to conform to POSIX and returned a relative pathname when the API
305 contract requires an absolute pathname.
306 With glibc 2.27 onwards this is corrected;
307 calling
308 .BR getcwd ()
309 from such a pathname will now result in failure with
310 .BR ENOENT .
311 .SH SEE ALSO
312 .BR pwd (1),
313 .BR chdir (2),
314 .BR fchdir (2),
315 .BR open (2),
316 .BR unlink (2),
317 .BR free (3),
318 .BR malloc (3)