1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1991-1999, 2004-2013 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 or if we're using the
33 gnulib replacement function with a native fdopendir, then enable
34 code below to make getcwd more efficient and robust. */
35 #if defined HAVE_OPENAT || (defined GNULIB_OPENAT && defined HAVE_FDOPENDIR)
36 # define HAVE_OPENAT_SUPPORT 1
38 # define HAVE_OPENAT_SUPPORT 0
42 # define __set_errno(val) (errno = (val))
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
64 # define MAX(a, b) ((a) < (b) ? (b) : (a))
67 # define MIN(a, b) ((a) < (b) ? (a) : (b))
72 /* In this file, PATH_MAX only serves as a threshold for choosing among two
75 # define PATH_MAX 8192
79 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
81 # define MATCHING_INO(dp, ino) true
85 # define __getcwd rpl_getcwd
86 # define __lstat lstat
87 # define __closedir closedir
88 # define __opendir opendir
89 # define __readdir readdir
92 /* The results of opendir() in this file are not used with dirfd and fchdir,
93 and we do not leak fds to any single-threaded code that could use stdio,
94 therefore save some unnecessary recursion in fchdir.c.
95 FIXME - if the kernel ever adds support for multi-thread safety for
96 avoiding standard fds, then we should use opendir_safer and
101 /* Get the name of the current working directory, and put it in SIZE
102 bytes of BUF. Returns NULL if the directory couldn't be determined or
103 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
104 NULL, an array is allocated with 'malloc'; the array is SIZE bytes long,
105 unless SIZE == 0, in which case it is as big as necessary. */
108 __getcwd (char *buf, size_t size)
110 /* Lengths of big file name components and entire file names, and a
111 deep level of file name nesting. These numbers are not upper
112 bounds; they are merely large values suitable for initial
113 allocations, designed to be large enough for most real-world
117 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
118 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
122 #if HAVE_OPENAT_SUPPORT
124 bool fd_needs_closing = false;
126 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
127 char *dotlist = dots;
128 size_t dotsize = sizeof dots;
131 DIR *dirstream = NULL;
132 dev_t rootdev, thisdev;
133 ino_t rootino, thisino;
137 size_t allocated = size;
140 #if HAVE_MINIMALLY_WORKING_GETCWD
141 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
142 this is much slower than the system getcwd (at least on
143 GNU/Linux). So trust the system getcwd's results unless they
146 Use the system getcwd even if we have openat support, since the
147 system getcwd works even when a parent is unreadable, while the
148 openat-based approach does not.
150 But on AIX 5.1..7.1, the system getcwd is not even minimally
151 working: If the current directory name is slightly longer than
152 PATH_MAX, it omits the first directory component and returns
153 this wrong result with errno = 0. */
156 dir = getcwd (buf, size);
157 if (dir || (size && errno == ERANGE))
160 /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
161 internal magic that lets it work even if an ancestor directory is
162 inaccessible, which is better in many cases. So in this case try
163 again with a buffer that's almost always big enough. */
164 if (errno == EINVAL && buf == NULL && size == 0)
166 char big_buffer[BIG_FILE_NAME_LENGTH + 1];
167 dir = getcwd (big_buffer, sizeof big_buffer);
172 # if HAVE_PARTLY_WORKING_GETCWD
173 /* The system getcwd works, except it sometimes fails when it
174 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. */
175 if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
184 __set_errno (EINVAL);
188 allocated = BIG_FILE_NAME_LENGTH + 1;
193 dir = malloc (allocated);
200 dirp = dir + allocated;
203 if (__lstat (".", &st) < 0)
208 if (__lstat ("/", &st) < 0)
213 while (!(thisdev == rootdev && thisino == rootino))
222 bool use_d_ino = true;
224 /* Look at the parent directory. */
225 #if HAVE_OPENAT_SUPPORT
226 fd = openat (fd, "..", O_RDONLY);
229 fd_needs_closing = true;
230 parent_status = fstat (fd, &st);
232 dotlist[dotlen++] = '.';
233 dotlist[dotlen++] = '.';
234 dotlist[dotlen] = '\0';
235 parent_status = __lstat (dotlist, &st);
237 if (parent_status != 0)
240 if (dirstream && __closedir (dirstream) != 0)
246 /* Figure out if this directory is a mount point. */
249 mount_point = dotdev != thisdev;
251 /* Search for the last directory. */
252 #if HAVE_OPENAT_SUPPORT
253 dirstream = fdopendir (fd);
254 if (dirstream == NULL)
256 fd_needs_closing = false;
258 dirstream = __opendir (dotlist);
259 if (dirstream == NULL)
261 dotlist[dotlen++] = '/';
265 /* Clear errno to distinguish EOF from error if readdir returns
268 d = __readdir (dirstream);
270 /* When we've iterated through all directory entries without finding
271 one with a matching d_ino, rewind the stream and consider each
272 name again, but this time, using lstat. This is necessary in a
273 chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
274 .., ../.., ../../.., etc. all had the same device number, yet the
275 d_ino values for entries in / did not match those obtained
277 if (d == NULL && errno == 0 && use_d_ino)
280 rewinddir (dirstream);
281 d = __readdir (dirstream);
287 /* EOF on dirstream, which can mean e.g., that the current
288 directory has been removed. */
289 __set_errno (ENOENT);
292 if (d->d_name[0] == '.' &&
293 (d->d_name[1] == '\0' ||
294 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
299 bool match = (MATCHING_INO (d, thisino) || mount_point);
306 #if HAVE_OPENAT_SUPPORT
307 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
309 /* Compute size needed for this file name, or for the file
310 name ".." in the same directory, whichever is larger.
311 Room for ".." might be needed the next time through
313 size_t name_alloc = _D_ALLOC_NAMLEN (d);
314 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
316 if (filesize < dotlen)
317 goto memory_exhausted;
319 if (dotsize < filesize)
321 /* My, what a deep directory tree you have, Grandma. */
322 size_t newsize = MAX (filesize, dotsize * 2);
324 if (newsize < dotsize)
325 goto memory_exhausted;
328 dotlist = malloc (newsize);
343 memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
344 entry_status = __lstat (dotlist, &st);
346 /* We don't fail here if we cannot stat() a directory entry.
347 This can happen when (network) file systems fail. If this
348 entry is in fact the one we are looking for we will find
349 out soon as we reach the end of the directory without
350 having found anything. */
351 if (entry_status == 0 && S_ISDIR (st.st_mode)
352 && st.st_dev == thisdev && st.st_ino == thisino)
357 dirroom = dirp - dir;
358 namlen = _D_EXACT_NAMLEN (d);
360 if (dirroom <= namlen)
364 __set_errno (ERANGE);
370 size_t oldsize = allocated;
372 allocated += MAX (allocated, namlen);
373 if (allocated < oldsize
374 || ! (tmp = realloc (dir, allocated)))
375 goto memory_exhausted;
377 /* Move current contents up to the end of the buffer.
378 This is guaranteed to be non-overlapping. */
379 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
386 memcpy (dirp, d->d_name, namlen);
393 if (dirstream && __closedir (dirstream) != 0)
399 if (dirp == &dir[allocated - 1])
402 #if ! HAVE_OPENAT_SUPPORT
407 used = dir + allocated - dirp;
408 memmove (dir, dirp, used);
411 /* Ensure that the buffer is only as large as necessary. */
412 buf = realloc (dir, used);
415 /* Either buf was NULL all along, or 'realloc' failed but
416 we still have the original string. */
422 __set_errno (ENOMEM);
427 __closedir (dirstream);
428 #if HAVE_OPENAT_SUPPORT
429 if (fd_needs_closing)
443 weak_alias (__getcwd, getcwd)