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_PARTLY_WORKING_GETCWD
142 /* The system getcwd works, except it sometimes fails when it
143 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
144 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
145 is much slower than the system getcwd (at least on GNU/Linux).
146 So trust the system getcwd's results unless they look
149 Use the system getcwd even if we have openat support, since the
150 system getcwd works even when a parent is unreadable, while the
151 openat-based approach does not. */
154 dir = getcwd (buf, size);
155 if (dir || (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT))
163 __set_errno (EINVAL);
167 allocated = BIG_FILE_NAME_LENGTH + 1;
172 dir = malloc (allocated);
179 dirp = dir + allocated;
182 if (__lstat (".", &st) < 0)
187 if (__lstat ("/", &st) < 0)
192 while (!(thisdev == rootdev && thisino == rootino))
201 bool use_d_ino = true;
203 /* Look at the parent directory. */
204 #if HAVE_OPENAT_SUPPORT
205 fd = openat (fd, "..", O_RDONLY);
208 fd_needs_closing = true;
209 parent_status = fstat (fd, &st);
211 dotlist[dotlen++] = '.';
212 dotlist[dotlen++] = '.';
213 dotlist[dotlen] = '\0';
214 parent_status = __lstat (dotlist, &st);
216 if (parent_status != 0)
219 if (dirstream && __closedir (dirstream) != 0)
225 /* Figure out if this directory is a mount point. */
228 mount_point = dotdev != thisdev;
230 /* Search for the last directory. */
231 #if HAVE_OPENAT_SUPPORT
232 dirstream = fdopendir (fd);
233 if (dirstream == NULL)
235 /* Reset fd. It may have been closed by fdopendir. */
236 fd = dirfd (dirstream);
237 fd_needs_closing = false;
239 dirstream = __opendir (dotlist);
240 if (dirstream == NULL)
242 dotlist[dotlen++] = '/';
246 /* Clear errno to distinguish EOF from error if readdir returns
249 d = __readdir (dirstream);
251 /* When we've iterated through all directory entries without finding
252 one with a matching d_ino, rewind the stream and consider each
253 name again, but this time, using lstat. This is necessary in a
254 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
255 .., ../.., ../../.., etc. all had the same device number, yet the
256 d_ino values for entries in / did not match those obtained
258 if (d == NULL && errno == 0 && use_d_ino)
261 rewinddir (dirstream);
262 d = __readdir (dirstream);
268 /* EOF on dirstream, which can mean e.g., that the current
269 directory has been removed. */
270 __set_errno (ENOENT);
273 if (d->d_name[0] == '.' &&
274 (d->d_name[1] == '\0' ||
275 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
280 bool match = (MATCHING_INO (d, thisino) || mount_point);
287 #if HAVE_OPENAT_SUPPORT
288 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
290 /* Compute size needed for this file name, or for the file
291 name ".." in the same directory, whichever is larger.
292 Room for ".." might be needed the next time through
294 size_t name_alloc = _D_ALLOC_NAMLEN (d);
295 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
297 if (filesize < dotlen)
298 goto memory_exhausted;
300 if (dotsize < filesize)
302 /* My, what a deep directory tree you have, Grandma. */
303 size_t newsize = MAX (filesize, dotsize * 2);
305 if (newsize < dotsize)
306 goto memory_exhausted;
309 dotlist = malloc (newsize);
324 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
325 entry_status = __lstat (dotlist, &st);
327 /* We don't fail here if we cannot stat() a directory entry.
328 This can happen when (network) file systems fail. If this
329 entry is in fact the one we are looking for we will find
330 out soon as we reach the end of the directory without
331 having found anything. */
332 if (entry_status == 0 && S_ISDIR (st.st_mode)
333 && st.st_dev == thisdev && st.st_ino == thisino)
338 dirroom = dirp - dir;
339 namlen = _D_EXACT_NAMLEN (d);
341 if (dirroom <= namlen)
345 __set_errno (ERANGE);
351 size_t oldsize = allocated;
353 allocated += MAX (allocated, namlen);
354 if (allocated < oldsize
355 || ! (tmp = realloc (dir, allocated)))
356 goto memory_exhausted;
358 /* Move current contents up to the end of the buffer.
359 This is guaranteed to be non-overlapping. */
360 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
367 memcpy (dirp, d->d_name, namlen);
374 if (dirstream && __closedir (dirstream) != 0)
380 if (dirp == &dir[allocated - 1])
383 #if ! HAVE_OPENAT_SUPPORT
388 used = dir + allocated - dirp;
389 memmove (dir, dirp, used);
392 /* Ensure that the buffer is only as large as necessary. */
393 buf = realloc (dir, used);
396 /* Either buf was NULL all along, or `realloc' failed but
397 we still have the original string. */
403 __set_errno (ENOMEM);
408 __closedir (dirstream);
409 #if HAVE_OPENAT_SUPPORT
410 if (fd_needs_closing)
424 weak_alias (__getcwd, getcwd)