1 /* Copyright (C) 1991-1999, 2004-2011 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 <http://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, then enable code below to make getcwd
32 more efficient and robust. */
33 #if defined HAVE_OPENAT || defined GNULIB_OPENAT
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
83 # define __getcwd rpl_getcwd
84 # define __lstat lstat
85 # define __closedir closedir
86 # define __opendir opendir
87 # define __readdir readdir
90 /* The results of opendir() in this file are not used with dirfd and fchdir,
91 and we do not leak fds to any single-threaded code that could use stdio,
92 therefore save some unnecessary recursion in fchdir.c.
93 FIXME - if the kernel ever adds support for multi-thread safety for
94 avoiding standard fds, then we should use opendir_safer and
99 /* Get the name of the current working directory, and put it in SIZE
100 bytes of BUF. Returns NULL if the directory couldn't be determined or
101 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
102 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
103 unless SIZE == 0, in which case it is as big as necessary. */
106 __getcwd (char *buf, size_t size)
108 /* Lengths of big file name components and entire file names, and a
109 deep level of file name nesting. These numbers are not upper
110 bounds; they are merely large values suitable for initial
111 allocations, designed to be large enough for most real-world
115 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
116 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
120 #if HAVE_OPENAT_SUPPORT
122 bool fd_needs_closing = false;
124 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
125 char *dotlist = dots;
126 size_t dotsize = sizeof dots;
129 DIR *dirstream = NULL;
130 dev_t rootdev, thisdev;
131 ino_t rootino, thisino;
135 size_t allocated = size;
138 #if HAVE_RAW_DECL_GETCWD
139 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
140 this is much slower than the system getcwd (at least on
141 GNU/Linux). So trust the system getcwd's results unless they
144 Use the system getcwd even if we have openat support, since the
145 system getcwd works even when a parent is unreadable, while the
146 openat-based approach does not. */
149 dir = getcwd (buf, size);
150 if (dir || (size && errno == ERANGE))
153 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
154 internal magic that lets it work even if an ancestor directory is
155 inaccessible, which is better in many cases. So in this case try
156 again with a buffer that's almost always big enough. */
157 if (errno == EINVAL && buf == NULL && size == 0)
159 char big_buffer[BIG_FILE_NAME_LENGTH + 1];
160 dir = getcwd (big_buffer, sizeof big_buffer);
165 # if HAVE_PARTLY_WORKING_GETCWD
166 /* The system getcwd works, except it sometimes fails when it
167 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
168 if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
177 __set_errno (EINVAL);
181 allocated = BIG_FILE_NAME_LENGTH + 1;
186 dir = malloc (allocated);
193 dirp = dir + allocated;
196 if (__lstat (".", &st) < 0)
201 if (__lstat ("/", &st) < 0)
206 while (!(thisdev == rootdev && thisino == rootino))
215 bool use_d_ino = true;
217 /* Look at the parent directory. */
218 #if HAVE_OPENAT_SUPPORT
219 fd = openat (fd, "..", O_RDONLY);
222 fd_needs_closing = true;
223 parent_status = fstat (fd, &st);
225 dotlist[dotlen++] = '.';
226 dotlist[dotlen++] = '.';
227 dotlist[dotlen] = '\0';
228 parent_status = __lstat (dotlist, &st);
230 if (parent_status != 0)
233 if (dirstream && __closedir (dirstream) != 0)
239 /* Figure out if this directory is a mount point. */
242 mount_point = dotdev != thisdev;
244 /* Search for the last directory. */
245 #if HAVE_OPENAT_SUPPORT
246 dirstream = fdopendir (fd);
247 if (dirstream == NULL)
249 fd_needs_closing = false;
251 dirstream = __opendir (dotlist);
252 if (dirstream == NULL)
254 dotlist[dotlen++] = '/';
258 /* Clear errno to distinguish EOF from error if readdir returns
261 d = __readdir (dirstream);
263 /* When we've iterated through all directory entries without finding
264 one with a matching d_ino, rewind the stream and consider each
265 name again, but this time, using lstat. This is necessary in a
266 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
267 .., ../.., ../../.., etc. all had the same device number, yet the
268 d_ino values for entries in / did not match those obtained
270 if (d == NULL && errno == 0 && use_d_ino)
273 rewinddir (dirstream);
274 d = __readdir (dirstream);
280 /* EOF on dirstream, which can mean e.g., that the current
281 directory has been removed. */
282 __set_errno (ENOENT);
285 if (d->d_name[0] == '.' &&
286 (d->d_name[1] == '\0' ||
287 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
292 bool match = (MATCHING_INO (d, thisino) || mount_point);
299 #if HAVE_OPENAT_SUPPORT
300 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
302 /* Compute size needed for this file name, or for the file
303 name ".." in the same directory, whichever is larger.
304 Room for ".." might be needed the next time through
306 size_t name_alloc = _D_ALLOC_NAMLEN (d);
307 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
309 if (filesize < dotlen)
310 goto memory_exhausted;
312 if (dotsize < filesize)
314 /* My, what a deep directory tree you have, Grandma. */
315 size_t newsize = MAX (filesize, dotsize * 2);
317 if (newsize < dotsize)
318 goto memory_exhausted;
321 dotlist = malloc (newsize);
336 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
337 entry_status = __lstat (dotlist, &st);
339 /* We don't fail here if we cannot stat() a directory entry.
340 This can happen when (network) file systems fail. If this
341 entry is in fact the one we are looking for we will find
342 out soon as we reach the end of the directory without
343 having found anything. */
344 if (entry_status == 0 && S_ISDIR (st.st_mode)
345 && st.st_dev == thisdev && st.st_ino == thisino)
350 dirroom = dirp - dir;
351 namlen = _D_EXACT_NAMLEN (d);
353 if (dirroom <= namlen)
357 __set_errno (ERANGE);
363 size_t oldsize = allocated;
365 allocated += MAX (allocated, namlen);
366 if (allocated < oldsize
367 || ! (tmp = realloc (dir, allocated)))
368 goto memory_exhausted;
370 /* Move current contents up to the end of the buffer.
371 This is guaranteed to be non-overlapping. */
372 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
379 memcpy (dirp, d->d_name, namlen);
386 if (dirstream && __closedir (dirstream) != 0)
392 if (dirp == &dir[allocated - 1])
395 #if ! HAVE_OPENAT_SUPPORT
400 used = dir + allocated - dirp;
401 memmove (dir, dirp, used);
404 /* Ensure that the buffer is only as large as necessary. */
405 buf = realloc (dir, used);
408 /* Either buf was NULL all along, or `realloc' failed but
409 we still have the original string. */
415 __set_errno (ENOMEM);
420 __closedir (dirstream);
421 #if HAVE_OPENAT_SUPPORT
422 if (fd_needs_closing)
436 weak_alias (__getcwd, getcwd)