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"
52 #if TARGET_OS_OSX && !TARGET_OS_SIMULATOR
53 #include <sys/attr.h> /* for FSOPT_NOFOLLOW */
54 #include <apfs/apfs_fsctl.h>
58 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
59 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
62 * Check if the given directory is a firmlink.
63 * Return 1 if it is firmlink otherwise return 0.
66 __check_for_firmlink(char *dir_path
)
68 #if TARGET_OS_OSX && !TARGET_OS_SIMULATOR
69 apfs_firmlink_control_t afc
;
73 afc
.cmd
= FIRMLINK_GET
;
76 err
= fsctl(dir_path
, APFSIOC_FIRMLINK_CTL
, (void *)&afc
, FSOPT_NOFOLLOW
);
78 is_firmlink
= afc
.val
? 1 : 0;
81 * On error, we assume it is a firmlink. This could be a false positive
82 * and we will end up incurring a lstat() cost but it will give us some
83 * chance to build the path successfully.
95 * If __getcwd() ever becomes a syscall, we can remove this workaround.
96 * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN,
97 * if size is less than MAXPATHLEN, we need to use a temporary buffer
98 * and see if it fits. We also have to assume that open() or fcntl()
99 * don't fail with errno=ERANGE.
102 __getcwd(char *buf
, size_t size
)
108 if ((fd
= open(".", O_RDONLY
)) < 0)
110 if (fstat(fd
, &dot
) < 0) {
116 /* check that the device and inode are non-zero, otherwise punt */
117 if (dot
.st_dev
== 0 || dot
.st_ino
== 0) {
122 if (size
< MAXPATHLEN
) {
123 /* the hard case; allocate a buffer of size MAXPATHLEN to use */
124 b
= (char *)alloca(MAXPATHLEN
);
127 errno
= ENOMEM
; /* make sure it isn't ERANGE */
133 err
= fcntl(fd
, F_GETPATH
, b
);
142 * now double-check that the path returned by fcntl() has the same
143 * device and inode number as '.'.
145 if (stat(b
, &pt
) < 0)
148 * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
149 * separately test for pt.st_dev and pt.st_ino being non-zero, because
152 if (dot
.st_dev
!= pt
.st_dev
|| dot
.st_ino
!= pt
.st_ino
) {
157 * For the case where we allocated a buffer, check that it can fit
158 * in the real buffer, and copy it over.
160 if (size
< MAXPATHLEN
) {
161 if (strlen(b
) >= size
) {
170 __private_extern__
char *
171 __private_getcwd(pt
, size
, usegetpath
)
185 size_t ptsize
, upsize
;
187 char *ept
, *eup
, *up
;
190 * If no buffer specified by the user, allocate one as necessary.
191 * If a buffer is specified, the size has to be non-zero. The path
192 * is built from the end of the buffer backwards.
206 if ((pt
= malloc(ptsize
= MAXPATHLEN
)) == NULL
)
211 if (__getcwd(pt
, ept
- pt
) == 0) {
213 } else if (errno
== ERANGE
) /* failed because buffer too small */
220 * Allocate MAXPATHLEN bytes for the string of "../"'s.
221 * Should always be enough. If it's not, allocate
222 * as necessary. Special case the first stat, it's ".", not "..".
224 if ((up
= malloc(upsize
= MAXPATHLEN
)) == NULL
)
226 eup
= up
+ MAXPATHLEN
;
231 /* Save root values, so know when to stop. */
237 errno
= 0; /* XXX readdir has no error return. */
239 for (first
= 1;; first
= 0) {
240 /* Stat the current level. */
244 /* Save current node values. */
248 /* Check for reaching root. */
249 if (root_dev
== dev
&& root_ino
== ino
) {
252 * It's unclear that it's a requirement to copy the
253 * path to the beginning of the buffer, but it's always
254 * been that way and stuff would probably break.
256 bcopy(bpt
, pt
, ept
- bpt
);
262 * Build pointer to the parent directory, allocating memory
263 * as necessary. Max length is 3 for "../", the largest
264 * possible component name, plus a trailing NUL.
266 while (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
267 if ((up
= reallocf(up
, upsize
*= 2)) == NULL
)
276 /* Open and stat parent directory. */
277 if (!(dir
= opendir(up
)) || _fstat(dirfd(dir
), &s
))
280 /* Add trailing slash for next directory. */
285 * If it's a mount point, have to stat each element because
286 * the inode number in the directory is for the entry in the
287 * parent directory, not the inode number of the mounted file.
290 if (s
.st_dev
== dev
) {
292 if (!(dp
= readdir(dir
)))
294 if (dp
->d_fileno
== ino
) {
296 } else if (!ISDOT(dp
) && dp
->d_type
== DT_DIR
) {
298 * The 'd_fileno' for firmlink directory would be different
299 * than the 'st_ino' returned by stat(). We have to do an
300 * extra lstat() on firmlink directory to determine if the
301 * 'st_ino' matches with what we are looking for.
303 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
305 if (__check_for_firmlink(up
) == 0)
314 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
320 if (!(dp
= readdir(dir
)))
324 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
326 /* Save the first error for later. */
333 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
338 * Check for length of the current name, preceding slash,
341 while (bpt
- pt
< dp
->d_namlen
+ (first
? 1 : 2)) {
350 if ((pt
= reallocf(pt
, ptsize
*= 2)) == NULL
)
354 bcopy(bpt
, ept
- len
, len
);
360 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
361 (void) closedir(dir
);
364 /* Truncate any file name. */
370 * If readdir set errno, use it, not any saved error; otherwise,
371 * didn't find the current directory in its parent directory, set
375 errno
= save_errno
? save_errno
: ENOENT
;
383 (void) closedir(dir
);
395 return __private_getcwd(pt
, size
, 1);
397 #pragma clang diagnostic pop