1 /* $Id: realpath.c,v 1.2 2010/04/21 17:47:49 sjg Exp $ */
2 /* from: $NetBSD: getcwd.c,v 1.45 2007/10/26 19:48:14 christos Exp $ */
5 * Copyright (c) 1989, 1991, 1993, 1995
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
56 * char *realpath(const char *path, char resolved[MAXPATHLEN]);
58 * Find the real name of path, by removing all ".", ".." and symlink
59 * components. Returns (resolved) on success, or (NULL) on failure,
60 * in which case the path which caused trouble is left in (resolved).
63 realpath(const char *path, char *resolved)
66 int idx = 0, n, nlnk = 0;
68 char *p, wbuf[2][MAXPATHLEN];
71 if (!path || !resolved || path == resolved)
75 * Build real path one by one with paying an attention to .,
76 * .. and symbolic link.
80 * `p' is where we'll put a new component with prepending
91 /* If relative path, start from current working directory. */
93 /* check for resolved pointer to appease coverity */
94 if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
99 len = strlen(resolved);
105 /* Skip any slash. */
116 /* Find the end of this component. */
120 while (*q != '/' && *q != 0);
123 if (path[0] == '.') {
128 if (path[1] == '.' && q - path == 2) {
129 /* Trim the last component. */
138 /* Append this component. */
139 if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
140 errno = ENAMETOOLONG;
148 /* LINTED We know q > path. */
153 * If this component is a symlink, toss it and prepend link
154 * target to unresolved path.
156 if (lstat(resolved, &sb) == -1) {
159 if (S_ISLNK(sb.st_mode)) {
160 if (nlnk++ >= MAXSYMLINKS) {
164 n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
172 /* Append unresolved path to link target and switch to it. */
173 if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
174 errno = ENAMETOOLONG;
177 memcpy(&wbuf[idx][n], q, len + 1);
181 /* If absolute symlink, start from root. */
186 if (*q == '/' && !S_ISDIR(sb.st_mode)) {
191 /* Advance both resolved and unresolved path. */