1 /* Copyright (C) 1991-1999, 2004-2008 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
62 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive. Its
63 value exceeds INT_MAX, so its use as an int doesn't conform to the
64 C standard, and GCC and Sun C complain in some cases. */
65 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
67 # define AT_FDCWD (-3041965)
71 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
73 # define is_ENAMETOOLONG(x) 0
77 # define MAX(a, b) ((a) < (b) ? (b) : (a))
80 # define MIN(a, b) ((a) < (b) ? (a) : (b))
85 # define PATH_MAX MAXPATHLEN
87 # define PATH_MAX 1024
92 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
94 # define MATCHING_INO(dp, ino) true
98 # define __getcwd rpl_getcwd
99 # define __lstat lstat
100 # define __closedir closedir
101 # define __opendir opendir
102 # define __readdir readdir
105 /* The results of opendir() in this file are not used with dirfd and fchdir,
106 therefore save some unnecessary recursion in fchdir.c. */
110 /* Get the name of the current working directory, and put it in SIZE
111 bytes of BUF. Returns NULL if the directory couldn't be determined or
112 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
113 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
114 unless SIZE == 0, in which case it is as big as necessary. */
117 __getcwd (char *buf, size_t size)
119 /* Lengths of big file name components and entire file names, and a
120 deep level of file name nesting. These numbers are not upper
121 bounds; they are merely large values suitable for initial
122 allocations, designed to be large enough for most real-world
126 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
127 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
131 #if HAVE_OPENAT_SUPPORT
133 bool fd_needs_closing = false;
135 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
136 char *dotlist = dots;
137 size_t dotsize = sizeof dots;
140 DIR *dirstream = NULL;
141 dev_t rootdev, thisdev;
142 ino_t rootino, thisino;
146 size_t allocated = size;
149 #if HAVE_PARTLY_WORKING_GETCWD
150 /* The system getcwd works, except it sometimes fails when it
151 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
152 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
153 is much slower than the system getcwd (at least on GNU/Linux).
154 So trust the system getcwd's results unless they look
157 Use the system getcwd even if we have openat support, since the
158 system getcwd works even when a parent is unreadable, while the
159 openat-based approach does not. */
162 dir = getcwd (buf, size);
163 if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
171 __set_errno (EINVAL);
175 allocated = BIG_FILE_NAME_LENGTH + 1;
180 dir = malloc (allocated);
187 dirp = dir + allocated;
190 if (__lstat (".", &st) < 0)
195 if (__lstat ("/", &st) < 0)
200 while (!(thisdev == rootdev && thisino == rootino))
209 bool use_d_ino = true;
211 /* Look at the parent directory. */
212 #if HAVE_OPENAT_SUPPORT
213 fd = openat (fd, "..", O_RDONLY);
216 fd_needs_closing = true;
217 parent_status = fstat (fd, &st);
219 dotlist[dotlen++] = '.';
220 dotlist[dotlen++] = '.';
221 dotlist[dotlen] = '\0';
222 parent_status = __lstat (dotlist, &st);
224 if (parent_status != 0)
227 if (dirstream && __closedir (dirstream) != 0)
233 /* Figure out if this directory is a mount point. */
236 mount_point = dotdev != thisdev;
238 /* Search for the last directory. */
239 #if HAVE_OPENAT_SUPPORT
240 dirstream = fdopendir (fd);
241 if (dirstream == NULL)
243 /* Reset fd. It may have been closed by fdopendir. */
244 fd = dirfd (dirstream);
245 fd_needs_closing = false;
247 dirstream = __opendir (dotlist);
248 if (dirstream == NULL)
250 dotlist[dotlen++] = '/';
254 /* Clear errno to distinguish EOF from error if readdir returns
257 d = __readdir (dirstream);
259 /* When we've iterated through all directory entries without finding
260 one with a matching d_ino, rewind the stream and consider each
261 name again, but this time, using lstat. This is necessary in a
262 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
263 .., ../.., ../../.., etc. all had the same device number, yet the
264 d_ino values for entries in / did not match those obtained
266 if (d == NULL && errno == 0 && use_d_ino)
269 rewinddir (dirstream);
270 d = __readdir (dirstream);
276 /* EOF on dirstream, which can mean e.g., that the current
277 directory has been removed. */
278 __set_errno (ENOENT);
281 if (d->d_name[0] == '.' &&
282 (d->d_name[1] == '\0' ||
283 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
288 bool match = (MATCHING_INO (d, thisino) || mount_point);
295 #if HAVE_OPENAT_SUPPORT
296 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
298 /* Compute size needed for this file name, or for the file
299 name ".." in the same directory, whichever is larger.
300 Room for ".." might be needed the next time through
302 size_t name_alloc = _D_ALLOC_NAMLEN (d);
303 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
305 if (filesize < dotlen)
306 goto memory_exhausted;
308 if (dotsize < filesize)
310 /* My, what a deep directory tree you have, Grandma. */
311 size_t newsize = MAX (filesize, dotsize * 2);
313 if (newsize < dotsize)
314 goto memory_exhausted;
317 dotlist = malloc (newsize);
332 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
333 entry_status = __lstat (dotlist, &st);
335 /* We don't fail here if we cannot stat() a directory entry.
336 This can happen when (network) file systems fail. If this
337 entry is in fact the one we are looking for we will find
338 out soon as we reach the end of the directory without
339 having found anything. */
340 if (entry_status == 0 && S_ISDIR (st.st_mode)
341 && st.st_dev == thisdev && st.st_ino == thisino)
346 dirroom = dirp - dir;
347 namlen = _D_EXACT_NAMLEN (d);
349 if (dirroom <= namlen)
353 __set_errno (ERANGE);
359 size_t oldsize = allocated;
361 allocated += MAX (allocated, namlen);
362 if (allocated < oldsize
363 || ! (tmp = realloc (dir, allocated)))
364 goto memory_exhausted;
366 /* Move current contents up to the end of the buffer.
367 This is guaranteed to be non-overlapping. */
368 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
375 memcpy (dirp, d->d_name, namlen);
382 if (dirstream && __closedir (dirstream) != 0)
388 if (dirp == &dir[allocated - 1])
391 #if ! HAVE_OPENAT_SUPPORT
396 used = dir + allocated - dirp;
397 memmove (dir, dirp, used);
400 /* Ensure that the buffer is only as large as necessary. */
401 buf = realloc (dir, used);
404 /* Either buf was NULL all along, or `realloc' failed but
405 we still have the original string. */
411 __set_errno (ENOMEM);
416 __closedir (dirstream);
417 #if HAVE_OPENAT_SUPPORT
418 if (fd_needs_closing)
432 weak_alias (__getcwd, getcwd)