1 /* Copyright (C) 1991-1999, 2004-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
28 #include <fcntl.h> /* For AT_FDCWD on Solaris 9. */
30 /* If this host provides the openat function or if we're using the
31 gnulib replacement function with a native fdopendir, then enable
32 code below to make getcwd more efficient and robust. */
33 #if defined HAVE_OPENAT || (defined GNULIB_OPENAT && defined HAVE_FDOPENDIR)
34 # define HAVE_OPENAT_SUPPORT 1
36 # define HAVE_OPENAT_SUPPORT 0
40 # define __set_errno(val) (errno = (val))
44 #ifndef _D_EXACT_NAMLEN
45 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
47 #ifndef _D_ALLOC_NAMLEN
48 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
57 # define mempcpy __mempcpy
62 # define MAX(a, b) ((a) < (b) ? (b) : (a))
65 # define MIN(a, b) ((a) < (b) ? (a) : (b))
70 /* In this file, PATH_MAX only serves as a threshold for choosing among two
73 # define PATH_MAX 8192
77 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
79 # define MATCHING_INO(dp, ino) true
82 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
83 # include "msvc-inval.h"
87 # define __getcwd rpl_getcwd
88 # define __lstat lstat
89 # define __closedir closedir
90 # define __opendir opendir
91 # define __readdir readdir
94 /* The results of opendir() in this file are not used with dirfd and fchdir,
95 and we do not leak fds to any single-threaded code that could use stdio,
96 therefore save some unnecessary recursion in fchdir.c.
97 FIXME - if the kernel ever adds support for multi-thread safety for
98 avoiding standard fds, then we should use opendir_safer and
100 #ifdef GNULIB_defined_opendir
103 #ifdef GNULIB_defined_closedir
108 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
110 getcwd_nothrow (char *buf
, size_t size
)
116 result
= _getcwd (buf
, size
);
128 # define getcwd_nothrow _getcwd
130 # define getcwd_system getcwd_nothrow
132 # define getcwd_system getcwd
135 /* Get the name of the current working directory, and put it in SIZE
136 bytes of BUF. Returns NULL if the directory couldn't be determined or
137 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
138 NULL, an array is allocated with 'malloc'; the array is SIZE bytes long,
139 unless SIZE == 0, in which case it is as big as necessary. */
142 __getcwd (char *buf
, size_t size
)
144 /* Lengths of big file name components and entire file names, and a
145 deep level of file name nesting. These numbers are not upper
146 bounds; they are merely large values suitable for initial
147 allocations, designed to be large enough for most real-world
151 BIG_FILE_NAME_COMPONENT_LENGTH
= 255,
152 BIG_FILE_NAME_LENGTH
= MIN (4095, PATH_MAX
- 1),
156 #if HAVE_OPENAT_SUPPORT
158 bool fd_needs_closing
= false;
160 char dots
[DEEP_NESTING
* sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH
+ 1];
161 char *dotlist
= dots
;
162 size_t dotsize
= sizeof dots
;
165 DIR *dirstream
= NULL
;
166 dev_t rootdev
, thisdev
;
167 ino_t rootino
, thisino
;
171 size_t allocated
= size
;
174 #if HAVE_MINIMALLY_WORKING_GETCWD
175 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
176 this is much slower than the system getcwd (at least on
177 GNU/Linux). So trust the system getcwd's results unless they
180 Use the system getcwd even if we have openat support, since the
181 system getcwd works even when a parent is unreadable, while the
182 openat-based approach does not.
184 But on AIX 5.1..7.1, the system getcwd is not even minimally
185 working: If the current directory name is slightly longer than
186 PATH_MAX, it omits the first directory component and returns
187 this wrong result with errno = 0. */
190 dir
= getcwd_system (buf
, size
);
191 if (dir
|| (size
&& errno
== ERANGE
))
194 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
195 internal magic that lets it work even if an ancestor directory is
196 inaccessible, which is better in many cases. So in this case try
197 again with a buffer that's almost always big enough. */
198 if (errno
== EINVAL
&& buf
== NULL
&& size
== 0)
200 char big_buffer
[BIG_FILE_NAME_LENGTH
+ 1];
201 dir
= getcwd_system (big_buffer
, sizeof big_buffer
);
206 # if HAVE_PARTLY_WORKING_GETCWD
207 /* The system getcwd works, except it sometimes fails when it
208 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
209 if (errno
!= ERANGE
&& errno
!= ENAMETOOLONG
&& errno
!= ENOENT
)
218 __set_errno (EINVAL
);
222 allocated
= BIG_FILE_NAME_LENGTH
+ 1;
227 dir
= malloc (allocated
);
234 dirp
= dir
+ allocated
;
237 if (__lstat (".", &st
) < 0)
242 if (__lstat ("/", &st
) < 0)
247 while (!(thisdev
== rootdev
&& thisino
== rootino
))
256 bool use_d_ino
= true;
258 /* Look at the parent directory. */
259 #if HAVE_OPENAT_SUPPORT
260 fd
= openat (fd
, "..", O_RDONLY
);
263 fd_needs_closing
= true;
264 parent_status
= fstat (fd
, &st
);
266 dotlist
[dotlen
++] = '.';
267 dotlist
[dotlen
++] = '.';
268 dotlist
[dotlen
] = '\0';
269 parent_status
= __lstat (dotlist
, &st
);
271 if (parent_status
!= 0)
274 if (dirstream
&& __closedir (dirstream
) != 0)
280 /* Figure out if this directory is a mount point. */
283 mount_point
= dotdev
!= thisdev
;
285 /* Search for the last directory. */
286 #if HAVE_OPENAT_SUPPORT
287 dirstream
= fdopendir (fd
);
288 if (dirstream
== NULL
)
290 fd_needs_closing
= false;
292 dirstream
= __opendir (dotlist
);
293 if (dirstream
== NULL
)
295 dotlist
[dotlen
++] = '/';
299 /* Clear errno to distinguish EOF from error if readdir returns
302 d
= __readdir (dirstream
);
304 /* When we've iterated through all directory entries without finding
305 one with a matching d_ino, rewind the stream and consider each
306 name again, but this time, using lstat. This is necessary in a
307 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
308 .., ../.., ../../.., etc. all had the same device number, yet the
309 d_ino values for entries in / did not match those obtained
311 if (d
== NULL
&& errno
== 0 && use_d_ino
)
314 rewinddir (dirstream
);
315 d
= __readdir (dirstream
);
321 /* EOF on dirstream, which can mean e.g., that the current
322 directory has been removed. */
323 __set_errno (ENOENT
);
326 if (d
->d_name
[0] == '.' &&
327 (d
->d_name
[1] == '\0' ||
328 (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
333 bool match
= (MATCHING_INO (d
, thisino
) || mount_point
);
340 #if HAVE_OPENAT_SUPPORT
341 entry_status
= fstatat (fd
, d
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
);
343 /* Compute size needed for this file name, or for the file
344 name ".." in the same directory, whichever is larger.
345 Room for ".." might be needed the next time through
347 size_t name_alloc
= _D_ALLOC_NAMLEN (d
);
348 size_t filesize
= dotlen
+ MAX (sizeof "..", name_alloc
);
350 if (filesize
< dotlen
)
351 goto memory_exhausted
;
353 if (dotsize
< filesize
)
355 /* My, what a deep directory tree you have, Grandma. */
356 size_t newsize
= MAX (filesize
, dotsize
* 2);
358 if (newsize
< dotsize
)
359 goto memory_exhausted
;
362 dotlist
= malloc (newsize
);
377 memcpy (dotlist
+ dotlen
, d
->d_name
, _D_ALLOC_NAMLEN (d
));
378 entry_status
= __lstat (dotlist
, &st
);
380 /* We don't fail here if we cannot stat() a directory entry.
381 This can happen when (network) file systems fail. If this
382 entry is in fact the one we are looking for we will find
383 out soon as we reach the end of the directory without
384 having found anything. */
385 if (entry_status
== 0 && S_ISDIR (st
.st_mode
)
386 && st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
391 dirroom
= dirp
- dir
;
392 namlen
= _D_EXACT_NAMLEN (d
);
394 if (dirroom
<= namlen
)
398 __set_errno (ERANGE
);
404 size_t oldsize
= allocated
;
406 allocated
+= MAX (allocated
, namlen
);
407 if (allocated
< oldsize
408 || ! (tmp
= realloc (dir
, allocated
)))
409 goto memory_exhausted
;
411 /* Move current contents up to the end of the buffer.
412 This is guaranteed to be non-overlapping. */
413 dirp
= memcpy (tmp
+ allocated
- (oldsize
- dirroom
),
420 memcpy (dirp
, d
->d_name
, namlen
);
427 if (dirstream
&& __closedir (dirstream
) != 0)
433 if (dirp
== &dir
[allocated
- 1])
436 #if ! HAVE_OPENAT_SUPPORT
441 used
= dir
+ allocated
- dirp
;
442 memmove (dir
, dirp
, used
);
445 /* Ensure that the buffer is only as large as necessary. */
446 buf
= (used
< allocated
? realloc (dir
, used
) : dir
);
449 /* Either buf was NULL all along, or 'realloc' failed but
450 we still have the original string. */
456 __set_errno (ENOMEM
);
461 __closedir (dirstream
);
462 #if HAVE_OPENAT_SUPPORT
463 if (fd_needs_closing
)
477 weak_alias (__getcwd
, getcwd
)