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, then enable
31 code below to make getcwd more efficient and robust. */
33 # define HAVE_OPENAT_SUPPORT 1
35 # define HAVE_OPENAT_SUPPORT 0
39 # define __set_errno(val) (errno = (val))
43 #ifndef _D_EXACT_NAMLEN
44 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
46 #ifndef _D_ALLOC_NAMLEN
47 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
56 # define mempcpy __mempcpy
63 # define MAX(a, b) ((a) < (b) ? (b) : (a))
66 # define MIN(a, b) ((a) < (b) ? (a) : (b))
71 # define PATH_MAX MAXPATHLEN
73 # define PATH_MAX 1024
78 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
80 # define MATCHING_INO(dp, ino) true
84 # define __getcwd rpl_getcwd
85 # define __lstat lstat
86 # define __closedir closedir
87 # define __opendir opendir
88 # define __readdir readdir
91 /* The results of opendir() in this file are not used with dirfd and fchdir,
92 and we do not leak fds to any single-threaded code that could use stdio,
93 therefore save some unnecessary recursion in fchdir.c.
94 FIXME - if the kernel ever adds support for multi-thread safety for
95 avoiding standard fds, then we should use opendir_safer and
100 /* Get the name of the current working directory, and put it in SIZE
101 bytes of BUF. Returns NULL if the directory couldn't be determined or
102 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
103 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
104 unless SIZE == 0, in which case it is as big as necessary. */
107 __getcwd (char *buf, size_t size)
109 /* Lengths of big file name components and entire file names, and a
110 deep level of file name nesting. These numbers are not upper
111 bounds; they are merely large values suitable for initial
112 allocations, designed to be large enough for most real-world
116 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
117 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
121 #if HAVE_OPENAT_SUPPORT
123 bool fd_needs_closing = false;
125 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
126 char *dotlist = dots;
127 size_t dotsize = sizeof dots;
130 DIR *dirstream = NULL;
131 dev_t rootdev, thisdev;
132 ino_t rootino, thisino;
136 size_t allocated = size;
139 #if HAVE_RAW_DECL_GETCWD
140 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
141 this is much slower than the system getcwd (at least on
142 GNU/Linux). So trust the system getcwd's results unless they
145 Use the system getcwd even if we have openat support, since the
146 system getcwd works even when a parent is unreadable, while the
147 openat-based approach does not. */
150 dir = getcwd (buf, size);
154 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
155 internal magic that lets it work even if an ancestor directory is
156 inaccessible, which is better in many cases. So in this case try
157 again with a buffer that's almost always big enough. */
158 if (errno == EINVAL && buf == NULL && size == 0)
160 char big_buffer[BIG_FILE_NAME_LENGTH + 1];
161 dir = getcwd (big_buffer, sizeof big_buffer);
166 # if HAVE_PARTLY_WORKING_GETCWD
167 /* The system getcwd works, except it sometimes fails when it
168 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
169 if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
178 __set_errno (EINVAL);
182 allocated = BIG_FILE_NAME_LENGTH + 1;
187 dir = malloc (allocated);
194 dirp = dir + allocated;
197 if (__lstat (".", &st) < 0)
202 if (__lstat ("/", &st) < 0)
207 while (!(thisdev == rootdev && thisino == rootino))
216 bool use_d_ino = true;
218 /* Look at the parent directory. */
219 #if HAVE_OPENAT_SUPPORT
220 fd = openat (fd, "..", O_RDONLY);
223 fd_needs_closing = true;
224 parent_status = fstat (fd, &st);
226 dotlist[dotlen++] = '.';
227 dotlist[dotlen++] = '.';
228 dotlist[dotlen] = '\0';
229 parent_status = __lstat (dotlist, &st);
231 if (parent_status != 0)
234 if (dirstream && __closedir (dirstream) != 0)
240 /* Figure out if this directory is a mount point. */
243 mount_point = dotdev != thisdev;
245 /* Search for the last directory. */
246 #if HAVE_OPENAT_SUPPORT
247 dirstream = fdopendir (fd);
248 if (dirstream == NULL)
250 fd_needs_closing = false;
252 dirstream = __opendir (dotlist);
253 if (dirstream == NULL)
255 dotlist[dotlen++] = '/';
259 /* Clear errno to distinguish EOF from error if readdir returns
262 d = __readdir (dirstream);
264 /* When we've iterated through all directory entries without finding
265 one with a matching d_ino, rewind the stream and consider each
266 name again, but this time, using lstat. This is necessary in a
267 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
268 .., ../.., ../../.., etc. all had the same device number, yet the
269 d_ino values for entries in / did not match those obtained
271 if (d == NULL && errno == 0 && use_d_ino)
274 rewinddir (dirstream);
275 d = __readdir (dirstream);
281 /* EOF on dirstream, which can mean e.g., that the current
282 directory has been removed. */
283 __set_errno (ENOENT);
286 if (d->d_name[0] == '.' &&
287 (d->d_name[1] == '\0' ||
288 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
293 bool match = (MATCHING_INO (d, thisino) || mount_point);
300 #if HAVE_OPENAT_SUPPORT
301 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
303 /* Compute size needed for this file name, or for the file
304 name ".." in the same directory, whichever is larger.
305 Room for ".." might be needed the next time through
307 size_t name_alloc = _D_ALLOC_NAMLEN (d);
308 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
310 if (filesize < dotlen)
311 goto memory_exhausted;
313 if (dotsize < filesize)
315 /* My, what a deep directory tree you have, Grandma. */
316 size_t newsize = MAX (filesize, dotsize * 2);
318 if (newsize < dotsize)
319 goto memory_exhausted;
322 dotlist = malloc (newsize);
337 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
338 entry_status = __lstat (dotlist, &st);
340 /* We don't fail here if we cannot stat() a directory entry.
341 This can happen when (network) file systems fail. If this
342 entry is in fact the one we are looking for we will find
343 out soon as we reach the end of the directory without
344 having found anything. */
345 if (entry_status == 0 && S_ISDIR (st.st_mode)
346 && st.st_dev == thisdev && st.st_ino == thisino)
351 dirroom = dirp - dir;
352 namlen = _D_EXACT_NAMLEN (d);
354 if (dirroom <= namlen)
358 __set_errno (ERANGE);
364 size_t oldsize = allocated;
366 allocated += MAX (allocated, namlen);
367 if (allocated < oldsize
368 || ! (tmp = realloc (dir, allocated)))
369 goto memory_exhausted;
371 /* Move current contents up to the end of the buffer.
372 This is guaranteed to be non-overlapping. */
373 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
380 memcpy (dirp, d->d_name, namlen);
387 if (dirstream && __closedir (dirstream) != 0)
393 if (dirp == &dir[allocated - 1])
396 #if ! HAVE_OPENAT_SUPPORT
401 used = dir + allocated - dirp;
402 memmove (dir, dirp, used);
405 /* Ensure that the buffer is only as large as necessary. */
406 buf = realloc (dir, used);
409 /* Either buf was NULL all along, or `realloc' failed but
410 we still have the original string. */
416 __set_errno (ENOMEM);
421 __closedir (dirstream);
422 #if HAVE_OPENAT_SUPPORT
423 if (fd_needs_closing)
437 weak_alias (__getcwd, getcwd)