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