]>
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 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Wstrict-prototypes"
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.29 2007/01/09 00:27:53 imp Exp $");
39 #include "namespace.h"
40 #include <sys/param.h>
50 #include "un-namespace.h"
53 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
54 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
57 * If __getcwd() ever becomes a syscall, we can remove this workaround.
58 * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN,
59 * if size is less than MAXPATHLEN, we need to use a temporary buffer
60 * and see if it fits. We also have to assume that open() or fcntl()
61 * don't fail with errno=ERANGE.
64 __getcwd(char *buf
, size_t size
)
70 if ((fd
= open(".", O_RDONLY
)) < 0)
72 if (fstat(fd
, &dot
) < 0) {
78 /* check that the device and inode are non-zero, otherwise punt */
79 if (dot
.st_dev
== 0 || dot
.st_ino
== 0) {
84 if (size
< MAXPATHLEN
) {
85 /* the hard case; allocate a buffer of size MAXPATHLEN to use */
86 b
= (char *)alloca(MAXPATHLEN
);
89 errno
= ENOMEM
; /* make sure it isn't ERANGE */
95 err
= fcntl(fd
, F_GETPATH
, b
);
104 * now double-check that the path returned by fcntl() has the same
105 * device and inode number as '.'.
107 if (stat(b
, &pt
) < 0)
110 * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
111 * separately test for pt.st_dev and pt.st_ino being non-zero, because
114 if (dot
.st_dev
!= pt
.st_dev
|| dot
.st_ino
!= pt
.st_ino
) {
119 * For the case where we allocated a buffer, check that it can fit
120 * in the real buffer, and copy it over.
122 if (size
< MAXPATHLEN
) {
123 if (strlen(b
) >= size
) {
132 __private_extern__
char *
133 __private_getcwd(pt
, size
, usegetpath
)
147 size_t ptsize
, upsize
;
149 char *ept
, *eup
, *up
;
152 * If no buffer specified by the user, allocate one as necessary.
153 * If a buffer is specified, the size has to be non-zero. The path
154 * is built from the end of the buffer backwards.
168 if ((pt
= malloc(ptsize
= MAXPATHLEN
)) == NULL
)
173 if (__getcwd(pt
, ept
- pt
) == 0) {
175 } else if (errno
== ERANGE
) /* failed because buffer too small */
182 * Allocate MAXPATHLEN bytes for the string of "../"'s.
183 * Should always be enough. If it's not, allocate
184 * as necessary. Special case the first stat, it's ".", not "..".
186 if ((up
= malloc(upsize
= MAXPATHLEN
)) == NULL
)
188 eup
= up
+ MAXPATHLEN
;
193 /* Save root values, so know when to stop. */
199 errno
= 0; /* XXX readdir has no error return. */
201 for (first
= 1;; first
= 0) {
202 /* Stat the current level. */
206 /* Save current node values. */
210 /* Check for reaching root. */
211 if (root_dev
== dev
&& root_ino
== ino
) {
214 * It's unclear that it's a requirement to copy the
215 * path to the beginning of the buffer, but it's always
216 * been that way and stuff would probably break.
218 bcopy(bpt
, pt
, ept
- bpt
);
224 * Build pointer to the parent directory, allocating memory
225 * as necessary. Max length is 3 for "../", the largest
226 * possible component name, plus a trailing NUL.
228 while (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
229 if ((up
= reallocf(up
, upsize
*= 2)) == NULL
)
238 /* Open and stat parent directory. */
239 if (!(dir
= opendir(up
)) || _fstat(dirfd(dir
), &s
))
242 /* Add trailing slash for next directory. */
247 * If it's a mount point, have to stat each element because
248 * the inode number in the directory is for the entry in the
249 * parent directory, not the inode number of the mounted file.
252 if (s
.st_dev
== dev
) {
254 if (!(dp
= readdir(dir
)))
256 if (dp
->d_fileno
== ino
)
261 if (!(dp
= readdir(dir
)))
265 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
267 /* Save the first error for later. */
274 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
279 * Check for length of the current name, preceding slash,
282 while (bpt
- pt
< dp
->d_namlen
+ (first
? 1 : 2)) {
291 if ((pt
= reallocf(pt
, ptsize
*= 2)) == NULL
)
295 bcopy(bpt
, ept
- len
, len
);
301 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
302 (void) closedir(dir
);
305 /* Truncate any file name. */
311 * If readdir set errno, use it, not any saved error; otherwise,
312 * didn't find the current directory in its parent directory, set
316 errno
= save_errno
? save_errno
: ENOENT
;
324 (void) closedir(dir
);
336 return __private_getcwd(pt
, size
, 1);
338 #pragma clang diagnostic pop