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