1 /* $NetBSD: getcwd.c,v 1.53 2012/06/21 23:29:23 enami Exp $ */
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
40 __RCSID("$NetBSD: getcwd.c,v 1.53 2012/06/21 23:29:23 enami Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
58 __weak_alias(getcwd
,_getcwd
)
59 __weak_alias(_sys_getcwd
,_getcwd
)
60 __weak_alias(realpath
,_realpath
)
64 * char *realpath(const char *path, char *resolved);
66 * Find the real name of path, by removing all ".", ".." and symlink
67 * components. Returns (resolved) on success, or (NULL) on failure,
68 * in which case the path which caused trouble is left in (resolved).
71 realpath(const char * __restrict path
, char * __restrict resolved
)
74 int idx
= 0, nlnk
= 0;
76 char *p
, wbuf
[2][MAXPATHLEN
], *fres
;
80 /* POSIX sez we must test for this */
86 if (resolved
== NULL
) {
87 fres
= resolved
= malloc(MAXPATHLEN
);
95 * Build real path one by one with paying an attention to .,
96 * .. and symbolic link.
100 * `p' is where we'll put a new component with prepending
111 /* If relative path, start from current working directory. */
113 /* check for resolved pointer to appease coverity */
114 if (resolved
&& getcwd(resolved
, MAXPATHLEN
) == NULL
) {
119 len
= strlen(resolved
);
125 /* Skip any slash. */
136 /* Find the end of this component. */
140 while (*q
!= '/' && *q
!= '\0');
143 if (path
[0] == '.') {
148 if (path
[1] == '.' && q
- path
== 2) {
149 /* Trim the last component. */
158 /* Append this component. */
159 if (p
- resolved
+ 1 + q
- path
+ 1 > MAXPATHLEN
) {
160 errno
= ENAMETOOLONG
;
168 /* LINTED We know q > path. */
170 p
[1 + q
- path
] = '\0';
173 * If this component is a symlink, toss it and prepend link
174 * target to unresolved path.
176 if (lstat(resolved
, &sb
) == -1)
179 if (S_ISLNK(sb
.st_mode
)) {
180 if (nlnk
++ >= MAXSYMLINKS
) {
184 n
= readlink(resolved
, wbuf
[idx
], sizeof(wbuf
[0]) - 1);
192 /* Append unresolved path to link target and switch to it. */
193 if (n
+ (len
= strlen(q
)) + 1 > sizeof(wbuf
[0])) {
194 errno
= ENAMETOOLONG
;
197 memcpy(&wbuf
[idx
][n
], q
, len
+ 1);
201 /* If absolute symlink, start from root. */
206 if (*q
== '/' && !S_ISDIR(sb
.st_mode
)) {
211 /* Advance both resolved and unresolved path. */
221 __ssp_real(getcwd
)(char *pt
, size_t size
)
226 * If a buffer is specified, the size has to be non-zero.
230 /* __getcwd(pt, 0) results ERANGE. */
234 if (__getcwd(pt
, size
) >= 0)
240 * If no buffer specified by the user, allocate one as necessary.
244 if ((npt
= realloc(pt
, size
<<= 1)) == NULL
)
247 if (__getcwd(pt
, size
) >= 0)
249 } while (size
<= MAXPATHLEN
* 4 && errno
== ERANGE
);