1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004,2005,2006 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 2, or (at your option)
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 along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 #include <sys/types.h>
33 #include <fcntl.h> /* For AT_FDCWD on Solaris 9. */
36 # define __set_errno(val) (errno = (val))
39 #if HAVE_DIRENT_H || _LIBC
42 # define dirent direct
43 # define _D_EXACT_NAMLEN(d) (d)->d_namlen
46 #ifndef _D_EXACT_NAMLEN
47 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
49 #ifndef _D_ALLOC_NAMLEN
50 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
59 # define mempcpy __mempcpy
67 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive. Its
68 value exceeds INT_MAX, so its use as an int doesn't conform to the
69 C standard, and GCC and Sun C complain in some cases. */
70 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
72 # define AT_FDCWD (-3041965)
76 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
78 # define is_ENAMETOOLONG(x) 0
82 # define MAX(a, b) ((a) < (b) ? (b) : (a))
85 # define MIN(a, b) ((a) < (b) ? (a) : (b))
90 # define PATH_MAX MAXPATHLEN
92 # define PATH_MAX 1024
97 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
99 # define MATCHING_INO(dp, ino) true
103 # define __getcwd getcwd
104 # define __lstat lstat
105 # define __closedir closedir
106 # define __opendir opendir
107 # define __readdir readdir
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),
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 && !defined AT_FDCWD
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 dir = getcwd (buf, size);
158 if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
166 __set_errno (EINVAL);
170 allocated = BIG_FILE_NAME_LENGTH + 1;
175 dir = malloc (allocated);
182 dirp = dir + allocated;
185 if (__lstat (".", &st) < 0)
190 if (__lstat ("/", &st) < 0)
195 while (!(thisdev == rootdev && thisino == rootino))
204 bool use_d_ino = true;
206 /* Look at the parent directory. */
208 fd = openat (fd, "..", O_RDONLY);
211 fd_needs_closing = true;
212 parent_status = fstat (fd, &st);
214 dotlist[dotlen++] = '.';
215 dotlist[dotlen++] = '.';
216 dotlist[dotlen] = '\0';
217 parent_status = __lstat (dotlist, &st);
219 if (parent_status != 0)
222 if (dirstream && __closedir (dirstream) != 0)
228 /* Figure out if this directory is a mount point. */
231 mount_point = dotdev != thisdev;
233 /* Search for the last directory. */
235 dirstream = fdopendir (fd);
236 if (dirstream == NULL)
238 fd_needs_closing = false;
240 dirstream = __opendir (dotlist);
241 if (dirstream == NULL)
243 dotlist[dotlen++] = '/';
247 /* Clear errno to distinguish EOF from error if readdir returns
250 d = __readdir (dirstream);
252 /* When we've iterated through all directory entries without finding
253 one with a matching d_ino, rewind the stream and consider each
254 name again, but this time, using lstat. This is necessary in a
255 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
256 .., ../.., ../../.., etc. all had the same device number, yet the
257 d_ino values for entries in / did not match those obtained
259 if (d == NULL && errno == 0 && use_d_ino)
262 rewinddir (dirstream);
263 d = __readdir (dirstream);
269 /* EOF on dirstream, which can mean e.g., that the current
270 directory has been removed. */
271 __set_errno (ENOENT);
274 if (d->d_name[0] == '.' &&
275 (d->d_name[1] == '\0' ||
276 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
281 bool match = (MATCHING_INO (d, thisino) || mount_point);
289 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
291 /* Compute size needed for this file name, or for the file
292 name ".." in the same directory, whichever is larger.
293 Room for ".." might be needed the next time through
295 size_t name_alloc = _D_ALLOC_NAMLEN (d);
296 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
298 if (filesize < dotlen)
299 goto memory_exhausted;
301 if (dotsize < filesize)
303 /* My, what a deep directory tree you have, Grandma. */
304 size_t newsize = MAX (filesize, dotsize * 2);
306 if (newsize < dotsize)
307 goto memory_exhausted;
310 dotlist = malloc (newsize);
325 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
326 entry_status = __lstat (dotlist, &st);
328 /* We don't fail here if we cannot stat() a directory entry.
329 This can happen when (network) file systems fail. If this
330 entry is in fact the one we are looking for we will find
331 out soon as we reach the end of the directory without
332 having found anything. */
333 if (entry_status == 0 && S_ISDIR (st.st_mode)
334 && st.st_dev == thisdev && st.st_ino == thisino)
339 dirroom = dirp - dir;
340 namlen = _D_EXACT_NAMLEN (d);
342 if (dirroom <= namlen)
346 __set_errno (ERANGE);
352 size_t oldsize = allocated;
354 allocated += MAX (allocated, namlen);
355 if (allocated < oldsize
356 || ! (tmp = realloc (dir, allocated)))
357 goto memory_exhausted;
359 /* Move current contents up to the end of the buffer.
360 This is guaranteed to be non-overlapping. */
361 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
368 memcpy (dirp, d->d_name, namlen);
375 if (dirstream && __closedir (dirstream) != 0)
381 if (dirp == &dir[allocated - 1])
389 used = dir + allocated - dirp;
390 memmove (dir, dirp, used);
392 if (buf == NULL && size == 0)
393 /* Ensure that the buffer is only as large as necessary. */
394 buf = realloc (dir, used);
397 /* Either buf was NULL all along, or `realloc' failed but
398 we still have the original string. */
404 __set_errno (ENOMEM);
409 __closedir (dirstream);
411 if (fd_needs_closing)
425 weak_alias (__getcwd, getcwd)