1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2010 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/>. */
22 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
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)
35 #if HAVE_SYS_PARAM_H || defined _LIBC
36 # include <sys/param.h>
43 # include <shlib-compat.h>
45 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
46 # define versioned_symbol(lib, local, symbol, version) extern int dummy
47 # define compat_symbol(lib, local, symbol, version)
48 # define weak_alias(local, symbol)
49 # define __canonicalize_file_name canonicalize_file_name
50 # define __realpath realpath
55 /* We want the directory in Unix syntax, not in VMS syntax. */
56 # define __getcwd(buf, max) getcwd (buf, max, 0)
58 # define __getcwd getcwd
61 # define __getcwd(buf, max) getwd (buf)
63 # define __readlink readlink
64 # define __set_errno(e) errno = (e)
65 /* Use the system functions, not the gnulib overrides in this file. */
69 # define MAXSYMLINKS SYMLOOP_MAX
71 # define MAXSYMLINKS 20
76 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
77 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
80 #if !FUNC_REALPATH_WORKS || defined _LIBC
81 /* Return the canonical absolute name of file NAME. A canonical name
82 does not contain any `.', `..' components nor any repeated path
83 separators ('/') or symlinks. All path components must exist. If
84 RESOLVED is null, the result is malloc'd; otherwise, if the
85 canonical name is PATH_MAX chars or more, returns null with `errno'
86 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
87 returns the name in RESOLVED. If the name cannot be resolved and
88 RESOLVED is non-NULL, it contains the path of the first component
89 that cannot be resolved. If the path can be resolved, RESOLVED
90 holds the same value as the value returned. */
93 __realpath (const char *name, char *resolved)
95 char *rpath, *dest, *extra_buf = NULL;
96 const char *start, *end, *rpath_limit;
102 /* As per Single Unix Specification V2 we must return an error if
103 either parameter is a null pointer. We extend this to allow
104 the RESOLVED parameter to be NULL in case the we are expected to
105 allocate the room for the return value. */
106 __set_errno (EINVAL);
112 /* As per Single Unix Specification V2 we must return an error if
113 the name argument points to an empty string. */
114 __set_errno (ENOENT);
121 path_max = pathconf (name, _PC_PATH_MAX);
126 if (resolved == NULL)
128 rpath = malloc (path_max);
131 /* It's easier to set errno to ENOMEM than to rely on the
132 'malloc-posix' gnulib module. */
139 rpath_limit = rpath + path_max;
143 if (!__getcwd (rpath, path_max))
148 dest = strchr (rpath, '\0');
154 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
158 for (start = end = name; *start; start = end)
167 /* Skip sequence of multiple path-separators. */
168 while (*start == '/')
171 /* Find end of path component. */
172 for (end = start; *end && *end != '/'; ++end)
175 if (end - start == 0)
177 else if (end - start == 1 && start[0] == '.')
179 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
181 /* Back up to previous component, ignore if at root already. */
182 if (dest > rpath + 1)
183 while ((--dest)[-1] != '/');
184 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
195 if (dest + (end - start) >= rpath_limit)
197 ptrdiff_t dest_offset = dest - rpath;
202 __set_errno (ENAMETOOLONG);
203 if (dest > rpath + 1)
208 new_size = rpath_limit - rpath;
209 if (end - start + 1 > path_max)
210 new_size += end - start + 1;
212 new_size += path_max;
213 new_rpath = (char *) realloc (rpath, new_size);
214 if (new_rpath == NULL)
216 /* It's easier to set errno to ENOMEM than to rely on the
217 'realloc-posix' gnulib module. */
222 rpath_limit = rpath + new_size;
224 dest = rpath + dest_offset;
228 dest = __mempcpy (dest, start, end - start);
230 memcpy (dest, start, end - start);
236 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
238 if (lstat (rpath, &st) < 0)
242 if (S_ISLNK (st.st_mode))
247 if (++num_links > MAXSYMLINKS)
253 buf = malloca (path_max);
260 n = __readlink (rpath, buf, path_max - 1);
263 int saved_errno = errno;
272 extra_buf = malloca (path_max);
282 if ((long int) (n + len) >= path_max)
285 __set_errno (ENAMETOOLONG);
289 /* Careful here, end may be a pointer into extra_buf... */
290 memmove (&extra_buf[n], end, len + 1);
291 name = end = memcpy (extra_buf, buf, n);
295 dest = rpath + 1; /* It's an absolute symlink */
296 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
301 /* Back up to previous component, ignore if at root
303 if (dest > rpath + 1)
304 while ((--dest)[-1] != '/');
305 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
310 else if (!S_ISDIR (st.st_mode) && *end != '\0')
312 __set_errno (ENOTDIR);
317 if (dest > rpath + 1 && dest[-1] == '/')
319 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/')
330 int saved_errno = errno;
333 if (resolved == NULL)
339 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
340 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
343 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
345 attribute_compat_text_section
346 __old_realpath (const char *name, char *resolved)
348 if (resolved == NULL)
350 __set_errno (EINVAL);
354 return __realpath (name, resolved);
356 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
361 __canonicalize_file_name (const char *name)
363 return __realpath (name, NULL);
365 weak_alias (__canonicalize_file_name, canonicalize_file_name)
369 /* This declaration is solely to ensure that after preprocessing
370 this file is never empty. */