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