1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004,2005,2006,2007 Free Software
3 This file is part of the GNU C Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include <sys/types.h>
30 #include <fcntl.h> /* For AT_FDCWD on Solaris 9. */
33 # define __set_errno(val) (errno = (val))
37 #ifndef _D_EXACT_NAMLEN
38 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
40 #ifndef _D_ALLOC_NAMLEN
41 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
50 # define mempcpy __mempcpy
56 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive. Its
57 value exceeds INT_MAX, so its use as an int doesn't conform to the
58 C standard, and GCC and Sun C complain in some cases. */
59 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
61 # define AT_FDCWD (-3041965)
65 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
67 # define is_ENAMETOOLONG(x) 0
71 # define MAX(a, b) ((a) < (b) ? (b) : (a))
74 # define MIN(a, b) ((a) < (b) ? (a) : (b))
79 # define PATH_MAX MAXPATHLEN
81 # define PATH_MAX 1024
86 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
88 # define MATCHING_INO(dp, ino) true
92 # define __getcwd rpl_getcwd
93 # define __lstat lstat
94 # define __closedir closedir
95 # define __opendir opendir
96 # define __readdir readdir
99 /* The results of opendir() in this file are not used with dirfd and fchdir,
100 therefore save some unnecessary recursion in fchdir.c. */
104 /* Get the name of the current working directory, and put it in SIZE
105 bytes of BUF. Returns NULL if the directory couldn't be determined or
106 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
107 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
108 unless SIZE == 0, in which case it is as big as necessary. */
111 __getcwd (char *buf, size_t size)
113 /* Lengths of big file name components and entire file names, and a
114 deep level of file name nesting. These numbers are not upper
115 bounds; they are merely large values suitable for initial
116 allocations, designed to be large enough for most real-world
120 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
121 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
127 bool fd_needs_closing = false;
129 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
130 char *dotlist = dots;
131 size_t dotsize = sizeof dots;
134 DIR *dirstream = NULL;
135 dev_t rootdev, thisdev;
136 ino_t rootino, thisino;
140 size_t allocated = size;
143 #if HAVE_PARTLY_WORKING_GETCWD
144 /* The system getcwd works, except it sometimes fails when it
145 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
146 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
147 is much slower than the system getcwd (at least on GNU/Linux).
148 So trust the system getcwd's results unless they look
151 Use the system getcwd even if we have openat support, since the
152 system getcwd works even when a parent is unreadable, while the
153 openat-based approach does not. */
156 dir = getcwd (buf, size);
157 if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
165 __set_errno (EINVAL);
169 allocated = BIG_FILE_NAME_LENGTH + 1;
174 dir = malloc (allocated);
181 dirp = dir + allocated;
184 if (__lstat (".", &st) < 0)
189 if (__lstat ("/", &st) < 0)
194 while (!(thisdev == rootdev && thisino == rootino))
203 bool use_d_ino = true;
205 /* Look at the parent directory. */
207 fd = openat (fd, "..", O_RDONLY);
210 fd_needs_closing = true;
211 parent_status = fstat (fd, &st);
213 dotlist[dotlen++] = '.';
214 dotlist[dotlen++] = '.';
215 dotlist[dotlen] = '\0';
216 parent_status = __lstat (dotlist, &st);
218 if (parent_status != 0)
221 if (dirstream && __closedir (dirstream) != 0)
227 /* Figure out if this directory is a mount point. */
230 mount_point = dotdev != thisdev;
232 /* Search for the last directory. */
234 dirstream = fdopendir (fd);
235 if (dirstream == NULL)
237 /* Reset fd. It may have been closed by fdopendir. */
238 fd = dirfd (dirstream);
239 fd_needs_closing = false;
241 dirstream = __opendir (dotlist);
242 if (dirstream == NULL)
244 dotlist[dotlen++] = '/';
248 /* Clear errno to distinguish EOF from error if readdir returns
251 d = __readdir (dirstream);
253 /* When we've iterated through all directory entries without finding
254 one with a matching d_ino, rewind the stream and consider each
255 name again, but this time, using lstat. This is necessary in a
256 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
257 .., ../.., ../../.., etc. all had the same device number, yet the
258 d_ino values for entries in / did not match those obtained
260 if (d == NULL && errno == 0 && use_d_ino)
263 rewinddir (dirstream);
264 d = __readdir (dirstream);
270 /* EOF on dirstream, which can mean e.g., that the current
271 directory has been removed. */
272 __set_errno (ENOENT);
275 if (d->d_name[0] == '.' &&
276 (d->d_name[1] == '\0' ||
277 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
282 bool match = (MATCHING_INO (d, thisino) || mount_point);
290 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
292 /* Compute size needed for this file name, or for the file
293 name ".." in the same directory, whichever is larger.
294 Room for ".." might be needed the next time through
296 size_t name_alloc = _D_ALLOC_NAMLEN (d);
297 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
299 if (filesize < dotlen)
300 goto memory_exhausted;
302 if (dotsize < filesize)
304 /* My, what a deep directory tree you have, Grandma. */
305 size_t newsize = MAX (filesize, dotsize * 2);
307 if (newsize < dotsize)
308 goto memory_exhausted;
311 dotlist = malloc (newsize);
326 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
327 entry_status = __lstat (dotlist, &st);
329 /* We don't fail here if we cannot stat() a directory entry.
330 This can happen when (network) file systems fail. If this
331 entry is in fact the one we are looking for we will find
332 out soon as we reach the end of the directory without
333 having found anything. */
334 if (entry_status == 0 && S_ISDIR (st.st_mode)
335 && st.st_dev == thisdev && st.st_ino == thisino)
340 dirroom = dirp - dir;
341 namlen = _D_EXACT_NAMLEN (d);
343 if (dirroom <= namlen)
347 __set_errno (ERANGE);
353 size_t oldsize = allocated;
355 allocated += MAX (allocated, namlen);
356 if (allocated < oldsize
357 || ! (tmp = realloc (dir, allocated)))
358 goto memory_exhausted;
360 /* Move current contents up to the end of the buffer.
361 This is guaranteed to be non-overlapping. */
362 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
369 memcpy (dirp, d->d_name, namlen);
376 if (dirstream && __closedir (dirstream) != 0)
382 if (dirp == &dir[allocated - 1])
390 used = dir + allocated - dirp;
391 memmove (dir, dirp, used);
394 /* Ensure that the buffer is only as large as necessary. */
395 buf = realloc (dir, used);
398 /* Either buf was NULL all along, or `realloc' failed but
399 we still have the original string. */
405 __set_errno (ENOMEM);
410 __closedir (dirstream);
412 if (fd_needs_closing)
426 weak_alias (__getcwd, getcwd)