2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libc/gen/getcwd.c,v 1.18 1999/09/28 13:24:13 marcel Exp $
34 * $DragonFly: src/lib/libcr/gen/Attic/getcwd.c,v 1.2 2003/06/17 04:26:42 dillon Exp $
36 * @(#)getcwd.c 8.5 (Berkeley) 2/7/95
39 #include <sys/param.h>
52 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
53 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
55 static int have__getcwd = 1; /* 0 = no, 1 = perhaps, 2 = yes */
62 register struct dirent *dp;
63 register DIR *dir = NULL;
67 register char *bpt, *bup;
71 size_t ptsize, upsize;
73 char *ept, *eup, *up, c;
76 * If no buffer specified by the user, allocate one as necessary.
77 * If a buffer is specified, the size has to be non-zero. The path
78 * is built from the end of the buffer backwards.
92 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
96 #if defined(__NETBSD_SYSCALLS)
100 struct sigaction sa, osa;
101 int sigsys_installed = 0;
104 if (have__getcwd == 1) { /* unsure? */
105 sigemptyset(&sa.sa_mask);
107 sa.sa_handler = SIG_IGN;
108 if (sigaction(SIGSYS, &sa, &osa) >= 0)
109 sigsys_installed = 1;
111 ret = __getcwd(pt, ept - pt);
112 if (sigsys_installed == 1) {
114 sigaction(SIGSYS, &osa, NULL);
117 /* XXX a bogus syscall seems to return EINVAL(!) */
118 if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
120 else if (have__getcwd == 1)
121 have__getcwd = 2; /* yep, remember we have it */
125 ept = pt + strlen(pt) - 1;
140 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
141 * Should always be enough (it's 340 levels). If it's not, allocate
142 * as necessary. Special case the first stat, it's ".", not "..".
144 if ((up = malloc(upsize = 1024 - 4)) == NULL)
146 eup = up + MAXPATHLEN;
151 /* Save root values, so know when to stop. */
157 errno = 0; /* XXX readdir has no error return. */
159 for (first = 1;; first = 0) {
160 /* Stat the current level. */
164 /* Save current node values. */
168 /* Check for reaching root. */
169 if (root_dev == dev && root_ino == ino) {
172 * It's unclear that it's a requirement to copy the
173 * path to the beginning of the buffer, but it's always
174 * been that way and stuff would probably break.
176 bcopy(bpt, pt, ept - bpt);
182 * Build pointer to the parent directory, allocating memory
183 * as necessary. Max length is 3 for "../", the largest
184 * possible component name, plus a trailing NULL.
186 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
187 if ((up = reallocf(up, upsize *= 2)) == NULL)
196 /* Open and stat parent directory. */
197 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
200 /* Add trailing slash for next directory. */
205 * If it's a mount point, have to stat each element because
206 * the inode number in the directory is for the entry in the
207 * parent directory, not the inode number of the mounted file.
210 if (s.st_dev == dev) {
212 if (!(dp = readdir(dir)))
214 if (dp->d_fileno == ino)
219 if (!(dp = readdir(dir)))
223 bcopy(dp->d_name, bup, dp->d_namlen + 1);
225 /* Save the first error for later. */
232 if (s.st_dev == dev && s.st_ino == ino)
237 * Check for length of the current name, preceding slash,
240 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
249 if ((pt = realloc(pt, ptsize *= 2)) == NULL)
253 bcopy(bpt, ept - len, len);
259 bcopy(dp->d_name, bpt, dp->d_namlen);
260 (void) closedir(dir);
263 /* Truncate any file name. */
269 * If readdir set errno, use it, not any saved error; otherwise,
270 * didn't find the current directory in its parent directory, set
274 errno = save_errno ? save_errno : ENOENT;
282 (void) closedir(dir);