Commit | Line | Data |
---|---|---|
ffca4c22 | 1 | /* Return the canonical absolute name of a given file. |
49114fd4 | 2 | Copyright (C) 1996-2011 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 | |
9157d901 LC |
54 | # if IN_RELOCWRAPPER |
55 | /* When building the relocatable program wrapper, use the system's getcwd | |
56 | function, not the gnulib override, otherwise we would get a link error. | |
57 | */ | |
58 | # undef getcwd | |
59 | # endif | |
ffca4c22 AW |
60 | # ifdef VMS |
61 | /* We want the directory in Unix syntax, not in VMS syntax. */ | |
62 | # define __getcwd(buf, max) getcwd (buf, max, 0) | |
63 | # else | |
64 | # define __getcwd getcwd | |
65 | # endif | |
66 | # else | |
67 | # define __getcwd(buf, max) getwd (buf) | |
68 | # endif | |
69 | # define __readlink readlink | |
8912421c | 70 | # define __set_errno(e) errno = (e) |
9157d901 LC |
71 | /* Use the system functions, not the gnulib overrides in this file. */ |
72 | # undef malloc | |
8912421c LC |
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 AW |
87 | /* Return the canonical absolute name of file NAME. A canonical name |
88 | does not contain any `.', `..' components nor any repeated path | |
89 | separators ('/') or symlinks. All path components must exist. If | |
90 | RESOLVED is null, the result is malloc'd; otherwise, if the | |
91 | canonical name is PATH_MAX chars or more, returns null with `errno' | |
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; |
ffca4c22 AW |
105 | |
106 | if (name == NULL) | |
107 | { | |
108 | /* As per Single Unix Specification V2 we must return an error if | |
1cd4fffc LC |
109 | either parameter is a null pointer. We extend this to allow |
110 | the RESOLVED parameter to be NULL in case the we are expected to | |
111 | allocate the room for the return value. */ | |
ffca4c22 AW |
112 | __set_errno (EINVAL); |
113 | return NULL; | |
114 | } | |
115 | ||
116 | if (name[0] == '\0') | |
117 | { | |
118 | /* As per Single Unix Specification V2 we must return an error if | |
1cd4fffc | 119 | the name argument points to an empty string. */ |
ffca4c22 AW |
120 | __set_errno (ENOENT); |
121 | return NULL; | |
122 | } | |
123 | ||
124 | #ifdef PATH_MAX | |
125 | path_max = PATH_MAX; | |
126 | #else | |
127 | path_max = pathconf (name, _PC_PATH_MAX); | |
128 | if (path_max <= 0) | |
129 | path_max = 1024; | |
130 | #endif | |
131 | ||
132 | if (resolved == NULL) | |
133 | { | |
134 | rpath = malloc (path_max); | |
135 | if (rpath == NULL) | |
1cd4fffc LC |
136 | { |
137 | /* It's easier to set errno to ENOMEM than to rely on the | |
138 | 'malloc-posix' gnulib module. */ | |
139 | errno = ENOMEM; | |
140 | return NULL; | |
141 | } | |
ffca4c22 AW |
142 | } |
143 | else | |
144 | rpath = resolved; | |
145 | rpath_limit = rpath + path_max; | |
146 | ||
147 | if (name[0] != '/') | |
148 | { | |
149 | if (!__getcwd (rpath, path_max)) | |
1cd4fffc LC |
150 | { |
151 | rpath[0] = '\0'; | |
152 | goto error; | |
153 | } | |
ffca4c22 AW |
154 | dest = strchr (rpath, '\0'); |
155 | } | |
156 | else | |
157 | { | |
158 | rpath[0] = '/'; | |
159 | dest = rpath + 1; | |
8912421c | 160 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/') |
1cd4fffc | 161 | *dest++ = '/'; |
ffca4c22 AW |
162 | } |
163 | ||
164 | for (start = end = name; *start; start = end) | |
165 | { | |
166 | #ifdef _LIBC | |
167 | struct stat64 st; | |
168 | #else | |
169 | struct stat st; | |
170 | #endif | |
8912421c | 171 | int n; |
ffca4c22 AW |
172 | |
173 | /* Skip sequence of multiple path-separators. */ | |
174 | while (*start == '/') | |
1cd4fffc | 175 | ++start; |
ffca4c22 AW |
176 | |
177 | /* Find end of path component. */ | |
178 | for (end = start; *end && *end != '/'; ++end) | |
1cd4fffc | 179 | /* Nothing. */; |
ffca4c22 AW |
180 | |
181 | if (end - start == 0) | |
1cd4fffc | 182 | break; |
ffca4c22 | 183 | else if (end - start == 1 && start[0] == '.') |
1cd4fffc | 184 | /* nothing */; |
ffca4c22 | 185 | else if (end - start == 2 && start[0] == '.' && start[1] == '.') |
1cd4fffc LC |
186 | { |
187 | /* Back up to previous component, ignore if at root already. */ | |
188 | if (dest > rpath + 1) | |
189 | while ((--dest)[-1] != '/'); | |
190 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
191 | && *dest == '/') | |
192 | dest++; | |
193 | } | |
ffca4c22 | 194 | else |
1cd4fffc LC |
195 | { |
196 | size_t new_size; | |
197 | ||
198 | if (dest[-1] != '/') | |
199 | *dest++ = '/'; | |
200 | ||
201 | if (dest + (end - start) >= rpath_limit) | |
202 | { | |
203 | ptrdiff_t dest_offset = dest - rpath; | |
204 | char *new_rpath; | |
205 | ||
206 | if (resolved) | |
207 | { | |
208 | __set_errno (ENAMETOOLONG); | |
209 | if (dest > rpath + 1) | |
210 | dest--; | |
211 | *dest = '\0'; | |
212 | goto error; | |
213 | } | |
214 | new_size = rpath_limit - rpath; | |
215 | if (end - start + 1 > path_max) | |
216 | new_size += end - start + 1; | |
217 | else | |
218 | new_size += path_max; | |
219 | new_rpath = (char *) realloc (rpath, new_size); | |
220 | if (new_rpath == NULL) | |
221 | { | |
222 | /* It's easier to set errno to ENOMEM than to rely on the | |
223 | 'realloc-posix' gnulib module. */ | |
224 | errno = ENOMEM; | |
225 | goto error; | |
226 | } | |
227 | rpath = new_rpath; | |
228 | rpath_limit = rpath + new_size; | |
229 | ||
230 | dest = rpath + dest_offset; | |
231 | } | |
ffca4c22 AW |
232 | |
233 | #ifdef _LIBC | |
1cd4fffc | 234 | dest = __mempcpy (dest, start, end - start); |
ffca4c22 | 235 | #else |
1cd4fffc LC |
236 | memcpy (dest, start, end - start); |
237 | dest += end - start; | |
ffca4c22 | 238 | #endif |
1cd4fffc | 239 | *dest = '\0'; |
ffca4c22 AW |
240 | |
241 | #ifdef _LIBC | |
1cd4fffc | 242 | if (__lxstat64 (_STAT_VER, rpath, &st) < 0) |
ffca4c22 | 243 | #else |
1cd4fffc | 244 | if (lstat (rpath, &st) < 0) |
ffca4c22 | 245 | #endif |
1cd4fffc LC |
246 | goto error; |
247 | ||
248 | if (S_ISLNK (st.st_mode)) | |
249 | { | |
250 | char *buf; | |
251 | size_t len; | |
252 | ||
253 | if (++num_links > MAXSYMLINKS) | |
254 | { | |
255 | __set_errno (ELOOP); | |
256 | goto error; | |
257 | } | |
258 | ||
259 | buf = malloca (path_max); | |
260 | if (!buf) | |
261 | { | |
262 | errno = ENOMEM; | |
263 | goto error; | |
264 | } | |
265 | ||
266 | n = __readlink (rpath, buf, path_max - 1); | |
267 | if (n < 0) | |
268 | { | |
269 | int saved_errno = errno; | |
270 | freea (buf); | |
271 | errno = saved_errno; | |
272 | goto error; | |
273 | } | |
274 | buf[n] = '\0'; | |
275 | ||
276 | if (!extra_buf) | |
277 | { | |
278 | extra_buf = malloca (path_max); | |
279 | if (!extra_buf) | |
280 | { | |
281 | freea (buf); | |
282 | errno = ENOMEM; | |
283 | goto error; | |
284 | } | |
285 | } | |
286 | ||
287 | len = strlen (end); | |
288 | if ((long int) (n + len) >= path_max) | |
289 | { | |
290 | freea (buf); | |
291 | __set_errno (ENAMETOOLONG); | |
292 | goto error; | |
293 | } | |
294 | ||
295 | /* Careful here, end may be a pointer into extra_buf... */ | |
296 | memmove (&extra_buf[n], end, len + 1); | |
297 | name = end = memcpy (extra_buf, buf, n); | |
298 | ||
299 | if (buf[0] == '/') | |
300 | { | |
301 | dest = rpath + 1; /* It's an absolute symlink */ | |
302 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/') | |
303 | *dest++ = '/'; | |
304 | } | |
305 | else | |
306 | { | |
307 | /* Back up to previous component, ignore if at root | |
308 | already: */ | |
309 | if (dest > rpath + 1) | |
310 | while ((--dest)[-1] != '/'); | |
311 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
312 | && *dest == '/') | |
313 | dest++; | |
314 | } | |
315 | } | |
316 | else if (!S_ISDIR (st.st_mode) && *end != '\0') | |
317 | { | |
318 | __set_errno (ENOTDIR); | |
319 | goto error; | |
320 | } | |
321 | } | |
ffca4c22 AW |
322 | } |
323 | if (dest > rpath + 1 && dest[-1] == '/') | |
324 | --dest; | |
8912421c LC |
325 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/') |
326 | dest++; | |
ffca4c22 AW |
327 | *dest = '\0'; |
328 | ||
329 | if (extra_buf) | |
330 | freea (extra_buf); | |
331 | ||
8912421c | 332 | return rpath; |
ffca4c22 AW |
333 | |
334 | error: | |
335 | { | |
336 | int saved_errno = errno; | |
337 | if (extra_buf) | |
338 | freea (extra_buf); | |
8912421c | 339 | if (resolved == NULL) |
ffca4c22 AW |
340 | free (rpath); |
341 | errno = saved_errno; | |
342 | } | |
343 | return NULL; | |
344 | } | |
ffca4c22 | 345 | versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); |
8912421c | 346 | #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */ |
ffca4c22 AW |
347 | |
348 | ||
349 | #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) | |
350 | char * | |
8912421c | 351 | attribute_compat_text_section |
ffca4c22 AW |
352 | __old_realpath (const char *name, char *resolved) |
353 | { | |
354 | if (resolved == NULL) | |
355 | { | |
356 | __set_errno (EINVAL); | |
357 | return NULL; | |
358 | } | |
359 | ||
360 | return __realpath (name, resolved); | |
361 | } | |
362 | compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0); | |
363 | #endif | |
364 | ||
365 | ||
366 | char * | |
367 | __canonicalize_file_name (const char *name) | |
368 | { | |
369 | return __realpath (name, NULL); | |
370 | } | |
371 | weak_alias (__canonicalize_file_name, canonicalize_file_name) | |
372 | ||
373 | #else | |
374 | ||
375 | /* This declaration is solely to ensure that after preprocessing | |
376 | this file is never empty. */ | |
377 | typedef int dummy; | |
378 | ||
379 | #endif |