]>
git.saurik.com Git - apple/libc.git/blob - gen/getcwd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1989, 1991, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
70 static char *getcwd_physical
__P((char *, size_t));
73 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
74 dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
87 /* Check $PWD -- if it's right, it's fast. */
88 if ((pwd
= getenv("PWD")) != NULL
&& pwd
[0] == '/' && stat(pwd
, &s
) != -1) {
91 if (stat(".", &s
) != -1 && dev
== s
.st_dev
&& ino
== s
.st_ino
) {
98 if (pwdlen
+ 1 > size
) {
102 } else if ((pt
= malloc(pwdlen
+ 1)) == NULL
) {
106 memmove(pt
, pwd
, pwdlen
);
112 return (getcwd_physical(pt
, size
));
116 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
118 * Find the real name of path, by removing all ".", ".." and symlink
119 * components. Returns (resolved) on success, or (NULL) on failure,
120 * in which case the path which caused trouble is left in (resolved).
123 realpath(path
, resolved
)
128 int fd
, n
, rootd
, serrno
;
129 char *p
, *q
, wbuf
[MAXPATHLEN
];
132 /* Save the starting point. */
133 if ((fd
= open(".", O_RDONLY
)) < 0) {
134 (void)strcpy(resolved
, ".");
139 * Find the dirname and basename from the path to be resolved.
140 * Change directory to the dirname component.
141 * lstat the basename part.
142 * if it is a symlink, read in the value and loop.
143 * if it is a directory, then change to that directory.
144 * get the current directory name and append the basename.
146 (void)strncpy(resolved
, path
, MAXPATHLEN
- 1);
147 resolved
[MAXPATHLEN
- 1] = '\0';
149 q
= strrchr(resolved
, '/');
157 } while (q
> resolved
&& *q
== '/');
166 /* Deal with the last component. */
167 if (lstat(p
, &sb
) == 0) {
168 if (S_ISLNK(sb
.st_mode
)) {
169 if (++symlinks
> MAXSYMLINKS
) {
173 n
= readlink(p
, resolved
, MAXPATHLEN
-1);
179 if (S_ISDIR(sb
.st_mode
)) {
187 * Save the last component name and get the full pathname of
188 * the current directory.
190 (void)strcpy(wbuf
, p
);
193 * Call the inernal internal version of getcwd which
194 * does a physical search rather than using the $PWD short-cut
196 if (getcwd_physical(resolved
, MAXPATHLEN
) == 0)
200 * Join the two strings together, ensuring that the right thing
201 * happens if the last component is empty, or the dirname is root.
203 if (resolved
[0] == '/' && resolved
[1] == '\0')
209 if (strlen(resolved
) + strlen(wbuf
) + (1-rootd
) + 1 >
211 errno
= ENAMETOOLONG
;
215 (void)strcat(resolved
, "/");
216 (void)strcat(resolved
, wbuf
);
219 /* Go back to where we came from. */
220 if (fchdir(fd
) < 0) {
225 /* It's okay if the close fails, what's an fd more or less? */
229 err1
: serrno
= errno
;
231 err2
: (void)close(fd
);
237 getcwd_physical(pt
, size
)
241 register struct dirent
*dp
;
246 register char *bpt
, *bup
;
250 size_t ptsize
, upsize
;
252 char *ept
, *eup
, *up
;
255 * If no buffer specified by the user, allocate one as necessary.
256 * If a buffer is specified, the size has to be non-zero. The path
257 * is built from the end of the buffer backwards.
267 if ((pt
= malloc(ptsize
= 1024 - 4)) == NULL
)
275 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
276 * Should always be enough (it's 340 levels). If it's not, allocate
277 * as necessary. Special case the first stat, it's ".", not "..".
279 if ((up
= malloc(upsize
= 1024 - 4)) == NULL
)
281 eup
= up
+ MAXPATHLEN
;
286 /* Save root values, so know when to stop. */
292 errno
= 0; /* XXX readdir has no error return. */
294 for (first
= 1;; first
= 0) {
295 /* Stat the current level. */
299 /* Save current node values. */
303 /* Check for reaching root. */
304 if (root_dev
== dev
&& root_ino
== ino
) {
307 * It's unclear that it's a requirement to copy the
308 * path to the beginning of the buffer, but it's always
309 * been that way and stuff would probably break.
311 (void)bcopy(bpt
, pt
, ept
- bpt
);
317 * Build pointer to the parent directory, allocating memory
318 * as necessary. Max length is 3 for "../", the largest
319 * possible component name, plus a trailing NULL.
321 if (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
322 if ((up
= realloc(up
, upsize
*= 2)) == NULL
)
331 /* Open and stat parent directory. */
332 if (!(dir
= opendir(up
)) || fstat(dirfd(dir
), &s
))
335 /* Add trailing slash for next directory. */
339 * If it's a mount point, have to stat each element because
340 * the inode number in the directory is for the entry in the
341 * parent directory, not the inode number of the mounted file.
344 if (s
.st_dev
== dev
) {
346 if (!(dp
= readdir(dir
)))
348 if (dp
->d_fileno
== ino
)
353 if (!(dp
= readdir(dir
)))
357 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
359 /* Save the first error for later. */
366 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
371 * Check for length of the current name, preceding slash,
374 if (bpt
- pt
<= dp
->d_namlen
+ (first
? 1 : 2)) {
383 if ((pt
= realloc(pt
, ptsize
*= 2)) == NULL
)
387 (void)bcopy(bpt
, ept
- len
, len
);
393 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
396 /* Truncate any file name. */
402 * If readdir set errno, use it, not any saved error; otherwise,
403 * didn't find the current directory in its parent directory, set
407 errno
= save_errno
? save_errno
: ENOENT
;