1 /* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
23 # define HAVE_OPENAT 1
24 # define D_INO_IN_DIRENT 1
25 # define HAVE_MSVC_INVALID_PARAMETER_HANDLER 0
26 # define HAVE_MINIMALLY_WORKING_GETCWD 0
30 #include <sys/types.h>
35 #include <fcntl.h> /* For AT_FDCWD on Solaris 9. */
37 /* If this host provides the openat function or if we're using the
38 gnulib replacement function with a native fdopendir, then enable
39 code below to make getcwd more efficient and robust. */
40 #if defined HAVE_OPENAT || (defined GNULIB_OPENAT && defined HAVE_FDOPENDIR)
41 # define HAVE_OPENAT_SUPPORT 1
43 # define HAVE_OPENAT_SUPPORT 0
47 # define __set_errno(val) (errno = (val))
51 #ifndef _D_EXACT_NAMLEN
52 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
54 #ifndef _D_ALLOC_NAMLEN
55 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
64 # define mempcpy __mempcpy
69 # define MAX(a, b) ((a) < (b) ? (b) : (a))
72 # define MIN(a, b) ((a) < (b) ? (a) : (b))
75 /* In this file, PATH_MAX only serves as a threshold for choosing among two
78 # define PATH_MAX 8192
82 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
84 # define MATCHING_INO(dp, ino) true
87 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
88 # include "msvc-inval.h"
92 # define __close_nocancel_nostatus close
93 # define __getcwd_generic rpl_getcwd
95 # define __fstat64 fstat
96 # define __fstatat64 fstatat
97 # define __lstat64 lstat
98 # define __closedir closedir
99 # define __opendir opendir
100 # define __readdir64 readdir
101 # define __fdopendir fdopendir
102 # define __openat openat
103 # define __rewinddir rewinddir
104 # define __openat64 openat
105 # define dirent64 dirent
107 # include <not-cancel.h>
110 /* The results of opendir() in this file are not used with dirfd and fchdir,
111 and we do not leak fds to any single-threaded code that could use stdio,
112 therefore save some unnecessary recursion in fchdir.c.
113 FIXME - if the kernel ever adds support for multi-thread safety for
114 avoiding standard fds, then we should use opendir_safer and
116 #ifdef GNULIB_defined_opendir
119 #ifdef GNULIB_defined_closedir
123 #if defined _WIN32 && !defined __CYGWIN__
124 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
126 getcwd_nothrow (char *buf, size_t size)
132 result = _getcwd (buf, size);
144 # define getcwd_nothrow _getcwd
146 # define getcwd_system getcwd_nothrow
148 # define getcwd_system getcwd
151 /* Get the name of the current working directory, and put it in SIZE
152 bytes of BUF. Returns NULL with errno set if the directory couldn't be
153 determined or SIZE was too small. If successful, returns BUF. In GNU,
154 if BUF is NULL, an array is allocated with 'malloc'; the array is SIZE
155 bytes long, unless SIZE == 0, in which case it is as big as necessary. */
158 __getcwd_generic (char *buf, size_t size)
160 /* Lengths of big file name components and entire file names, and a
161 deep level of file name nesting. These numbers are not upper
162 bounds; they are merely large values suitable for initial
163 allocations, designed to be large enough for most real-world
167 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
168 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
172 #if HAVE_OPENAT_SUPPORT
174 bool fd_needs_closing = false;
176 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
177 char *dotlist = dots;
178 size_t dotsize = sizeof dots;
181 DIR *dirstream = NULL;
182 dev_t rootdev, thisdev;
183 ino_t rootino, thisino;
186 struct __stat64_t64 st;
187 size_t allocated = size;
190 /* A size of 1 byte is never useful. */
193 __set_errno (ERANGE);
197 #if HAVE_MINIMALLY_WORKING_GETCWD
198 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
199 this is much slower than the system getcwd (at least on
200 GNU/Linux). So trust the system getcwd's results unless they
203 Use the system getcwd even if we have openat support, since the
204 system getcwd works even when a parent is unreadable, while the
205 openat-based approach does not.
207 But on AIX 5.1..7.1, the system getcwd is not even minimally
208 working: If the current directory name is slightly longer than
209 PATH_MAX, it omits the first directory component and returns
210 this wrong result with errno = 0. */
213 dir = getcwd_system (buf, size);
214 if (dir || (size && errno == ERANGE))
217 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
218 internal magic that lets it work even if an ancestor directory is
219 inaccessible, which is better in many cases. So in this case try
220 again with a buffer that's almost always big enough. */
221 if (errno == EINVAL && buf == NULL && size == 0)
223 char big_buffer[BIG_FILE_NAME_LENGTH + 1];
224 dir = getcwd_system (big_buffer, sizeof big_buffer);
229 # if HAVE_PARTLY_WORKING_GETCWD
230 /* The system getcwd works, except it sometimes fails when it
231 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
232 if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
240 __set_errno (EINVAL);
244 allocated = BIG_FILE_NAME_LENGTH + 1;
249 dir = malloc (allocated);
256 dirp = dir + allocated;
259 if (__lstat64_time64 (".", &st) < 0)
264 if (__lstat64_time64 ("/", &st) < 0)
269 while (!(thisdev == rootdev && thisino == rootino))
278 bool use_d_ino = true;
280 /* Look at the parent directory. */
281 #if HAVE_OPENAT_SUPPORT
282 fd = __openat64 (fd, "..", O_RDONLY);
285 fd_needs_closing = true;
286 parent_status = __fstat64_time64 (fd, &st);
288 dotlist[dotlen++] = '.';
289 dotlist[dotlen++] = '.';
290 dotlist[dotlen] = '\0';
291 parent_status = __lstat64_time64 (dotlist, &st);
293 if (parent_status != 0)
296 if (dirstream && __closedir (dirstream) != 0)
302 /* Figure out if this directory is a mount point. */
305 mount_point = dotdev != thisdev;
307 /* Search for the last directory. */
308 #if HAVE_OPENAT_SUPPORT
309 dirstream = __fdopendir (fd);
310 if (dirstream == NULL)
312 fd_needs_closing = false;
314 dirstream = __opendir (dotlist);
315 if (dirstream == NULL)
317 dotlist[dotlen++] = '/';
321 /* Clear errno to distinguish EOF from error if readdir returns
324 d = __readdir64 (dirstream);
326 /* When we've iterated through all directory entries without finding
327 one with a matching d_ino, rewind the stream and consider each
328 name again, but this time, using lstat. This is necessary in a
329 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
330 .., ../.., ../../.., etc. all had the same device number, yet the
331 d_ino values for entries in / did not match those obtained
333 if (d == NULL && errno == 0 && use_d_ino)
336 __rewinddir (dirstream);
337 d = __readdir64 (dirstream);
343 /* EOF on dirstream, which can mean e.g., that the current
344 directory has been removed. */
345 __set_errno (ENOENT);
348 if (d->d_name[0] == '.' &&
349 (d->d_name[1] == '\0' ||
350 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
355 bool match = (MATCHING_INO (d, thisino) || mount_point);
362 #if HAVE_OPENAT_SUPPORT
363 entry_status = __fstatat64_time64 (fd, d->d_name, &st,
364 AT_SYMLINK_NOFOLLOW);
366 /* Compute size needed for this file name, or for the file
367 name ".." in the same directory, whichever is larger.
368 Room for ".." might be needed the next time through
370 size_t name_alloc = _D_ALLOC_NAMLEN (d);
371 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
373 if (filesize < dotlen)
374 goto memory_exhausted;
376 if (dotsize < filesize)
378 /* My, what a deep directory tree you have, Grandma. */
379 size_t newsize = MAX (filesize, dotsize * 2);
381 if (newsize < dotsize)
382 goto memory_exhausted;
385 dotlist = malloc (newsize);
400 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
401 entry_status = __lstat64_time64 (dotlist, &st);
403 /* We don't fail here if we cannot stat() a directory entry.
404 This can happen when (network) file systems fail. If this
405 entry is in fact the one we are looking for we will find
406 out soon as we reach the end of the directory without
407 having found anything. */
408 if (entry_status == 0 && S_ISDIR (st.st_mode)
409 && st.st_dev == thisdev && st.st_ino == thisino)
414 dirroom = dirp - dir;
415 namlen = _D_EXACT_NAMLEN (d);
417 if (dirroom <= namlen)
421 __set_errno (ERANGE);
427 size_t oldsize = allocated;
429 allocated += MAX (allocated, namlen);
430 if (allocated < oldsize
431 || ! (tmp = realloc (dir, allocated)))
432 goto memory_exhausted;
434 /* Move current contents up to the end of the buffer.
435 This is guaranteed to be non-overlapping. */
436 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
443 memcpy (dirp, d->d_name, namlen);
450 if (dirstream && __closedir (dirstream) != 0)
456 if (dirp == &dir[allocated - 1])
459 #if ! HAVE_OPENAT_SUPPORT
464 used = dir + allocated - dirp;
465 memmove (dir, dirp, used);
468 /* Ensure that the buffer is only as large as necessary. */
469 buf = (used < allocated ? realloc (dir, used) : dir);
472 /* Either buf was NULL all along, or 'realloc' failed but
473 we still have the original string. */
479 __set_errno (ENOMEM);
484 __closedir (dirstream);
485 #if HAVE_OPENAT_SUPPORT
486 if (fd_needs_closing)
487 __close_nocancel_nostatus (fd);
499 #if defined _LIBC && !defined GETCWD_RETURN_TYPE
500 libc_hidden_def (__getcwd)
501 weak_alias (__getcwd, getcwd)