xref: /dflybsd-src/lib/libc/stdlib/realpath.c (revision 225cb38fefe493d6b322d9e7699d9987ec2c56e5)
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  * @(#)realpath.c	8.1 (Berkeley) 2/16/94
29  * $FreeBSD: head/lib/libc/stdlib/realpath.c 264417 2014-04-13 19:48:28Z jilles $
30  */
31 
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include "un-namespace.h"
41 
42 int     __realpath(const char *path, char *rbuf, size_t len);
43 
44 /*
45  * Find the real name of path, by removing all ".", ".." and symlink
46  * components.  Returns (resolved) on success, or (NULL) on failure,
47  * in which case the path which caused trouble is left in (resolved).
48  */
49 char *
50 realpath(const char * __restrict path, char * __restrict resolved)
51 {
52 	struct stat sb;
53 	char *p, *q, *s;
54 	size_t left_len, resolved_len;
55 	unsigned symlinks;
56 	int m, slen;
57 	char left[PATH_MAX], next_token[PATH_MAX], my_symlink[PATH_MAX];
58 
59 	if (path == NULL) {
60 		errno = EINVAL;
61 		return (NULL);
62 	}
63 	if (path[0] == '\0') {
64 		errno = ENOENT;
65 		return (NULL);
66 	}
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 	}
75 
76 	if (getosreldate() >= 500710) {
77 		if (__realpath(path, resolved, PATH_MAX) < 0) {
78 			if (m)
79 				free(resolved);
80 			return (NULL);
81 		}
82 		return resolved;
83 	}
84 
85 	symlinks = 0;
86 	if (path[0] == '/') {
87 		resolved[0] = '/';
88 		resolved[1] = '\0';
89 		if (path[1] == '\0')
90 			return (resolved);
91 		resolved_len = 1;
92 		left_len = strlcpy(left, path + 1, sizeof(left));
93 	} else {
94 		if (getcwd(resolved, PATH_MAX) == NULL) {
95 			if (m) {
96 				free(resolved);
97 			} else {
98 				resolved[0] = '.';
99 				resolved[1] = '\0';
100 			}
101 			return (NULL);
102 		}
103 		resolved_len = strlen(resolved);
104 		left_len = strlcpy(left, path, sizeof(left));
105 	}
106 	if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
107 		if (m)
108 			free(resolved);
109 		errno = ENAMETOOLONG;
110 		return (NULL);
111 	}
112 
113 	/*
114 	 * Iterate over path components in `left'.
115 	 */
116 	while (left_len != 0) {
117 		/*
118 		 * Extract the next path component and adjust `left'
119 		 * and its length.
120 		 */
121 		p = strchr(left, '/');
122 		s = p ? p : left + left_len;
123 		if (s - left >= sizeof(next_token)) {
124 			if (m)
125 				free(resolved);
126 			errno = ENAMETOOLONG;
127 			return (NULL);
128 		}
129 		memcpy(next_token, left, s - left);
130 		next_token[s - left] = '\0';
131 		left_len -= s - left;
132 		if (p != NULL)
133 			memmove(left, s + 1, left_len + 1);
134 		if (resolved[resolved_len - 1] != '/') {
135 			if (resolved_len + 1 >= PATH_MAX) {
136 				if (m)
137 					free(resolved);
138 				errno = ENAMETOOLONG;
139 				return (NULL);
140 			}
141 			resolved[resolved_len++] = '/';
142 			resolved[resolved_len] = '\0';
143 		}
144 		if (next_token[0] == '\0') {
145 			/* Handle consequential slashes. */
146 			continue;
147 		} else if (strcmp(next_token, ".") == 0) {
148 			continue;
149 		} else if (strcmp(next_token, "..") == 0) {
150 			/*
151 			 * Strip the last path component except when we have
152 			 * single "/"
153 			 */
154 			if (resolved_len > 1) {
155 				resolved[resolved_len - 1] = '\0';
156 				q = strrchr(resolved, '/') + 1;
157 				*q = '\0';
158 				resolved_len = q - resolved;
159 			}
160 			continue;
161 		}
162 
163 		/*
164 		 * Append the next path component and lstat() it.
165 		 */
166 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
167 		if (resolved_len >= PATH_MAX) {
168 			if (m)
169 				free(resolved);
170 			errno = ENAMETOOLONG;
171 			return (NULL);
172 		}
173 		if (lstat(resolved, &sb) != 0) {
174 			if (m)
175 				free(resolved);
176 			return (NULL);
177 		}
178 		if (S_ISLNK(sb.st_mode)) {
179 			if (symlinks++ > MAXSYMLINKS) {
180 				if (m)
181 					free(resolved);
182 				errno = ELOOP;
183 				return (NULL);
184 			}
185 			slen = readlink(resolved, my_symlink, sizeof(my_symlink) - 1);
186 			if (slen < 0) {
187 				if (m)
188 					free(resolved);
189 				return (NULL);
190 			}
191 			my_symlink[slen] = '\0';
192 			if (my_symlink[0] == '/') {
193 				resolved[1] = 0;
194 				resolved_len = 1;
195 			} else if (resolved_len > 1) {
196 				/* Strip the last path component. */
197 				resolved[resolved_len - 1] = '\0';
198 				q = strrchr(resolved, '/') + 1;
199 				*q = '\0';
200 				resolved_len = q - resolved;
201 			}
202 
203 			/*
204 			 * If there are any path components left, then
205 			 * append them to symlink. The result is placed
206 			 * in `left'.
207 			 */
208 			if (p != NULL) {
209 				if (my_symlink[slen - 1] != '/') {
210 					if (slen + 1 >= (int)sizeof(my_symlink)) {
211 						if (m)
212 							free(resolved);
213 						errno = ENAMETOOLONG;
214 						return (NULL);
215 					}
216 					my_symlink[slen] = '/';
217 					my_symlink[slen + 1] = 0;
218 				}
219 				left_len = strlcat(my_symlink, left,
220 				    sizeof(my_symlink));
221 				if (left_len >= sizeof(left)) {
222 					if (m)
223 						free(resolved);
224 					errno = ENAMETOOLONG;
225 					return (NULL);
226 				}
227 			}
228 			left_len = strlcpy(left, my_symlink, sizeof(left));
229 		} else if (!S_ISDIR(sb.st_mode) && p != NULL) {
230 			if (m)
231 				free(resolved);
232 			errno = ENOTDIR;
233 			return (NULL);
234 		}
235 	}
236 
237 	/*
238 	 * Remove trailing slash except when the resolved pathname
239 	 * is a single "/".
240 	 */
241 	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
242 		resolved[resolved_len - 1] = '\0';
243 	return (resolved);
244 }
245