1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU 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.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 # define _GL_USE_STDLIB_ALLOC 1
23 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
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)
36 #if HAVE_SYS_PARAM_H || defined _LIBC
37 # include <sys/param.h>
44 # include <shlib-compat.h>
46 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
47 # define versioned_symbol(lib, local, symbol, version) extern int dummy
48 # define compat_symbol(lib, local, symbol, version)
49 # define weak_alias(local, symbol)
50 # define __canonicalize_file_name canonicalize_file_name
51 # define __realpath realpath
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.
62 /* We want the directory in Unix syntax, not in VMS syntax. */
63 # define __getcwd(buf, max) getcwd (buf, max, 0)
65 # define __getcwd getcwd
68 # define __getcwd(buf, max) getwd (buf)
70 # define __readlink readlink
71 # define __set_errno(e) errno = (e)
74 # define MAXSYMLINKS SYMLOOP_MAX
76 # define MAXSYMLINKS 20
81 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
82 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
85 #if !FUNC_REALPATH_WORKS || defined _LIBC
86 /* Return the canonical absolute name of file NAME. A canonical name
87 does not contain any ".", ".." components nor any repeated path
88 separators ('/') or symlinks. All path components must exist. If
89 RESOLVED is null, the result is malloc'd; otherwise, if the
90 canonical name is PATH_MAX chars or more, returns null with 'errno'
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. */
98 __realpath (const char *name, char *resolved)
100 char *rpath, *dest, *extra_buf = NULL;
101 const char *start, *end, *rpath_limit;
107 /* As per Single Unix Specification V2 we must return an error if
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. */
111 __set_errno (EINVAL);
117 /* As per Single Unix Specification V2 we must return an error if
118 the name argument points to an empty string. */
119 __set_errno (ENOENT);
126 path_max = pathconf (name, _PC_PATH_MAX);
131 if (resolved == NULL)
133 rpath = malloc (path_max);
136 /* It's easier to set errno to ENOMEM than to rely on the
137 'malloc-posix' gnulib module. */
144 rpath_limit = rpath + path_max;
148 if (!__getcwd (rpath, path_max))
153 dest = strchr (rpath, '\0');
159 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
161 if (name[1] == '/' && name[2] != '/')
167 for (start = end = name; *start; start = end)
176 /* Skip sequence of multiple path-separators. */
177 while (*start == '/')
180 /* Find end of path component. */
181 for (end = start; *end && *end != '/'; ++end)
184 if (end - start == 0)
186 else if (end - start == 1 && start[0] == '.')
188 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
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
194 && *dest == '/' && dest[1] != '/')
204 if (dest + (end - start) >= rpath_limit)
206 ptrdiff_t dest_offset = dest - rpath;
211 __set_errno (ENAMETOOLONG);
212 if (dest > rpath + 1)
217 new_size = rpath_limit - rpath;
218 if (end - start + 1 > path_max)
219 new_size += end - start + 1;
221 new_size += path_max;
222 new_rpath = (char *) realloc (rpath, new_size);
223 if (new_rpath == NULL)
225 /* It's easier to set errno to ENOMEM than to rely on the
226 'realloc-posix' gnulib module. */
231 rpath_limit = rpath + new_size;
233 dest = rpath + dest_offset;
237 dest = __mempcpy (dest, start, end - start);
239 memcpy (dest, start, end - start);
245 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
247 if (lstat (rpath, &st) < 0)
251 if (S_ISLNK (st.st_mode))
256 if (++num_links > MAXSYMLINKS)
262 buf = malloca (path_max);
269 n = __readlink (rpath, buf, path_max - 1);
272 int saved_errno = errno;
281 extra_buf = malloca (path_max);
291 if ((long int) (n + len) >= path_max)
294 __set_errno (ENAMETOOLONG);
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);
304 dest = rpath + 1; /* It's an absolute symlink */
305 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
307 if (buf[1] == '/' && buf[2] != '/')
314 /* Back up to previous component, ignore if at root
316 if (dest > rpath + 1)
317 while ((--dest)[-1] != '/');
318 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
319 && *dest == '/' && dest[1] != '/')
323 else if (!S_ISDIR (st.st_mode) && *end != '\0')
325 __set_errno (ENOTDIR);
330 if (dest > rpath + 1 && dest[-1] == '/')
332 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
333 && *dest == '/' && dest[1] != '/')
344 int saved_errno = errno;
347 if (resolved == NULL)
353 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
354 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
357 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
359 attribute_compat_text_section
360 __old_realpath (const char *name, char *resolved)
362 if (resolved == NULL)
364 __set_errno (EINVAL);
368 return __realpath (name, resolved);
370 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
375 __canonicalize_file_name (const char *name)
377 return __realpath (name, NULL);
379 weak_alias (__canonicalize_file_name, canonicalize_file_name)
383 /* This declaration is solely to ensure that after preprocessing
384 this file is never empty. */