]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getcwd.3
encrypt.3: srcfix: rewrap source lines
[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 2017-09-15 "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 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
84 .BR chroot (2)
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
94 as a relative path.
95 This is no longer true under some C libraries; see
96 .BR NOTES .
97 .PP
98 The
99 .BR getcwd ()
100 function copies an absolute pathname of the current working directory
101 to the array pointed to by
102 .IR buf ,
103 which is of length
104 .IR size .
105 .PP
106 If the length of the absolute pathname of the current working directory,
107 including the terminating null byte, exceeds
108 .I size
109 bytes, NULL is returned, and
110 .I errno
111 is set to
112 .BR ERANGE ;
113 an application should check for this error, and allocate a larger
114 buffer if necessary.
115 .PP
116 As an extension to the POSIX.1-2001 standard, glibc's
117 .BR getcwd ()
118 allocates the buffer dynamically using
119 .BR malloc (3)
120 if
121 .I buf
122 is NULL.
123 In this case, the allocated buffer has the length
124 .I size
125 unless
126 .I size
127 is zero, when
128 .I buf
129 is allocated as big as necessary.
130 The caller should
131 .BR free (3)
132 the returned buffer.
133 .PP
134 .BR get_current_dir_name ()
135 will
136 .BR malloc (3)
137 an array big enough to hold the absolute pathname of
138 the current working directory.
139 If the environment
140 variable
141 .B PWD
142 is set, and its value is correct, then that value will be returned.
143 The caller should
144 .BR free (3)
145 the returned buffer.
146 .PP
147 .BR getwd ()
148 does not
149 .BR malloc (3)
150 any memory.
151 The
152 .I buf
153 argument should be a pointer to an array at least
154 .B PATH_MAX
155 bytes long.
156 If the length of the absolute pathname of the current working directory,
157 including the terminating null byte, exceeds
158 .B PATH_MAX
159 bytes, NULL is returned, and
160 .I errno
161 is set to
162 .BR ENAMETOOLONG .
163 (Note that on some systems,
164 .B PATH_MAX
165 may not be a compile-time constant;
166 furthermore, its value may depend on the filesystem, see
167 .BR pathconf (3).)
168 For portability and security reasons, use of
169 .BR getwd ()
170 is deprecated.
171 .SH RETURN VALUE
172 On success, these functions return a pointer to a string containing
173 the pathname of the current working directory.
174 In the case
175 .BR getcwd ()
176 and
177 .BR getwd ()
178 this is the same value as
179 .IR buf .
180 .PP
181 On failure, these functions return NULL, and
182 .I errno
183 is set to indicate the error.
184 The contents of the array pointed to by
185 .I buf
186 are undefined on error.
187 .SH ERRORS
188 .TP
189 .B EACCES
190 Permission to read or search a component of the filename was denied.
191 .TP
192 .B EFAULT
193 .I buf
194 points to a bad address.
195 .TP
196 .B EINVAL
197 The
198 .I size
199 argument is zero and
200 .I buf
201 is not a null pointer.
202 .TP
203 .B EINVAL
204 .BR getwd ():
205 .I buf
206 is NULL.
207 .TP
208 .B ENAMETOOLONG
209 .BR getwd ():
210 The size of the null-terminated absolute pathname string exceeds
211 .B PATH_MAX
212 bytes.
213 .TP
214 .B ENOENT
215 The current working directory has been unlinked.
216 .TP
217 .B ENOMEM
218 Out of memory.
219 .TP
220 .B ERANGE
221 The
222 .I size
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.
226 .SH ATTRIBUTES
227 For an explanation of the terms used in this section, see
228 .BR attributes (7).
229 .TS
230 allbox;
231 lbw22 lb lb
232 l l l.
233 Interface Attribute Value
234 T{
235 .BR getcwd (),
236 .BR getwd ()
237 T} Thread safety MT-Safe
238 T{
239 .BR get_current_dir_name ()
240 T} Thread safety MT-Safe env
241 .TE
242 .SH CONFORMING TO
243 .BR getcwd ()
244 conforms to POSIX.1-2001.
245 Note however that POSIX.1-2001 leaves the behavior of
246 .BR getcwd ()
247 unspecified if
248 .I buf
249 is NULL.
250 .PP
251 .BR getwd ()
252 is present in POSIX.1-2001, but marked LEGACY.
253 POSIX.1-2008 removes the specification of
254 .BR getwd ().
255 Use
256 .BR getcwd ()
257 instead.
258 POSIX.1-2001
259 does not define any errors for
260 .BR getwd ().
261 .PP
262 .BR get_current_dir_name ()
263 is a GNU extension.
264 .SH NOTES
265 Under Linux, the function
266 .BR getcwd ()
267 is a system call (since 2.1.92).
268 On older systems it would query
269 .IR /proc/self/cwd .
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
274 .BR EACCES .
275 .PP
276 Since a Linux 2.6.36 change that added "(unreachable)", the glibc
277 .BR getcwd ()
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;
281 calling
282 .BR getcwd ()
283 from such a path will now result in failure with
284 .BR ENOENT .
285 .PP
286 These functions are often used to save the location of the current working
287 directory for the purpose of returning to it later.
288 Opening the current
289 directory (".") and calling
290 .BR fchdir (2)
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.
293 .SH SEE ALSO
294 .BR pwd (1),
295 .BR chdir (2),
296 .BR fchdir (2),
297 .BR open (2),
298 .BR unlink (2),
299 .BR free (3),
300 .BR malloc (3)