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