realpath.c revision 295367
1/*	$OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
2/*
3 * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/* OPENBSD ORIGINAL: lib/libc/stdlib/realpath.c */
31
32#include "includes.h"
33
34#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/stat.h>
39
40#include <errno.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <unistd.h>
45
46/*
47 * char *realpath(const char *path, char resolved[PATH_MAX]);
48 *
49 * Find the real name of path, by removing all ".", ".." and symlink
50 * components.  Returns (resolved) on success, or (NULL) on failure,
51 * in which case the path which caused trouble is left in (resolved).
52 */
53char *
54realpath(const char *path, char resolved[PATH_MAX])
55{
56	struct stat sb;
57	char *p, *q, *s;
58	size_t left_len, resolved_len;
59	unsigned symlinks;
60	int serrno, slen;
61	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
62
63	serrno = errno;
64	symlinks = 0;
65	if (path[0] == '/') {
66		resolved[0] = '/';
67		resolved[1] = '\0';
68		if (path[1] == '\0')
69			return (resolved);
70		resolved_len = 1;
71		left_len = strlcpy(left, path + 1, sizeof(left));
72	} else {
73		if (getcwd(resolved, PATH_MAX) == NULL) {
74			strlcpy(resolved, ".", PATH_MAX);
75			return (NULL);
76		}
77		resolved_len = strlen(resolved);
78		left_len = strlcpy(left, path, sizeof(left));
79	}
80	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
81		errno = ENAMETOOLONG;
82		return (NULL);
83	}
84
85	/*
86	 * Iterate over path components in `left'.
87	 */
88	while (left_len != 0) {
89		/*
90		 * Extract the next path component and adjust `left'
91		 * and its length.
92		 */
93		p = strchr(left, '/');
94		s = p ? p : left + left_len;
95		if (s - left >= (ptrdiff_t)sizeof(next_token)) {
96			errno = ENAMETOOLONG;
97			return (NULL);
98		}
99		memcpy(next_token, left, s - left);
100		next_token[s - left] = '\0';
101		left_len -= s - left;
102		if (p != NULL)
103			memmove(left, s + 1, left_len + 1);
104		if (resolved[resolved_len - 1] != '/') {
105			if (resolved_len + 1 >= PATH_MAX) {
106				errno = ENAMETOOLONG;
107				return (NULL);
108			}
109			resolved[resolved_len++] = '/';
110			resolved[resolved_len] = '\0';
111		}
112		if (next_token[0] == '\0')
113			continue;
114		else if (strcmp(next_token, ".") == 0)
115			continue;
116		else if (strcmp(next_token, "..") == 0) {
117			/*
118			 * Strip the last path component except when we have
119			 * single "/"
120			 */
121			if (resolved_len > 1) {
122				resolved[resolved_len - 1] = '\0';
123				q = strrchr(resolved, '/') + 1;
124				*q = '\0';
125				resolved_len = q - resolved;
126			}
127			continue;
128		}
129
130		/*
131		 * Append the next path component and lstat() it. If
132		 * lstat() fails we still can return successfully if
133		 * there are no more path components left.
134		 */
135		resolved_len = strlcat(resolved, next_token, PATH_MAX);
136		if (resolved_len >= PATH_MAX) {
137			errno = ENAMETOOLONG;
138			return (NULL);
139		}
140		if (lstat(resolved, &sb) != 0) {
141			if (errno == ENOENT && p == NULL) {
142				errno = serrno;
143				return (resolved);
144			}
145			return (NULL);
146		}
147		if (S_ISLNK(sb.st_mode)) {
148			if (symlinks++ > MAXSYMLINKS) {
149				errno = ELOOP;
150				return (NULL);
151			}
152			slen = readlink(resolved, symlink, sizeof(symlink) - 1);
153			if (slen < 0)
154				return (NULL);
155			symlink[slen] = '\0';
156			if (symlink[0] == '/') {
157				resolved[1] = 0;
158				resolved_len = 1;
159			} else if (resolved_len > 1) {
160				/* Strip the last path component. */
161				resolved[resolved_len - 1] = '\0';
162				q = strrchr(resolved, '/') + 1;
163				*q = '\0';
164				resolved_len = q - resolved;
165			}
166
167			/*
168			 * If there are any path components left, then
169			 * append them to symlink. The result is placed
170			 * in `left'.
171			 */
172			if (p != NULL) {
173				if (symlink[slen - 1] != '/') {
174					if (slen + 1 >=
175					    (ptrdiff_t)sizeof(symlink)) {
176						errno = ENAMETOOLONG;
177						return (NULL);
178					}
179					symlink[slen] = '/';
180					symlink[slen + 1] = 0;
181				}
182				left_len = strlcat(symlink, left, sizeof(left));
183				if (left_len >= sizeof(left)) {
184					errno = ENAMETOOLONG;
185					return (NULL);
186				}
187			}
188			left_len = strlcpy(left, symlink, sizeof(left));
189		}
190	}
191
192	/*
193	 * Remove trailing slash except when the resolved pathname
194	 * is a single "/".
195	 */
196	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
197		resolved[resolved_len - 1] = '\0';
198	return (resolved);
199}
200#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */
201