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)
63 #if HAVE_UNISTD_H || _LIBC
72 # define mempcpy __mempcpy
81 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
83 # define is_ENAMETOOLONG(x) 0
87 # define MAX(a, b) ((a) < (b) ? (b) : (a))
90 # define MIN(a, b) ((a) < (b) ? (a) : (b))
95 # define PATH_MAX MAXPATHLEN
97 # define PATH_MAX 1024
102 # define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
104 # define MATCHING_INO(dp, ino) true
108 # define __getcwd getcwd
109 # define __lstat lstat
110 # define __closedir closedir
111 # define __opendir opendir
112 # define __readdir readdir
115 /* Get the name of the current working directory, and put it in SIZE
116 bytes of BUF. Returns NULL if the directory couldn't be determined or
117 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
118 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
119 unless SIZE == 0, in which case it is as big as necessary. */
122 __getcwd (char *buf
, size_t size
)
124 /* Lengths of big file name components and entire file names, and a
125 deep level of file name nesting. These numbers are not upper
126 bounds; they are merely large values suitable for initial
127 allocations, designed to be large enough for most real-world
131 BIG_FILE_NAME_COMPONENT_LENGTH
= 255,
132 BIG_FILE_NAME_LENGTH
= MIN (4095, PATH_MAX
- 1),
138 bool fd_needs_closing
= false;
140 char dots
[DEEP_NESTING
* sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH
+ 1];
141 char *dotlist
= dots
;
142 size_t dotsize
= sizeof dots
;
145 DIR *dirstream
= NULL
;
146 dev_t rootdev
, thisdev
;
147 ino_t rootino
, thisino
;
151 size_t allocated
= size
;
154 #if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD
155 /* The system getcwd works, except it sometimes fails when it
156 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
157 AT_FDCWD is not defined, the algorithm below is O(N**2) and this
158 is much slower than the system getcwd (at least on GNU/Linux).
159 So trust the system getcwd's results unless they look
162 dir
= getcwd (buf
, size
);
163 if (dir
|| (errno
!= ERANGE
&& !is_ENAMETOOLONG (errno
) && errno
!= ENOENT
))
171 __set_errno (EINVAL
);
175 allocated
= BIG_FILE_NAME_LENGTH
+ 1;
180 dir
= malloc (allocated
);
187 dirp
= dir
+ allocated
;
190 if (__lstat (".", &st
) < 0)
195 if (__lstat ("/", &st
) < 0)
200 while (!(thisdev
== rootdev
&& thisino
== rootino
))
208 /* Look at the parent directory. */
210 fd
= openat (fd
, "..", O_RDONLY
);
213 fd_needs_closing
= true;
214 parent_status
= fstat (fd
, &st
);
216 dotlist
[dotlen
++] = '.';
217 dotlist
[dotlen
++] = '.';
218 dotlist
[dotlen
] = '\0';
219 parent_status
= __lstat (dotlist
, &st
);
221 if (parent_status
!= 0)
224 if (dirstream
&& __closedir (dirstream
) != 0)
230 /* Figure out if this directory is a mount point. */
233 mount_point
= dotdev
!= thisdev
;
235 /* Search for the last directory. */
237 dirstream
= fdopendir (fd
);
238 if (dirstream
== NULL
)
240 fd_needs_closing
= false;
242 dirstream
= __opendir (dotlist
);
243 if (dirstream
== NULL
)
245 dotlist
[dotlen
++] = '/';
247 /* Clear errno to distinguish EOF from error if readdir returns
250 while ((d
= __readdir (dirstream
)) != NULL
)
252 if (d
->d_name
[0] == '.' &&
253 (d
->d_name
[1] == '\0' ||
254 (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
256 if (MATCHING_INO (d
, thisino
) || mount_point
)
260 entry_status
= fstatat (fd
, d
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
);
262 /* Compute size needed for this file name, or for the file
263 name ".." in the same directory, whichever is larger.
264 Room for ".." might be needed the next time through
266 size_t name_alloc
= _D_ALLOC_NAMLEN (d
);
267 size_t filesize
= dotlen
+ MAX (sizeof "..", name_alloc
);
269 if (filesize
< dotlen
)
270 goto memory_exhausted
;
272 if (dotsize
< filesize
)
274 /* My, what a deep directory tree you have, Grandma. */
275 size_t newsize
= MAX (filesize
, dotsize
* 2);
277 if (newsize
< dotsize
)
278 goto memory_exhausted
;
281 dotlist
= malloc (newsize
);
296 strcpy (dotlist
+ dotlen
, d
->d_name
);
297 entry_status
= __lstat (dotlist
, &st
);
299 /* We don't fail here if we cannot stat() a directory entry.
300 This can happen when (network) file systems fail. If this
301 entry is in fact the one we are looking for we will find
302 out soon as we reach the end of the directory without
303 having found anything. */
304 if (entry_status
== 0 && S_ISDIR (st
.st_mode
)
305 && st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
312 /* EOF on dirstream, which means that the current directory
314 __set_errno (ENOENT
);
319 size_t dirroom
= dirp
- dir
;
320 size_t namlen
= _D_EXACT_NAMLEN (d
);
322 if (dirroom
<= namlen
)
326 __set_errno (ERANGE
);
332 size_t oldsize
= allocated
;
334 allocated
+= MAX (allocated
, namlen
);
335 if (allocated
< oldsize
336 || ! (tmp
= realloc (dir
, allocated
)))
337 goto memory_exhausted
;
339 /* Move current contents up to the end of the buffer.
340 This is guaranteed to be non-overlapping. */
341 dirp
= memcpy (tmp
+ allocated
- (oldsize
- dirroom
),
348 memcpy (dirp
, d
->d_name
, namlen
);
356 if (dirstream
&& __closedir (dirstream
) != 0)
362 if (dirp
== &dir
[allocated
- 1])
370 used
= dir
+ allocated
- dirp
;
371 memmove (dir
, dirp
, used
);
373 if (buf
== NULL
&& size
== 0)
374 /* Ensure that the buffer is only as large as necessary. */
375 buf
= realloc (dir
, used
);
378 /* Either buf was NULL all along, or `realloc' failed but
379 we still have the original string. */
385 __set_errno (ENOMEM
);
390 __closedir (dirstream
);
392 if (fd_needs_closing
)
406 weak_alias (__getcwd
, getcwd
)