]>
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')))
54 * If __getcwd() ever becomes a syscall, we can remove this workaround.
55 * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN,
56 * if size is less than MAXPATHLEN, we need to use a temporary buffer
57 * and see if it fits. We also have to assume that open() or fcntl()
58 * don't fail with errno=ERANGE.
61 __getcwd(char *buf
, size_t size
)
67 if ((fd
= open(".", O_RDONLY
)) < 0)
69 if (fstat(fd
, &dot
) < 0) {
75 /* check that the device and inode are non-zero, otherwise punt */
76 if (dot
.st_dev
== 0 || dot
.st_ino
== 0) {
81 if (size
< MAXPATHLEN
) {
82 /* the hard case; allocate a buffer of size MAXPATHLEN to use */
83 b
= (char *)alloca(MAXPATHLEN
);
86 errno
= ENOMEM
; /* make sure it isn't ERANGE */
92 err
= fcntl(fd
, F_GETPATH
, b
);
101 * now double-check that the path returned by fcntl() has the same
102 * device and inode number as '.'.
104 if (stat(b
, &pt
) < 0)
107 * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
108 * separately test for pt.st_dev and pt.st_ino being non-zero, because
111 if (dot
.st_dev
!= pt
.st_dev
|| dot
.st_ino
!= pt
.st_ino
) {
116 * For the case where we allocated a buffer, check that it can fit
117 * in the real buffer, and copy it over.
119 if (size
< MAXPATHLEN
) {
120 if (strlen(b
) >= size
) {
129 __private_extern__
char *
130 __private_getcwd(pt
, size
, usegetpath
)
144 size_t ptsize
, upsize
;
146 char *ept
, *eup
, *up
;
149 * If no buffer specified by the user, allocate one as necessary.
150 * If a buffer is specified, the size has to be non-zero. The path
151 * is built from the end of the buffer backwards.
165 if ((pt
= malloc(ptsize
= MAXPATHLEN
)) == NULL
)
170 if (__getcwd(pt
, ept
- pt
) == 0) {
172 } else if (errno
== ERANGE
) /* failed because buffer too small */
179 * Allocate MAXPATHLEN bytes for the string of "../"'s.
180 * Should always be enough. If it's not, allocate
181 * as necessary. Special case the first stat, it's ".", not "..".
183 if ((up
= malloc(upsize
= MAXPATHLEN
)) == NULL
)
185 eup
= up
+ MAXPATHLEN
;
190 /* Save root values, so know when to stop. */
196 errno
= 0; /* XXX readdir has no error return. */
198 for (first
= 1;; first
= 0) {
199 /* Stat the current level. */
203 /* Save current node values. */
207 /* Check for reaching root. */
208 if (root_dev
== dev
&& root_ino
== ino
) {
211 * It's unclear that it's a requirement to copy the
212 * path to the beginning of the buffer, but it's always
213 * been that way and stuff would probably break.
215 bcopy(bpt
, pt
, ept
- bpt
);
221 * Build pointer to the parent directory, allocating memory
222 * as necessary. Max length is 3 for "../", the largest
223 * possible component name, plus a trailing NUL.
225 while (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
226 if ((up
= reallocf(up
, upsize
*= 2)) == NULL
)
235 /* Open and stat parent directory. */
236 if (!(dir
= opendir(up
)) || _fstat(dirfd(dir
), &s
))
239 /* Add trailing slash for next directory. */
244 * If it's a mount point, have to stat each element because
245 * the inode number in the directory is for the entry in the
246 * parent directory, not the inode number of the mounted file.
249 if (s
.st_dev
== dev
) {
251 if (!(dp
= readdir(dir
)))
253 if (dp
->d_fileno
== ino
)
258 if (!(dp
= readdir(dir
)))
262 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
264 /* Save the first error for later. */
271 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
276 * Check for length of the current name, preceding slash,
279 while (bpt
- pt
< dp
->d_namlen
+ (first
? 1 : 2)) {
288 if ((pt
= reallocf(pt
, ptsize
*= 2)) == NULL
)
292 bcopy(bpt
, ept
- len
, len
);
298 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
299 (void) closedir(dir
);
302 /* Truncate any file name. */
308 * If readdir set errno, use it, not any saved error; otherwise,
309 * didn't find the current directory in its parent directory, set
313 errno
= save_errno
? save_errno
: ENOENT
;
321 (void) closedir(dir
);
333 return __private_getcwd(pt
, size
, 1);