2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
8
* Copyright (c) 1989, 1991, 1993
9
* The Regents of the University of California. All rights reserved.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of the University nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#include "db_config.h"
39
static const char revid[] = "$Id$";
42
#ifndef NO_SYSTEM_INCLUDES
43
#include <sys/types.h>
48
# define NAMLEN(dirent) strlen((dirent)->d_name)
50
# define dirent direct
51
# define NAMLEN(dirent) (dirent)->d_namlen
53
# include <sys/ndir.h>
72
(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
73
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
76
#define dirfd(dirp) ((dirp)->dd_fd)
81
* Get the current working directory.
83
* PUBLIC: #ifndef HAVE_GETCWD
84
* PUBLIC: char *getcwd __P((char *, size_t));
92
register struct dirent *dp;
97
register char *bpt, *bup;
101
size_t ptsize, upsize;
103
char *ept, *eup, *up;
106
* If no buffer specified by the user, allocate one as necessary.
107
* If a buffer is specified, the size has to be non-zero. The path
108
* is built from the end of the buffer backwards.
113
__os_set_errno(EINVAL);
117
__os_set_errno(ERANGE);
123
__os_malloc(NULL, ptsize = 1024 - 4, &pt)) != 0) {
133
* Allocate bytes (1024 - malloc space) for the string of "../"'s.
134
* Should always be enough (it's 340 levels). If it's not, allocate
135
* as necessary. Special case the first stat, it's ".", not "..".
137
if ((ret = __os_malloc(NULL, upsize = 1024 - 4, &up)) != 0)
144
/* Save root values, so know when to stop. */
150
__os_set_errno(0); /* XXX readdir has no error return. */
152
for (first = 1;; first = 0) {
153
/* Stat the current level. */
157
/* Save current node values. */
161
/* Check for reaching root. */
162
if (root_dev == dev && root_ino == ino) {
163
*--bpt = PATH_SEPARATOR[0];
165
* It's unclear that it's a requirement to copy the
166
* path to the beginning of the buffer, but it's always
167
* been that way and stuff would probably break.
169
bcopy(bpt, pt, ept - bpt);
175
* Build pointer to the parent directory, allocating memory
176
* as necessary. Max length is 3 for "../", the largest
177
* possible component name, plus a trailing NULL.
179
if (bup + 3 + MAXNAMLEN + 1 >= eup) {
180
if (__os_realloc(NULL, upsize *= 2, &up) != 0)
189
/* Open and stat parent directory. */
190
if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
193
/* Add trailing slash for next directory. */
194
*bup++ = PATH_SEPARATOR[0];
197
* If it's a mount point, have to stat each element because
198
* the inode number in the directory is for the entry in the
199
* parent directory, not the inode number of the mounted file.
202
if (s.st_dev == dev) {
204
if (!(dp = readdir(dir)))
206
if (dp->d_fileno == ino)
211
if (!(dp = readdir(dir)))
215
bcopy(dp->d_name, bup, dp->d_namlen + 1);
217
/* Save the first error for later. */
220
save_errno = __os_get_errno();
224
if (s.st_dev == dev && s.st_ino == ino)
229
* Check for length of the current name, preceding slash,
232
if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
236
__os_set_errno(ERANGE);
241
if (__os_realloc(NULL, ptsize *= 2, &pt) != 0)
245
bcopy(bpt, ept - len, len);
249
*--bpt = PATH_SEPARATOR[0];
251
bcopy(dp->d_name, bpt, dp->d_namlen);
254
/* Truncate any file name. */
260
* If readdir set errno, use it, not any saved error; otherwise,
261
* didn't find the current directory in its parent directory, set
264
if (__os_get_errno_ret_zero() == 0)
265
__os_set_errno(save_errno == 0 ? ENOENT : save_errno);