]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/getcwd.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid
[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.29 2007/01/09 00:27:53 imp Exp $");
36 #include "namespace.h"
37 #include <sys/param.h>
47 #include "un-namespace.h"
50 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
51 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
53 extern int __getcwd(char *, size_t);
69 size_t ptsize
, upsize
;
71 char *ept
, *eup
, *up
, c
;
74 * If no buffer specified by the user, allocate one as necessary.
75 * If a buffer is specified, the size has to be non-zero. The path
76 * is built from the end of the buffer backwards.
90 if ((pt
= malloc(ptsize
= PATH_MAX
)) == NULL
)
94 if (__getcwd(pt
, ept
- pt
) == 0) {
97 ept
= pt
+ strlen(pt
) - 1;
110 * Allocate 1024 bytes for the string of "../"'s.
111 * Should always be enough. If it's not, allocate
112 * as necessary. Special case the first stat, it's ".", not "..".
114 if ((up
= malloc(upsize
= 1024)) == NULL
)
121 /* Save root values, so know when to stop. */
127 errno
= 0; /* XXX readdir has no error return. */
129 for (first
= 1;; first
= 0) {
130 /* Stat the current level. */
134 /* Save current node values. */
138 /* Check for reaching root. */
139 if (root_dev
== dev
&& root_ino
== ino
) {
142 * It's unclear that it's a requirement to copy the
143 * path to the beginning of the buffer, but it's always
144 * been that way and stuff would probably break.
146 bcopy(bpt
, pt
, ept
- bpt
);
152 * Build pointer to the parent directory, allocating memory
153 * as necessary. Max length is 3 for "../", the largest
154 * possible component name, plus a trailing NUL.
156 while (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
157 if ((up
= reallocf(up
, upsize
*= 2)) == NULL
)
166 /* Open and stat parent directory. */
167 if (!(dir
= opendir(up
)) || _fstat(dirfd(dir
), &s
))
170 /* Add trailing slash for next directory. */
175 * If it's a mount point, have to stat each element because
176 * the inode number in the directory is for the entry in the
177 * parent directory, not the inode number of the mounted file.
180 if (s
.st_dev
== dev
) {
182 if (!(dp
= readdir(dir
)))
184 if (dp
->d_fileno
== ino
)
189 if (!(dp
= readdir(dir
)))
193 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
195 /* Save the first error for later. */
202 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
207 * Check for length of the current name, preceding slash,
210 while (bpt
- pt
< dp
->d_namlen
+ (first
? 1 : 2)) {
219 if ((pt
= reallocf(pt
, ptsize
*= 2)) == NULL
)
223 bcopy(bpt
, ept
- len
, len
);
229 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
230 (void) closedir(dir
);
233 /* Truncate any file name. */
239 * If readdir set errno, use it, not any saved error; otherwise,
240 * didn't find the current directory in its parent directory, set
244 errno
= save_errno
? save_errno
: ENOENT
;
252 (void) closedir(dir
);