]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getcwd.3
getcwd.3, getdtablesize.3, getpass.3, lockf.3, ualarm.3, usleep.3: Update FTM require...
[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 2015-04-19 "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 .sp
39 .BI "char *getcwd(char *" buf ", size_t " size );
40 .sp
41 .BI "char *getwd(char *" buf );
42 .sp
43 .B "char *get_current_dir_name(void);"
44 .fi
45 .sp
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .sp
50 .in
51 .BR get_current_dir_name ():
52 .RS
53 _GNU_SOURCE
54 .RE
55 .sp
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
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
96 The
97 .BR getcwd ()
98 function copies an absolute pathname of the current working directory
99 to the array pointed to by
100 .IR buf ,
101 which is of length
102 .IR size .
103 .PP
104 If the length of the absolute pathname of the current working directory,
105 including the terminating null byte, exceeds
106 .I size
107 bytes, NULL is returned, and
108 .I errno
109 is set to
110 .BR ERANGE ;
111 an application should check for this error, and allocate a larger
112 buffer if necessary.
113 .PP
114 As an extension to the POSIX.1-2001 standard, glibc's
115 .BR getcwd ()
116 allocates the buffer dynamically using
117 .BR malloc (3)
118 if
119 .I buf
120 is NULL.
121 In this case, the allocated buffer has the length
122 .I size
123 unless
124 .I size
125 is zero, when
126 .I buf
127 is allocated as big as necessary.
128 The caller should
129 .BR free (3)
130 the returned buffer.
131
132 .BR get_current_dir_name ()
133 will
134 .BR malloc (3)
135 an array big enough to hold the absolute pathname of
136 the current working directory.
137 If the environment
138 variable
139 .B PWD
140 is set, and its value is correct, then that value will be returned.
141 The caller should
142 .BR free (3)
143 the returned buffer.
144
145 .BR getwd ()
146 does not
147 .BR malloc (3)
148 any memory.
149 The
150 .I buf
151 argument should be a pointer to an array at least
152 .B PATH_MAX
153 bytes long.
154 If the length of the absolute pathname of the current working directory,
155 including the terminating null byte, exceeds
156 .B PATH_MAX
157 bytes, NULL is returned, and
158 .I errno
159 is set to
160 .BR ENAMETOOLONG .
161 (Note that on some systems,
162 .B PATH_MAX
163 may not be a compile-time constant;
164 furthermore, its value may depend on the filesystem, see
165 .BR pathconf (3).)
166 For portability and security reasons, use of
167 .BR getwd ()
168 is deprecated.
169 .SH RETURN VALUE
170 On success, these functions return a pointer to a string containing
171 the pathname of the current working directory.
172 In the case
173 .BR getcwd ()
174 and
175 .BR getwd ()
176 this is the same value as
177 .IR buf .
178
179 On failure, these functions return NULL, and
180 .I errno
181 is set to indicate the error.
182 The contents of the array pointed to by
183 .I buf
184 are undefined on error.
185 .SH ERRORS
186 .TP
187 .B EACCES
188 Permission to read or search a component of the filename was denied.
189 .TP
190 .B EFAULT
191 .I buf
192 points to a bad address.
193 .TP
194 .B EINVAL
195 The
196 .I size
197 argument is zero and
198 .I buf
199 is not a null pointer.
200 .TP
201 .B EINVAL
202 .BR getwd ():
203 .I buf
204 is NULL.
205 .TP
206 .B ENAMETOOLONG
207 .BR getwd ():
208 The size of the null-terminated absolute pathname string exceeds
209 .B PATH_MAX
210 bytes.
211 .TP
212 .B ENOMEM
213 Out of memory.
214 .TP
215 .B ENOENT
216 The current working directory has been unlinked.
217 .TP
218 .B ERANGE
219 The
220 .I size
221 argument is less than the length of the absolute pathname of the
222 working directory, including the terminating null byte.
223 You need to allocate a bigger array and try again.
224 .SH ATTRIBUTES
225 For an explanation of the terms used in this section, see
226 .BR attributes (7).
227 .TS
228 allbox;
229 lbw22 lb lb
230 l l l.
231 Interface Attribute Value
232 T{
233 .BR getcwd (),
234 .BR getwd ()
235 T} Thread safety MT-Safe
236 T{
237 .BR get_current_dir_name ()
238 T} Thread safety MT-Safe env
239 .TE
240 .SH CONFORMING TO
241 .BR getcwd ()
242 conforms to POSIX.1-2001.
243 Note however that POSIX.1-2001 leaves the behavior of
244 .BR getcwd ()
245 unspecified if
246 .I buf
247 is NULL.
248
249 .BR getwd ()
250 is present in POSIX.1-2001, but marked LEGACY.
251 POSIX.1-2008 removes the specification of
252 .BR getwd ().
253 Use
254 .BR getcwd ()
255 instead.
256 POSIX.1-2001
257 does not define any errors for
258 .BR getwd ().
259
260 .BR get_current_dir_name ()
261 is a GNU extension.
262 .SH NOTES
263 Under Linux, the function
264 .BR getcwd ()
265 is a system call (since 2.1.92).
266 On older systems it would query
267 .IR /proc/self/cwd .
268 If both system call and proc filesystem are missing, a
269 generic implementation is called.
270 Only in that case can
271 these calls fail under Linux with
272 .BR EACCES .
273 .LP
274 These functions are often used to save the location of the current working
275 directory for the purpose of returning to it later.
276 Opening the current
277 directory (".") and calling
278 .BR fchdir (2)
279 to return is usually a faster and more reliable alternative when sufficiently
280 many file descriptors are available, especially on platforms other than Linux.
281 .SH SEE ALSO
282 .BR pwd (1),
283 .BR chdir (2),
284 .BR fchdir (2),
285 .BR open (2),
286 .BR unlink (2),
287 .BR free (3),
288 .BR malloc (3)