]>
git.saurik.com Git - apple/libc.git/blob - gen.subproj/getcwd.c
0c71e79f7d92da9f0fe5ae07c4a80b1a305e26d1
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1989, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/param.h>
67 static char *getcwd_physical
__P((char *, size_t));
70 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
71 dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
84 /* Check $PWD -- if it's right, it's fast. */
85 if ((pwd
= getenv("PWD")) != NULL
&& pwd
[0] == '/' && stat(pwd
, &s
) != -1) {
88 if (stat(".", &s
) != -1 && dev
== s
.st_dev
&& ino
== s
.st_ino
) {
95 if (pwdlen
+ 1 > size
) {
99 } else if ((pt
= malloc(pwdlen
+ 1)) == NULL
) {
103 memmove(pt
, pwd
, pwdlen
);
109 return (getcwd_physical(pt
, size
));
113 * char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
115 * Find the real name of path, by removing all ".", ".." and symlink
116 * components. Returns (resolved) on success, or (NULL) on failure,
117 * in which case the path which caused trouble is left in (resolved).
120 realpath(path
, resolved
)
125 int fd
, n
, rootd
, serrno
;
126 char *p
, *q
, wbuf
[MAXPATHLEN
];
129 /* Save the starting point. */
130 if ((fd
= open(".", O_RDONLY
)) < 0) {
131 (void)strcpy(resolved
, ".");
136 * Find the dirname and basename from the path to be resolved.
137 * Change directory to the dirname component.
138 * lstat the basename part.
139 * if it is a symlink, read in the value and loop.
140 * if it is a directory, then change to that directory.
141 * get the current directory name and append the basename.
143 (void)strncpy(resolved
, path
, MAXPATHLEN
- 1);
144 resolved
[MAXPATHLEN
- 1] = '\0';
146 q
= strrchr(resolved
, '/');
154 } while (q
> resolved
&& *q
== '/');
163 /* Deal with the last component. */
164 if (lstat(p
, &sb
) == 0) {
165 if (S_ISLNK(sb
.st_mode
)) {
166 if (++symlinks
> MAXSYMLINKS
) {
170 n
= readlink(p
, resolved
, MAXPATHLEN
-1);
176 if (S_ISDIR(sb
.st_mode
)) {
184 * Save the last component name and get the full pathname of
185 * the current directory.
187 (void)strcpy(wbuf
, p
);
190 * Call the inernal internal version of getcwd which
191 * does a physical search rather than using the $PWD short-cut
193 if (getcwd_physical(resolved
, MAXPATHLEN
) == 0)
197 * Join the two strings together, ensuring that the right thing
198 * happens if the last component is empty, or the dirname is root.
200 if (resolved
[0] == '/' && resolved
[1] == '\0')
206 if (strlen(resolved
) + strlen(wbuf
) + rootd
+ 1 > MAXPATHLEN
) {
207 errno
= ENAMETOOLONG
;
211 (void)strcat(resolved
, "/");
212 (void)strcat(resolved
, wbuf
);
215 /* Go back to where we came from. */
216 if (fchdir(fd
) < 0) {
221 /* It's okay if the close fails, what's an fd more or less? */
225 err1
: serrno
= errno
;
227 err2
: (void)close(fd
);
233 getcwd_physical(pt
, size
)
237 register struct dirent
*dp
;
242 register char *bpt
, *bup
;
246 size_t ptsize
, upsize
;
248 char *ept
, *eup
, *up
;
251 * If no buffer specified by the user, allocate one as necessary.
252 * If a buffer is specified, the size has to be non-zero. The path
253 * is built from the end of the buffer backwards.
263 if ((pt
= malloc(ptsize
= 1024 - 4)) == NULL
)
271 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
272 * Should always be enough (it's 340 levels). If it's not, allocate
273 * as necessary. Special case the first stat, it's ".", not "..".
275 if ((up
= malloc(upsize
= 1024 - 4)) == NULL
)
277 eup
= up
+ MAXPATHLEN
;
282 /* Save root values, so know when to stop. */
288 errno
= 0; /* XXX readdir has no error return. */
290 for (first
= 1;; first
= 0) {
291 /* Stat the current level. */
295 /* Save current node values. */
299 /* Check for reaching root. */
300 if (root_dev
== dev
&& root_ino
== ino
) {
303 * It's unclear that it's a requirement to copy the
304 * path to the beginning of the buffer, but it's always
305 * been that way and stuff would probably break.
307 (void)bcopy(bpt
, pt
, ept
- bpt
);
313 * Build pointer to the parent directory, allocating memory
314 * as necessary. Max length is 3 for "../", the largest
315 * possible component name, plus a trailing NULL.
317 if (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
318 if ((up
= realloc(up
, upsize
*= 2)) == NULL
)
327 /* Open and stat parent directory. */
328 if (!(dir
= opendir(up
)) || fstat(dirfd(dir
), &s
))
331 /* Add trailing slash for next directory. */
335 * If it's a mount point, have to stat each element because
336 * the inode number in the directory is for the entry in the
337 * parent directory, not the inode number of the mounted file.
340 if (s
.st_dev
== dev
) {
342 if (!(dp
= readdir(dir
)))
344 if (dp
->d_fileno
== ino
)
349 if (!(dp
= readdir(dir
)))
353 bcopy(dp
->d_name
, bup
, dp
->d_namlen
+ 1);
355 /* Save the first error for later. */
362 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
367 * Check for length of the current name, preceding slash,
370 if (bpt
- pt
<= dp
->d_namlen
+ (first
? 1 : 2)) {
379 if ((pt
= realloc(pt
, ptsize
*= 2)) == NULL
)
383 (void)bcopy(bpt
, ept
- len
, len
);
389 bcopy(dp
->d_name
, bpt
, dp
->d_namlen
);
392 /* Truncate any file name. */
398 * If readdir set errno, use it, not any saved error; otherwise,
399 * didn't find the current directory in its parent directory, set
403 errno
= save_errno
? save_errno
: ENOENT
;