1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1991-1999, 2004-2010 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 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. */
32 /* If this host provides the openat function, then enable
33 code below to make getcwd more efficient and robust. */
35 # define HAVE_OPENAT_SUPPORT 1
37 # define HAVE_OPENAT_SUPPORT 0
41 # define __set_errno(val) (errno = (val))
45 #ifndef _D_EXACT_NAMLEN
46 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
48 #ifndef _D_ALLOC_NAMLEN
49 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
58 # define mempcpy __mempcpy
65 # define MAX(a, b) ((a) < (b) ? (b) : (a))
68 # define MIN(a, b) ((a) < (b) ? (a) : (b))
73 # define PATH_MAX MAXPATHLEN
75 # define PATH_MAX 1024
80 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
82 # define MATCHING_INO(dp, ino) true
86 # define __getcwd rpl_getcwd
87 # define __lstat lstat
88 # define __closedir closedir
89 # define __opendir opendir
90 # define __readdir readdir
93 /* The results of opendir() in this file are not used with dirfd and fchdir,
94 and we do not leak fds to any single-threaded code that could use stdio,
95 therefore save some unnecessary recursion in fchdir.c.
96 FIXME - if the kernel ever adds support for multi-thread safety for
97 avoiding standard fds, then we should use opendir_safer and
102 /* Get the name of the current working directory, and put it in SIZE
103 bytes of BUF. Returns NULL if the directory couldn't be determined or
104 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
105 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
106 unless SIZE == 0, in which case it is as big as necessary. */
109 __getcwd (char *buf, size_t size)
111 /* Lengths of big file name components and entire file names, and a
112 deep level of file name nesting. These numbers are not upper
113 bounds; they are merely large values suitable for initial
114 allocations, designed to be large enough for most real-world
118 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
119 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
123 #if HAVE_OPENAT_SUPPORT
125 bool fd_needs_closing = false;
127 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
128 char *dotlist = dots;
129 size_t dotsize = sizeof dots;
132 DIR *dirstream = NULL;
133 dev_t rootdev, thisdev;
134 ino_t rootino, thisino;
138 size_t allocated = size;
141 #if HAVE_RAW_DECL_GETCWD
142 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
143 this is much slower than the system getcwd (at least on
144 GNU/Linux). So trust the system getcwd's results unless they
147 Use the system getcwd even if we have openat support, since the
148 system getcwd works even when a parent is unreadable, while the
149 openat-based approach does not. */
152 dir = getcwd (buf, size);
156 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
157 internal magic that lets it work even if an ancestor directory is
158 inaccessible, which is better in many cases. So in this case try
159 again with a buffer that's almost always big enough. */
160 if (errno == EINVAL && buf == NULL && size == 0)
162 char big_buffer[BIG_FILE_NAME_LENGTH + 1];
163 dir = getcwd (big_buffer, sizeof big_buffer);
168 # if HAVE_PARTLY_WORKING_GETCWD
169 /* The system getcwd works, except it sometimes fails when it
170 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
171 if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
180 __set_errno (EINVAL);
184 allocated = BIG_FILE_NAME_LENGTH + 1;
189 dir = malloc (allocated);
196 dirp = dir + allocated;
199 if (__lstat (".", &st) < 0)
204 if (__lstat ("/", &st) < 0)
209 while (!(thisdev == rootdev && thisino == rootino))
218 bool use_d_ino = true;
220 /* Look at the parent directory. */
221 #if HAVE_OPENAT_SUPPORT
222 fd = openat (fd, "..", O_RDONLY);
225 fd_needs_closing = true;
226 parent_status = fstat (fd, &st);
228 dotlist[dotlen++] = '.';
229 dotlist[dotlen++] = '.';
230 dotlist[dotlen] = '\0';
231 parent_status = __lstat (dotlist, &st);
233 if (parent_status != 0)
236 if (dirstream && __closedir (dirstream) != 0)
242 /* Figure out if this directory is a mount point. */
245 mount_point = dotdev != thisdev;
247 /* Search for the last directory. */
248 #if HAVE_OPENAT_SUPPORT
249 dirstream = fdopendir (fd);
250 if (dirstream == NULL)
252 fd_needs_closing = false;
254 dirstream = __opendir (dotlist);
255 if (dirstream == NULL)
257 dotlist[dotlen++] = '/';
261 /* Clear errno to distinguish EOF from error if readdir returns
264 d = __readdir (dirstream);
266 /* When we've iterated through all directory entries without finding
267 one with a matching d_ino, rewind the stream and consider each
268 name again, but this time, using lstat. This is necessary in a
269 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
270 .., ../.., ../../.., etc. all had the same device number, yet the
271 d_ino values for entries in / did not match those obtained
273 if (d == NULL && errno == 0 && use_d_ino)
276 rewinddir (dirstream);
277 d = __readdir (dirstream);
283 /* EOF on dirstream, which can mean e.g., that the current
284 directory has been removed. */
285 __set_errno (ENOENT);
288 if (d->d_name[0] == '.' &&
289 (d->d_name[1] == '\0' ||
290 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
295 bool match = (MATCHING_INO (d, thisino) || mount_point);
302 #if HAVE_OPENAT_SUPPORT
303 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
305 /* Compute size needed for this file name, or for the file
306 name ".." in the same directory, whichever is larger.
307 Room for ".." might be needed the next time through
309 size_t name_alloc = _D_ALLOC_NAMLEN (d);
310 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
312 if (filesize < dotlen)
313 goto memory_exhausted;
315 if (dotsize < filesize)
317 /* My, what a deep directory tree you have, Grandma. */
318 size_t newsize = MAX (filesize, dotsize * 2);
320 if (newsize < dotsize)
321 goto memory_exhausted;
324 dotlist = malloc (newsize);
339 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
340 entry_status = __lstat (dotlist, &st);
342 /* We don't fail here if we cannot stat() a directory entry.
343 This can happen when (network) file systems fail. If this
344 entry is in fact the one we are looking for we will find
345 out soon as we reach the end of the directory without
346 having found anything. */
347 if (entry_status == 0 && S_ISDIR (st.st_mode)
348 && st.st_dev == thisdev && st.st_ino == thisino)
353 dirroom = dirp - dir;
354 namlen = _D_EXACT_NAMLEN (d);
356 if (dirroom <= namlen)
360 __set_errno (ERANGE);
366 size_t oldsize = allocated;
368 allocated += MAX (allocated, namlen);
369 if (allocated < oldsize
370 || ! (tmp = realloc (dir, allocated)))
371 goto memory_exhausted;
373 /* Move current contents up to the end of the buffer.
374 This is guaranteed to be non-overlapping. */
375 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
382 memcpy (dirp, d->d_name, namlen);
389 if (dirstream && __closedir (dirstream) != 0)
395 if (dirp == &dir[allocated - 1])
398 #if ! HAVE_OPENAT_SUPPORT
403 used = dir + allocated - dirp;
404 memmove (dir, dirp, used);
407 /* Ensure that the buffer is only as large as necessary. */
408 buf = realloc (dir, used);
411 /* Either buf was NULL all along, or `realloc' failed but
412 we still have the original string. */
418 __set_errno (ENOMEM);
423 __closedir (dirstream);
424 #if HAVE_OPENAT_SUPPORT
425 if (fd_needs_closing)
439 weak_alias (__getcwd, getcwd)