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>
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 is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
85
# define is_ENAMETOOLONG(x) 0
89
# define MAX(a, b) ((a) < (b) ? (b) : (a))
92
# define MIN(a, b) ((a) < (b) ? (a) : (b))
97
# define PATH_MAX MAXPATHLEN
99
# define PATH_MAX 1024
104
# define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
106
# define MATCHING_INO(dp, ino) true
110
# define __getcwd getcwd
111
# define __lstat lstat
112
# define __closedir closedir
113
# define __opendir opendir
114
# define __readdir readdir
117
/* Get the name of the current working directory, and put it in SIZE
118
bytes of BUF. Returns NULL if the directory couldn't be determined or
119
SIZE was too small. If successful, returns BUF. In GNU, if BUF is
120
NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
121
unless SIZE == 0, in which case it is as big as necessary. */
124
__getcwd (char *buf, size_t size)
126
/* Lengths of big file name components and entire file names, and a
127
deep level of file name nesting. These numbers are not upper
128
bounds; they are merely large values suitable for initial
129
allocations, designed to be large enough for most real-world
133
BIG_FILE_NAME_COMPONENT_LENGTH = 255,
134
BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
140
bool fd_needs_closing = false;
142
char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
143
char *dotlist = dots;
144
size_t dotsize = sizeof dots;
147
DIR *dirstream = NULL;
148
dev_t rootdev, thisdev;
149
ino_t rootino, thisino;
153
size_t allocated = size;
156
#if HAVE_PARTLY_WORKING_GETCWD && !defined AT_FDCWD
157
/* The system getcwd works, except it sometimes fails when it
158
shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT. If
159
AT_FDCWD is not defined, the algorithm below is O(N**2) and this
160
is much slower than the system getcwd (at least on GNU/Linux).
161
So trust the system getcwd's results unless they look
164
dir = getcwd (buf, size);
165
if (dir || (errno != ERANGE && !is_ENAMETOOLONG (errno) && errno != ENOENT))
173
__set_errno (EINVAL);
177
allocated = BIG_FILE_NAME_LENGTH + 1;
182
dir = malloc (allocated);
189
dirp = dir + allocated;
192
if (__lstat (".", &st) < 0)
197
if (__lstat ("/", &st) < 0)
202
while (!(thisdev == rootdev && thisino == rootino))
210
/* Look at the parent directory. */
212
fd = openat (fd, "..", O_RDONLY);
215
fd_needs_closing = true;
216
parent_status = fstat (fd, &st);
218
dotlist[dotlen++] = '.';
219
dotlist[dotlen++] = '.';
220
dotlist[dotlen] = '\0';
221
parent_status = __lstat (dotlist, &st);
223
if (parent_status != 0)
226
if (dirstream && __closedir (dirstream) != 0)
232
/* Figure out if this directory is a mount point. */
235
mount_point = dotdev != thisdev;
237
/* Search for the last directory. */
239
dirstream = fdopendir (fd);
240
if (dirstream == NULL)
242
fd_needs_closing = false;
244
dirstream = __opendir (dotlist);
245
if (dirstream == NULL)
247
dotlist[dotlen++] = '/';
249
/* Clear errno to distinguish EOF from error if readdir returns
252
while ((d = __readdir (dirstream)) != NULL)
254
if (d->d_name[0] == '.' &&
255
(d->d_name[1] == '\0' ||
256
(d->d_name[1] == '.' && d->d_name[2] == '\0')))
258
if (MATCHING_INO (d, thisino) || mount_point)
262
entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
264
/* Compute size needed for this file name, or for the file
265
name ".." in the same directory, whichever is larger.
266
Room for ".." might be needed the next time through
268
size_t name_alloc = _D_ALLOC_NAMLEN (d);
269
size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
271
if (filesize < dotlen)
272
goto memory_exhausted;
274
if (dotsize < filesize)
276
/* My, what a deep directory tree you have, Grandma. */
277
size_t newsize = MAX (filesize, dotsize * 2);
279
if (newsize < dotsize)
280
goto memory_exhausted;
283
dotlist = malloc (newsize);
298
strcpy (dotlist + dotlen, d->d_name);
299
entry_status = __lstat (dotlist, &st);
301
/* We don't fail here if we cannot stat() a directory entry.
302
This can happen when (network) file systems fail. If this
303
entry is in fact the one we are looking for we will find
304
out soon as we reach the end of the directory without
305
having found anything. */
306
if (entry_status == 0 && S_ISDIR (st.st_mode)
307
&& st.st_dev == thisdev && st.st_ino == thisino)
314
/* EOF on dirstream, which means that the current directory
316
__set_errno (ENOENT);
321
size_t dirroom = dirp - dir;
322
size_t namlen = _D_EXACT_NAMLEN (d);
324
if (dirroom <= namlen)
328
__set_errno (ERANGE);
334
size_t oldsize = allocated;
336
allocated += MAX (allocated, namlen);
337
if (allocated < oldsize
338
|| ! (tmp = realloc (dir, allocated)))
339
goto memory_exhausted;
341
/* Move current contents up to the end of the buffer.
342
This is guaranteed to be non-overlapping. */
343
dirp = memcpy (tmp + allocated - (oldsize - dirroom),
350
memcpy (dirp, d->d_name, namlen);
358
if (dirstream && __closedir (dirstream) != 0)
364
if (dirp == &dir[allocated - 1])
372
used = dir + allocated - dirp;
373
memmove (dir, dirp, used);
375
if (buf == NULL && size == 0)
376
/* Ensure that the buffer is only as large as necessary. */
377
buf = realloc (dir, used);
380
/* Either buf was NULL all along, or `realloc' failed but
381
we still have the original string. */
387
__set_errno (ENOMEM);
392
__closedir (dirstream);
394
if (fd_needs_closing)
408
weak_alias (__getcwd, getcwd)