Commit | Line | Data |
---|---|---|
ffca4c22 | 1 | /* Return the canonical absolute name of a given file. |
61cd9dc9 | 2 | Copyright (C) 1996-2010 Free Software Foundation, Inc. |
ffca4c22 AW |
3 | This file is part of the GNU C Library. |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU Lesser General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
8912421c LC |
18 | #ifndef _LIBC |
19 | # include <config.h> | |
20 | #endif | |
ffca4c22 | 21 | |
8912421c | 22 | #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC |
ffca4c22 | 23 | |
1cd4fffc LC |
24 | /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc |
25 | optimizes away the name == NULL test below. */ | |
26 | #define _GL_ARG_NONNULL(params) | |
27 | ||
ffca4c22 | 28 | /* Specification. */ |
ffca4c22 | 29 | #include <stdlib.h> |
ffca4c22 | 30 | |
8912421c LC |
31 | #include <alloca.h> |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
ffca4c22 | 34 | #include <limits.h> |
ffca4c22 AW |
35 | #if HAVE_SYS_PARAM_H || defined _LIBC |
36 | # include <sys/param.h> | |
37 | #endif | |
ffca4c22 | 38 | #include <sys/stat.h> |
ffca4c22 | 39 | #include <errno.h> |
8912421c | 40 | #include <stddef.h> |
ffca4c22 AW |
41 | |
42 | #ifdef _LIBC | |
43 | # include <shlib-compat.h> | |
44 | #else | |
45 | # define SHLIB_COMPAT(lib, introduced, obsoleted) 0 | |
a927b6c1 | 46 | # define versioned_symbol(lib, local, symbol, version) extern int dummy |
ffca4c22 AW |
47 | # define compat_symbol(lib, local, symbol, version) |
48 | # define weak_alias(local, symbol) | |
49 | # define __canonicalize_file_name canonicalize_file_name | |
8912421c | 50 | # define __realpath realpath |
ffca4c22 AW |
51 | # include "pathmax.h" |
52 | # include "malloca.h" | |
53 | # if HAVE_GETCWD | |
54 | # ifdef VMS | |
55 | /* We want the directory in Unix syntax, not in VMS syntax. */ | |
56 | # define __getcwd(buf, max) getcwd (buf, max, 0) | |
57 | # else | |
58 | # define __getcwd getcwd | |
59 | # endif | |
60 | # else | |
61 | # define __getcwd(buf, max) getwd (buf) | |
62 | # endif | |
63 | # define __readlink readlink | |
8912421c LC |
64 | # define __set_errno(e) errno = (e) |
65 | # ifndef MAXSYMLINKS | |
66 | # ifdef SYMLOOP_MAX | |
67 | # define MAXSYMLINKS SYMLOOP_MAX | |
68 | # else | |
69 | # define MAXSYMLINKS 20 | |
70 | # endif | |
ffca4c22 AW |
71 | # endif |
72 | #endif | |
73 | ||
8912421c LC |
74 | #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT |
75 | # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0 | |
76 | #endif | |
77 | ||
78 | #if !FUNC_REALPATH_WORKS || defined _LIBC | |
ffca4c22 AW |
79 | /* Return the canonical absolute name of file NAME. A canonical name |
80 | does not contain any `.', `..' components nor any repeated path | |
81 | separators ('/') or symlinks. All path components must exist. If | |
82 | RESOLVED is null, the result is malloc'd; otherwise, if the | |
83 | canonical name is PATH_MAX chars or more, returns null with `errno' | |
84 | set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, | |
85 | returns the name in RESOLVED. If the name cannot be resolved and | |
86 | RESOLVED is non-NULL, it contains the path of the first component | |
87 | that cannot be resolved. If the path can be resolved, RESOLVED | |
88 | holds the same value as the value returned. */ | |
89 | ||
90 | char * | |
91 | __realpath (const char *name, char *resolved) | |
92 | { | |
93 | char *rpath, *dest, *extra_buf = NULL; | |
94 | const char *start, *end, *rpath_limit; | |
95 | long int path_max; | |
ffca4c22 | 96 | int num_links = 0; |
ffca4c22 AW |
97 | |
98 | if (name == NULL) | |
99 | { | |
100 | /* As per Single Unix Specification V2 we must return an error if | |
1cd4fffc LC |
101 | either parameter is a null pointer. We extend this to allow |
102 | the RESOLVED parameter to be NULL in case the we are expected to | |
103 | allocate the room for the return value. */ | |
ffca4c22 AW |
104 | __set_errno (EINVAL); |
105 | return NULL; | |
106 | } | |
107 | ||
108 | if (name[0] == '\0') | |
109 | { | |
110 | /* As per Single Unix Specification V2 we must return an error if | |
1cd4fffc | 111 | the name argument points to an empty string. */ |
ffca4c22 AW |
112 | __set_errno (ENOENT); |
113 | return NULL; | |
114 | } | |
115 | ||
116 | #ifdef PATH_MAX | |
117 | path_max = PATH_MAX; | |
118 | #else | |
119 | path_max = pathconf (name, _PC_PATH_MAX); | |
120 | if (path_max <= 0) | |
121 | path_max = 1024; | |
122 | #endif | |
123 | ||
124 | if (resolved == NULL) | |
125 | { | |
126 | rpath = malloc (path_max); | |
127 | if (rpath == NULL) | |
1cd4fffc LC |
128 | { |
129 | /* It's easier to set errno to ENOMEM than to rely on the | |
130 | 'malloc-posix' gnulib module. */ | |
131 | errno = ENOMEM; | |
132 | return NULL; | |
133 | } | |
ffca4c22 AW |
134 | } |
135 | else | |
136 | rpath = resolved; | |
137 | rpath_limit = rpath + path_max; | |
138 | ||
139 | if (name[0] != '/') | |
140 | { | |
141 | if (!__getcwd (rpath, path_max)) | |
1cd4fffc LC |
142 | { |
143 | rpath[0] = '\0'; | |
144 | goto error; | |
145 | } | |
ffca4c22 AW |
146 | dest = strchr (rpath, '\0'); |
147 | } | |
148 | else | |
149 | { | |
150 | rpath[0] = '/'; | |
151 | dest = rpath + 1; | |
8912421c | 152 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/') |
1cd4fffc | 153 | *dest++ = '/'; |
ffca4c22 AW |
154 | } |
155 | ||
156 | for (start = end = name; *start; start = end) | |
157 | { | |
158 | #ifdef _LIBC | |
159 | struct stat64 st; | |
160 | #else | |
161 | struct stat st; | |
162 | #endif | |
8912421c | 163 | int n; |
ffca4c22 AW |
164 | |
165 | /* Skip sequence of multiple path-separators. */ | |
166 | while (*start == '/') | |
1cd4fffc | 167 | ++start; |
ffca4c22 AW |
168 | |
169 | /* Find end of path component. */ | |
170 | for (end = start; *end && *end != '/'; ++end) | |
1cd4fffc | 171 | /* Nothing. */; |
ffca4c22 AW |
172 | |
173 | if (end - start == 0) | |
1cd4fffc | 174 | break; |
ffca4c22 | 175 | else if (end - start == 1 && start[0] == '.') |
1cd4fffc | 176 | /* nothing */; |
ffca4c22 | 177 | else if (end - start == 2 && start[0] == '.' && start[1] == '.') |
1cd4fffc LC |
178 | { |
179 | /* Back up to previous component, ignore if at root already. */ | |
180 | if (dest > rpath + 1) | |
181 | while ((--dest)[-1] != '/'); | |
182 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
183 | && *dest == '/') | |
184 | dest++; | |
185 | } | |
ffca4c22 | 186 | else |
1cd4fffc LC |
187 | { |
188 | size_t new_size; | |
189 | ||
190 | if (dest[-1] != '/') | |
191 | *dest++ = '/'; | |
192 | ||
193 | if (dest + (end - start) >= rpath_limit) | |
194 | { | |
195 | ptrdiff_t dest_offset = dest - rpath; | |
196 | char *new_rpath; | |
197 | ||
198 | if (resolved) | |
199 | { | |
200 | __set_errno (ENAMETOOLONG); | |
201 | if (dest > rpath + 1) | |
202 | dest--; | |
203 | *dest = '\0'; | |
204 | goto error; | |
205 | } | |
206 | new_size = rpath_limit - rpath; | |
207 | if (end - start + 1 > path_max) | |
208 | new_size += end - start + 1; | |
209 | else | |
210 | new_size += path_max; | |
211 | new_rpath = (char *) realloc (rpath, new_size); | |
212 | if (new_rpath == NULL) | |
213 | { | |
214 | /* It's easier to set errno to ENOMEM than to rely on the | |
215 | 'realloc-posix' gnulib module. */ | |
216 | errno = ENOMEM; | |
217 | goto error; | |
218 | } | |
219 | rpath = new_rpath; | |
220 | rpath_limit = rpath + new_size; | |
221 | ||
222 | dest = rpath + dest_offset; | |
223 | } | |
ffca4c22 AW |
224 | |
225 | #ifdef _LIBC | |
1cd4fffc | 226 | dest = __mempcpy (dest, start, end - start); |
ffca4c22 | 227 | #else |
1cd4fffc LC |
228 | memcpy (dest, start, end - start); |
229 | dest += end - start; | |
ffca4c22 | 230 | #endif |
1cd4fffc | 231 | *dest = '\0'; |
ffca4c22 AW |
232 | |
233 | #ifdef _LIBC | |
1cd4fffc | 234 | if (__lxstat64 (_STAT_VER, rpath, &st) < 0) |
ffca4c22 | 235 | #else |
1cd4fffc | 236 | if (lstat (rpath, &st) < 0) |
ffca4c22 | 237 | #endif |
1cd4fffc LC |
238 | goto error; |
239 | ||
240 | if (S_ISLNK (st.st_mode)) | |
241 | { | |
242 | char *buf; | |
243 | size_t len; | |
244 | ||
245 | if (++num_links > MAXSYMLINKS) | |
246 | { | |
247 | __set_errno (ELOOP); | |
248 | goto error; | |
249 | } | |
250 | ||
251 | buf = malloca (path_max); | |
252 | if (!buf) | |
253 | { | |
254 | errno = ENOMEM; | |
255 | goto error; | |
256 | } | |
257 | ||
258 | n = __readlink (rpath, buf, path_max - 1); | |
259 | if (n < 0) | |
260 | { | |
261 | int saved_errno = errno; | |
262 | freea (buf); | |
263 | errno = saved_errno; | |
264 | goto error; | |
265 | } | |
266 | buf[n] = '\0'; | |
267 | ||
268 | if (!extra_buf) | |
269 | { | |
270 | extra_buf = malloca (path_max); | |
271 | if (!extra_buf) | |
272 | { | |
273 | freea (buf); | |
274 | errno = ENOMEM; | |
275 | goto error; | |
276 | } | |
277 | } | |
278 | ||
279 | len = strlen (end); | |
280 | if ((long int) (n + len) >= path_max) | |
281 | { | |
282 | freea (buf); | |
283 | __set_errno (ENAMETOOLONG); | |
284 | goto error; | |
285 | } | |
286 | ||
287 | /* Careful here, end may be a pointer into extra_buf... */ | |
288 | memmove (&extra_buf[n], end, len + 1); | |
289 | name = end = memcpy (extra_buf, buf, n); | |
290 | ||
291 | if (buf[0] == '/') | |
292 | { | |
293 | dest = rpath + 1; /* It's an absolute symlink */ | |
294 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/') | |
295 | *dest++ = '/'; | |
296 | } | |
297 | else | |
298 | { | |
299 | /* Back up to previous component, ignore if at root | |
300 | already: */ | |
301 | if (dest > rpath + 1) | |
302 | while ((--dest)[-1] != '/'); | |
303 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
304 | && *dest == '/') | |
305 | dest++; | |
306 | } | |
307 | } | |
308 | else if (!S_ISDIR (st.st_mode) && *end != '\0') | |
309 | { | |
310 | __set_errno (ENOTDIR); | |
311 | goto error; | |
312 | } | |
313 | } | |
ffca4c22 AW |
314 | } |
315 | if (dest > rpath + 1 && dest[-1] == '/') | |
316 | --dest; | |
8912421c LC |
317 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/') |
318 | dest++; | |
ffca4c22 AW |
319 | *dest = '\0'; |
320 | ||
321 | if (extra_buf) | |
322 | freea (extra_buf); | |
323 | ||
8912421c | 324 | return rpath; |
ffca4c22 AW |
325 | |
326 | error: | |
327 | { | |
328 | int saved_errno = errno; | |
329 | if (extra_buf) | |
330 | freea (extra_buf); | |
8912421c | 331 | if (resolved == NULL) |
ffca4c22 AW |
332 | free (rpath); |
333 | errno = saved_errno; | |
334 | } | |
335 | return NULL; | |
336 | } | |
ffca4c22 | 337 | versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); |
8912421c | 338 | #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */ |
ffca4c22 AW |
339 | |
340 | ||
341 | #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) | |
342 | char * | |
8912421c | 343 | attribute_compat_text_section |
ffca4c22 AW |
344 | __old_realpath (const char *name, char *resolved) |
345 | { | |
346 | if (resolved == NULL) | |
347 | { | |
348 | __set_errno (EINVAL); | |
349 | return NULL; | |
350 | } | |
351 | ||
352 | return __realpath (name, resolved); | |
353 | } | |
354 | compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0); | |
355 | #endif | |
356 | ||
357 | ||
358 | char * | |
359 | __canonicalize_file_name (const char *name) | |
360 | { | |
361 | return __realpath (name, NULL); | |
362 | } | |
363 | weak_alias (__canonicalize_file_name, canonicalize_file_name) | |
364 | ||
365 | #else | |
366 | ||
367 | /* This declaration is solely to ensure that after preprocessing | |
368 | this file is never empty. */ | |
369 | typedef int dummy; | |
370 | ||
371 | #endif |