1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2004 Free Software Foundation,
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
34 # include <fcntl.h> /* For AT_FDCWD on Solaris 9. */
38 # define __set_errno(val) (errno = (val))
41 #if HAVE_DIRENT_H || _LIBC
43 # ifndef _D_EXACT_NAMLEN
44 # define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
47 # define dirent direct
49 # include <sys/ndir.h>
58 #ifndef _D_EXACT_NAMLEN
59 # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
61 #ifndef _D_ALLOC_NAMLEN
62 # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
65 #if HAVE_UNISTD_H || _LIBC
74 # define mempcpy __mempcpy
83 # define MAX(a, b) ((a) < (b) ? (b) : (a))
86 # define MIN(a, b) ((a) < (b) ? (a) : (b))
91 # define PATH_MAX MAXPATHLEN
93 # define PATH_MAX 1024
98 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
100 # define MATCHING_INO(dp, ino) true
104 # define __getcwd getcwd
105 # define __lstat lstat
106 # define __closedir closedir
107 # define __opendir opendir
108 # define __readdir readdir
111 /* Get the pathname of the current working directory, and put it in SIZE
112 bytes of BUF. Returns NULL if the directory couldn't be determined or
113 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
114 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
115 unless SIZE == 0, in which case it is as big as necessary. */
118 __getcwd (char *buf, size_t size)
120 /* Lengths of big file name components and entire file names, and a
121 deep level of file name nesting. These numbers are not upper
122 bounds; they are merely large values suitable for initial
123 allocations, designed to be large enough for most real-world
127 BIG_FILE_NAME_COMPONENT_LENGTH = 255,
128 BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
134 bool fd_needs_closing = false;
136 char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
137 char *dotlist = dots;
138 size_t dotsize = sizeof dots;
141 DIR *dirstream = NULL;
142 dev_t rootdev, thisdev;
143 ino_t rootino, thisino;
145 register char *pathp;
147 int prev_errno = errno;
148 size_t allocated = size;
154 __set_errno (EINVAL);
158 allocated = BIG_FILE_NAME_LENGTH + 1;
165 path = malloc (allocated);
170 pathp = path + allocated;
173 if (__lstat (".", &st) < 0)
178 if (__lstat ("/", &st) < 0)
183 while (!(thisdev == rootdev && thisino == rootino))
191 /* Look at the parent directory. */
193 fd = openat (fd, "..", O_RDONLY);
196 fd_needs_closing = true;
197 parent_status = fstat (fd, &st);
199 dotlist[dotlen++] = '.';
200 dotlist[dotlen++] = '.';
201 dotlist[dotlen] = '\0';
202 parent_status = __lstat (dotlist, &st);
204 if (parent_status != 0)
207 if (dirstream && __closedir (dirstream) != 0)
213 /* Figure out if this directory is a mount point. */
216 mount_point = dotdev != thisdev;
218 /* Search for the last directory. */
220 dirstream = fdopendir (fd);
221 if (dirstream == NULL)
223 fd_needs_closing = false;
225 dirstream = __opendir (dotlist);
226 if (dirstream == NULL)
228 dotlist[dotlen++] = '/';
230 /* Clear errno to distinguish EOF from error if readdir returns
233 while ((d = __readdir (dirstream)) != NULL)
235 if (d->d_name[0] == '.' &&
236 (d->d_name[1] == '\0' ||
237 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
239 if (MATCHING_INO (d, thisino) || mount_point)
243 entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
245 /* Compute size needed for this file name, or for the file
246 name ".." in the same directory, whichever is larger.
247 Room for ".." might be needed the next time through
249 size_t name_alloc = _D_ALLOC_NAMLEN (d);
250 size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
252 if (filesize < dotlen)
253 goto memory_exhausted;
255 if (dotsize < filesize)
257 /* My, what a deep directory tree you have, Grandma. */
258 size_t newsize = MAX (filesize, dotsize * 2);
260 if (newsize < dotsize)
261 goto memory_exhausted;
264 dotlist = malloc (newsize);
279 strcpy (dotlist + dotlen, d->d_name);
280 entry_status = __lstat (dotlist, &st);
282 /* We don't fail here if we cannot stat() a directory entry.
283 This can happen when (network) file systems fail. If this
284 entry is in fact the one we are looking for we will find
285 out soon as we reach the end of the directory without
286 having found anything. */
287 if (entry_status == 0 && S_ISDIR (st.st_mode)
288 && st.st_dev == thisdev && st.st_ino == thisino)
295 /* EOF on dirstream, which means that the current directory
297 __set_errno (ENOENT);
302 size_t pathroom = pathp - path;
303 size_t namlen = _D_EXACT_NAMLEN (d);
305 if (pathroom <= namlen)
309 __set_errno (ERANGE);
315 size_t oldsize = allocated;
317 allocated += MAX (allocated, namlen);
318 if (allocated < oldsize
319 || ! (tmp = realloc (path, allocated)))
320 goto memory_exhausted;
322 /* Move current contents up to the end of the buffer.
323 This is guaranteed to be non-overlapping. */
324 pathp = memcpy (tmp + allocated - (oldsize - pathroom),
331 memcpy (pathp, d->d_name, namlen);
339 if (dirstream && __closedir (dirstream) != 0)
345 if (pathp == &path[allocated - 1])
353 memmove (path, pathp, path + allocated - pathp);
355 /* Restore errno on successful return. */
356 __set_errno (prev_errno);
361 __set_errno (ENOMEM);
366 __closedir (dirstream);
368 if (fd_needs_closing)
382 weak_alias (__getcwd, getcwd)