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
41 # ifndef _D_EXACT_NAMLEN
42 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
45 # define dirent direct
47 # include <sys/ndir.h>
56 #ifndef _D_EXACT_NAMLEN
57 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
59 #ifndef _D_ALLOC_NAMLEN
60 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
69 # define mempcpy __mempcpy
77 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive. Its
78 value exceeds INT_MAX, so its use as an int doesn't conform to the
79 C standard, and GCC and Sun C complain in some cases. */
80 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
82 # define AT_FDCWD (-3041965)
86 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
88 # define is_ENAMETOOLONG(x) 0
92 # define MAX(a, b) ((a) < (b) ? (b) : (a))
95 # define MIN(a, b) ((a) < (b) ? (a) : (b))
100 # define PATH_MAX MAXPATHLEN
102 # define PATH_MAX 1024
107 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
109 # define MATCHING_INO(dp, ino) true
113 # define __getcwd getcwd
114 # define __lstat lstat
115 # define __closedir closedir
116 # define __opendir opendir
117 # define __readdir readdir
120 /* Get the name of the current working directory, and put it in SIZE
121 bytes of BUF. Returns NULL if the directory couldn't be determined or
122 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
123 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
124 unless SIZE == 0, in which case it is as big as necessary. */
127 __getcwd (char *buf, size_t size)
129 /* Lengths of big file name components and entire file names, and a
130 deep level of file name nesting. These numbers are not upper
131 bounds; they are merely large values suitable for initial
132 allocations, designed to be large enough for most real-world
136 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
137 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
143 bool fd_needs_closing = false;
145 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
146 char *dotlist = dots;
147 size_t dotsize = sizeof dots;
150 DIR *dirstream = NULL;
151 dev_t rootdev, thisdev;
152 ino_t rootino, thisino;
156 size_t allocated = size;
159 #if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD
160 /* The system getcwd works, except it sometimes fails when it
161 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
162 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
163 is much slower than the system getcwd (at least on GNU/Linux).
164 So trust the system getcwd's results unless they look
167 dir = getcwd (buf, size);
168 if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
176 __set_errno (EINVAL);
180 allocated = BIG_FILE_NAME_LENGTH + 1;
185 dir = malloc (allocated);
192 dirp = dir + allocated;
195 if (__lstat (".", &st) < 0)
200 if (__lstat ("/", &st) < 0)
205 while (!(thisdev == rootdev && thisino == rootino))
214 bool use_d_ino = true;
216 /* Look at the parent directory. */
218 fd = openat (fd, "..", O_RDONLY);
221 fd_needs_closing = true;
222 parent_status = fstat (fd, &st);
224 dotlist[dotlen++] = '.';
225 dotlist[dotlen++] = '.';
226 dotlist[dotlen] = '\0';
227 parent_status = __lstat (dotlist, &st);
229 if (parent_status != 0)
232 if (dirstream && __closedir (dirstream) != 0)
238 /* Figure out if this directory is a mount point. */
241 mount_point = dotdev != thisdev;
243 /* Search for the last directory. */
245 dirstream = fdopendir (fd);
246 if (dirstream == NULL)
248 fd_needs_closing = false;
250 dirstream = __opendir (dotlist);
251 if (dirstream == NULL)
253 dotlist[dotlen++] = '/';
257 /* Clear errno to distinguish EOF from error if readdir returns
260 d = __readdir (dirstream);
262 /* When we've iterated through all directory entries without finding
263 one with a matching d_ino, rewind the stream and consider each
264 name again, but this time, using lstat. This is necessary in a
265 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
266 .., ../.., ../../.., etc. all had the same device number, yet the
267 d_ino values for entries in / did not match those obtained
269 if (d == NULL && errno == 0 && use_d_ino)
272 rewinddir (dirstream);
273 d = __readdir (dirstream);
279 /* EOF on dirstream, which can mean e.g., that the current
280 directory has been removed. */
281 __set_errno (ENOENT);
284 if (d->d_name[0] == '.' &&
285 (d->d_name[1] == '\0' ||
286 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
291 bool match = (MATCHING_INO (d, thisino) || mount_point);
299 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
301 /* Compute size needed for this file name, or for the file
302 name ".." in the same directory, whichever is larger.
303 Room for ".." might be needed the next time through
305 size_t name_alloc = _D_ALLOC_NAMLEN (d);
306 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
308 if (filesize < dotlen)
309 goto memory_exhausted;
311 if (dotsize < filesize)
313 /* My, what a deep directory tree you have, Grandma. */
314 size_t newsize = MAX (filesize, dotsize * 2);
316 if (newsize < dotsize)
317 goto memory_exhausted;
320 dotlist = malloc (newsize);
335 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
336 entry_status = __lstat (dotlist, &st);
338 /* We don't fail here if we cannot stat() a directory entry.
339 This can happen when (network) file systems fail. If this
340 entry is in fact the one we are looking for we will find
341 out soon as we reach the end of the directory without
342 having found anything. */
343 if (entry_status == 0 && S_ISDIR (st.st_mode)
344 && st.st_dev == thisdev && st.st_ino == thisino)
349 dirroom = dirp - dir;
350 namlen = _D_EXACT_NAMLEN (d);
352 if (dirroom <= namlen)
356 __set_errno (ERANGE);
362 size_t oldsize = allocated;
364 allocated += MAX (allocated, namlen);
365 if (allocated < oldsize
366 || ! (tmp = realloc (dir, allocated)))
367 goto memory_exhausted;
369 /* Move current contents up to the end of the buffer.
370 This is guaranteed to be non-overlapping. */
371 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
378 memcpy (dirp, d->d_name, namlen);
385 if (dirstream && __closedir (dirstream) != 0)
391 if (dirp == &dir[allocated - 1])
399 used = dir + allocated - dirp;
400 memmove (dir, dirp, used);
402 if (buf == NULL && size == 0)
403 /* Ensure that the buffer is only as large as necessary. */
404 buf = realloc (dir, used);
407 /* Either buf was NULL all along, or `realloc' failed but
408 we still have the original string. */
414 __set_errno (ENOMEM);
419 __closedir (dirstream);
421 if (fd_needs_closing)
435 weak_alias (__getcwd, getcwd)