1 /* $Id: realpath.c,v 1.3 2013/01/25 17:06:09 sjg Exp $ */
2 /* from: $NetBSD: getcwd.c,v 1.53 2012/06/21 23:29:23 enami 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 # define __restrict /* restrict */
60 * char *realpath(const char *path, char *resolved);
62 * Find the real name of path, by removing all ".", ".." and symlink
63 * components. Returns (resolved) on success, or (NULL) on failure,
64 * in which case the path which caused trouble is left in (resolved).
67 realpath(const char * __restrict path, char * __restrict resolved)
70 int idx = 0, nlnk = 0;
72 char *p, wbuf[2][MAXPATHLEN], *fres;
76 /* POSIX sez we must test for this */
82 if (resolved == NULL) {
83 fres = resolved = malloc(MAXPATHLEN);
91 * Build real path one by one with paying an attention to .,
92 * .. and symbolic link.
96 * `p' is where we'll put a new component with prepending
107 /* If relative path, start from current working directory. */
109 /* check for resolved pointer to appease coverity */
110 if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
115 len = strlen(resolved);
121 /* Skip any slash. */
132 /* Find the end of this component. */
136 while (*q != '/' && *q != '\0');
139 if (path[0] == '.') {
144 if (path[1] == '.' && q - path == 2) {
145 /* Trim the last component. */
154 /* Append this component. */
155 if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
156 errno = ENAMETOOLONG;
164 /* LINTED We know q > path. */
166 p[1 + q - path] = '\0';
169 * If this component is a symlink, toss it and prepend link
170 * target to unresolved path.
172 if (lstat(resolved, &sb) == -1)
175 if (S_ISLNK(sb.st_mode)) {
176 if (nlnk++ >= MAXSYMLINKS) {
180 n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
188 /* Append unresolved path to link target and switch to it. */
189 if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
190 errno = ENAMETOOLONG;
193 memcpy(&wbuf[idx][n], q, len + 1);
197 /* If absolute symlink, start from root. */
202 if (*q == '/' && !S_ISDIR(sb.st_mode)) {
207 /* Advance both resolved and unresolved path. */