4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The names of the authors may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/realpath.c
57 * $OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $
65 #include <sys/types.h>
67 #include <sys/param.h>
70 * char *s_realpath(const char *path, char resolved_path[MAXPATHLEN]);
72 * Find the real name of path, by removing all ".", ".." and symlink
73 * components. Returns (resolved) on success, or (NULL) on failure,
74 * in which case the path which caused trouble is left in (resolved).
76 * DEVINFO: For libdevinfo we have added code to special case symlinks into
77 * /devices - the path below that point is known to not point to any
78 * additional symlinks. This knowledge allows us to avoid causing attach.
81 s_realpath(const char *path
, char *resolved
)
85 size_t left_len
, resolved_len
;
88 char left
[PATH_MAX
], next_token
[PATH_MAX
], symlink
[PATH_MAX
];
98 left_len
= strlcpy(left
, path
+ 1, sizeof (left
));
100 if (getcwd(resolved
, PATH_MAX
) == NULL
) {
101 (void) strlcpy(resolved
, ".", PATH_MAX
);
104 resolved_len
= strlen(resolved
);
105 left_len
= strlcpy(left
, path
, sizeof (left
));
107 if (left_len
>= sizeof (left
) || resolved_len
>= PATH_MAX
) {
108 errno
= ENAMETOOLONG
;
113 * Iterate over path components in `left'.
115 while (left_len
!= 0) {
117 * Extract the next path component and adjust `left'
120 p
= strchr(left
, '/');
121 s
= p
? p
: left
+ left_len
;
122 if (s
- left
>= sizeof (next_token
)) {
123 errno
= ENAMETOOLONG
;
126 (void) memcpy(next_token
, left
, s
- left
);
127 next_token
[s
- left
] = '\0';
128 left_len
-= s
- left
;
130 (void) memmove(left
, s
+ 1, left_len
+ 1);
131 if (resolved
[resolved_len
- 1] != '/') {
132 if (resolved_len
+ 1 >= PATH_MAX
) {
133 errno
= ENAMETOOLONG
;
136 resolved
[resolved_len
++] = '/';
137 resolved
[resolved_len
] = '\0';
139 if (next_token
[0] == '\0')
141 else if (strcmp(next_token
, ".") == 0)
143 else if (strcmp(next_token
, "..") == 0) {
145 * Strip the last path component except when we have
148 if (resolved_len
> 1) {
149 resolved
[resolved_len
- 1] = '\0';
150 q
= strrchr(resolved
, '/') + 1;
152 resolved_len
= q
- resolved
;
158 * Append the next path component and lstat() it. If
159 * lstat() fails we still can return successfully if
160 * there are no more path components left.
162 resolved_len
= strlcat(resolved
, next_token
, PATH_MAX
);
163 if (resolved_len
>= PATH_MAX
) {
164 errno
= ENAMETOOLONG
;
169 * DEVINFO: Check if link points into /devices and resolve
170 * without causing attach if that is the case - there are no
171 * further symlinks in /devices.
173 if (strcmp(resolved
, "/devices") == 0) {
174 resolved
[resolved_len
] = '/';
175 resolved_len
= strlcat(resolved
, left
, sizeof (left
));
180 if (lstat(resolved
, &sb
) != 0) {
181 if (errno
== ENOENT
&& p
== NULL
) {
188 if (S_ISLNK(sb
.st_mode
)) {
189 if (symlinks
++ > MAXSYMLINKS
) {
193 slen
= readlink(resolved
, symlink
,
194 sizeof (symlink
) - 1);
197 symlink
[slen
] = '\0';
199 if (symlink
[0] == '/') {
202 } else if (resolved_len
> 1) {
203 /* Strip the last path component. */
204 resolved
[resolved_len
- 1] = '\0';
205 q
= strrchr(resolved
, '/') + 1;
207 resolved_len
= q
- resolved
;
211 * If there are any path components left, then
212 * append them to symlink. The result is placed
216 if (symlink
[slen
- 1] != '/') {
217 if (slen
+ 1 >= sizeof (symlink
)) {
218 errno
= ENAMETOOLONG
;
222 symlink
[slen
+ 1] = 0;
224 left_len
= strlcat(symlink
, left
,
226 if (left_len
>= sizeof (left
)) {
227 errno
= ENAMETOOLONG
;
231 left_len
= strlcpy(left
, symlink
, sizeof (left
));
236 * Remove trailing slash except when the resolved pathname
239 if (resolved_len
> 1 && resolved
[resolved_len
- 1] == '/')
240 resolved
[resolved_len
- 1] = '\0';