1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004,2005 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
78 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
80 # define is_ENAMETOOLONG(x) 0
84 # define MAX(a, b) ((a) < (b) ? (b) : (a))
87 # define MIN(a, b) ((a) < (b) ? (a) : (b))
92 # define PATH_MAX MAXPATHLEN
94 # define PATH_MAX 1024
99 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
101 # define MATCHING_INO(dp, ino) true
105 # define __getcwd getcwd
106 # define __lstat lstat
107 # define __closedir closedir
108 # define __opendir opendir
109 # define __readdir readdir
112 /* Get the name of the current working directory, and put it in SIZE
113 bytes of BUF. Returns NULL if the directory couldn't be determined or
114 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
115 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
116 unless SIZE == 0, in which case it is as big as necessary. */
119 __getcwd (char *buf, size_t size)
121 /* Lengths of big file name components and entire file names, and a
122 deep level of file name nesting. These numbers are not upper
123 bounds; they are merely large values suitable for initial
124 allocations, designed to be large enough for most real-world
128 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
129 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
135 bool fd_needs_closing = false;
137 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
138 char *dotlist = dots;
139 size_t dotsize = sizeof dots;
142 DIR *dirstream = NULL;
143 dev_t rootdev, thisdev;
144 ino_t rootino, thisino;
148 size_t allocated = size;
151 #if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD
152 /* The system getcwd works, except it sometimes fails when it
153 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
154 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
155 is much slower than the system getcwd (at least on GNU/Linux).
156 So trust the system getcwd's results unless they look
159 dir = getcwd (buf, size);
160 if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
168 __set_errno (EINVAL);
172 allocated = BIG_FILE_NAME_LENGTH + 1;
177 dir = malloc (allocated);
184 dirp = dir + allocated;
187 if (__lstat (".", &st) < 0)
192 if (__lstat ("/", &st) < 0)
197 while (!(thisdev == rootdev && thisino == rootino))
207 /* Look at the parent directory. */
209 fd = openat (fd, "..", O_RDONLY);
212 fd_needs_closing = true;
213 parent_status = fstat (fd, &st);
215 dotlist[dotlen++] = '.';
216 dotlist[dotlen++] = '.';
217 dotlist[dotlen] = '\0';
218 parent_status = __lstat (dotlist, &st);
220 if (parent_status != 0)
223 if (dirstream && __closedir (dirstream) != 0)
229 /* Figure out if this directory is a mount point. */
232 mount_point = dotdev != thisdev;
234 /* Search for the last directory. */
236 dirstream = fdopendir (fd);
237 if (dirstream == NULL)
239 fd_needs_closing = false;
241 dirstream = __opendir (dotlist);
242 if (dirstream == NULL)
244 dotlist[dotlen++] = '/';
248 /* Clear errno to distinguish EOF from error if readdir returns
251 d = __readdir (dirstream);
255 /* EOF on dirstream, which means that the current directory
257 __set_errno (ENOENT);
260 if (d->d_name[0] == '.' &&
261 (d->d_name[1] == '\0' ||
262 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
264 if (MATCHING_INO (d, thisino) || mount_point)
268 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
270 /* Compute size needed for this file name, or for the file
271 name ".." in the same directory, whichever is larger.
272 Room for ".." might be needed the next time through
274 size_t name_alloc = _D_ALLOC_NAMLEN (d);
275 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
277 if (filesize < dotlen)
278 goto memory_exhausted;
280 if (dotsize < filesize)
282 /* My, what a deep directory tree you have, Grandma. */
283 size_t newsize = MAX (filesize, dotsize * 2);
285 if (newsize < dotsize)
286 goto memory_exhausted;
289 dotlist = malloc (newsize);
304 strcpy (dotlist + dotlen, d->d_name);
305 entry_status = __lstat (dotlist, &st);
307 /* We don't fail here if we cannot stat() a directory entry.
308 This can happen when (network) file systems fail. If this
309 entry is in fact the one we are looking for we will find
310 out soon as we reach the end of the directory without
311 having found anything. */
312 if (entry_status == 0 && S_ISDIR (st.st_mode)
313 && st.st_dev == thisdev && st.st_ino == thisino)
318 dirroom = dirp - dir;
319 namlen = _D_EXACT_NAMLEN (d);
321 if (dirroom <= namlen)
325 __set_errno (ERANGE);
331 size_t oldsize = allocated;
333 allocated += MAX (allocated, namlen);
334 if (allocated < oldsize
335 || ! (tmp = realloc (dir, allocated)))
336 goto memory_exhausted;
338 /* Move current contents up to the end of the buffer.
339 This is guaranteed to be non-overlapping. */
340 dirp = memcpy (tmp + allocated - (oldsize - dirroom),
347 memcpy (dirp, d->d_name, namlen);
354 if (dirstream && __closedir (dirstream) != 0)
360 if (dirp == &dir[allocated - 1])
368 used = dir + allocated - dirp;
369 memmove (dir, dirp, used);
371 if (buf == NULL && size == 0)
372 /* Ensure that the buffer is only as large as necessary. */
373 buf = realloc (dir, used);
376 /* Either buf was NULL all along, or `realloc' failed but
377 we still have the original string. */
383 __set_errno (ENOMEM);
388 __closedir (dirstream);
390 if (fd_needs_closing)
404 weak_alias (__getcwd, getcwd)