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