1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2003, 2005-2009 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/>. */
20 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
33 #if HAVE_SYS_PARAM_H || defined _LIBC
34 # include <sys/param.h>
38 # define MAXSYMLINKS SYMLOOP_MAX
40 # define MAXSYMLINKS 20
48 # define __set_errno(e) errno = (e)
52 # include <shlib-compat.h>
54 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
55 # define versioned_symbol(lib, local, symbol, version)
56 # define compat_symbol(lib, local, symbol, version)
57 # define weak_alias(local, symbol)
58 # define __canonicalize_file_name canonicalize_file_name
59 # define __realpath realpath
64 /* We want the directory in Unix syntax, not in VMS syntax. */
65 # define __getcwd(buf, max) getcwd (buf, max, 0)
67 # define __getcwd getcwd
70 # define __getcwd(buf, max) getwd (buf)
72 # define __readlink readlink
75 #if !FUNC_REALPATH_WORKS || defined _LIBC
76 /* Return the canonical absolute name of file NAME. A canonical name
77 does not contain any `.', `..' components nor any repeated path
78 separators ('/') or symlinks. All path components must exist. If
79 RESOLVED is null, the result is malloc'd; otherwise, if the
80 canonical name is PATH_MAX chars or more, returns null with `errno'
81 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
82 returns the name in RESOLVED. If the name cannot be resolved and
83 RESOLVED is non-NULL, it contains the path of the first component
84 that cannot be resolved. If the path can be resolved, RESOLVED
85 holds the same value as the value returned. */
88 __realpath (const char *name, char *resolved)
90 char *rpath, *dest, *extra_buf = NULL;
91 const char *start, *end, *rpath_limit;
97 /* As per Single Unix Specification V2 we must return an error if
98 either parameter is a null pointer. We extend this to allow
99 the RESOLVED parameter to be NULL in case the we are expected to
100 allocate the room for the return value. */
101 __set_errno (EINVAL);
107 /* As per Single Unix Specification V2 we must return an error if
108 the name argument points to an empty string. */
109 __set_errno (ENOENT);
116 path_max = pathconf (name, _PC_PATH_MAX);
121 if (resolved == NULL)
123 rpath = malloc (path_max);
126 /* It's easier to set errno to ENOMEM than to rely on the
127 'malloc-posix' gnulib module. */
134 rpath_limit = rpath + path_max;
138 if (!__getcwd (rpath, path_max))
143 dest = strchr (rpath, '\0');
151 for (start = end = name; *start; start = end)
159 /* Skip sequence of multiple path-separators. */
160 while (*start == '/')
163 /* Find end of path component. */
164 for (end = start; *end && *end != '/'; ++end)
167 if (end - start == 0)
169 else if (end - start == 1 && start[0] == '.')
171 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
173 /* Back up to previous component, ignore if at root already. */
174 if (dest > rpath + 1)
175 while ((--dest)[-1] != '/');
184 if (dest + (end - start) >= rpath_limit)
186 ptrdiff_t dest_offset = dest - rpath;
191 __set_errno (ENAMETOOLONG);
192 if (dest > rpath + 1)
197 new_size = rpath_limit - rpath;
198 if (end - start + 1 > path_max)
199 new_size += end - start + 1;
201 new_size += path_max;
202 new_rpath = (char *) realloc (rpath, new_size);
203 if (new_rpath == NULL)
205 /* It's easier to set errno to ENOMEM than to rely on the
206 'realloc-posix' gnulib module. */
211 rpath_limit = rpath + new_size;
213 dest = rpath + dest_offset;
217 dest = __mempcpy (dest, start, end - start);
219 memcpy (dest, start, end - start);
225 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
227 if (lstat (rpath, &st) < 0)
231 if (S_ISLNK (st.st_mode))
237 if (++num_links > MAXSYMLINKS)
243 buf = malloca (path_max);
250 n = __readlink (rpath, buf, path_max - 1);
253 int saved_errno = errno;
262 extra_buf = malloca (path_max);
272 if ((long int) (n + len) >= path_max)
275 __set_errno (ENAMETOOLONG);
279 /* Careful here, end may be a pointer into extra_buf... */
280 memmove (&extra_buf[n], end, len + 1);
281 name = end = memcpy (extra_buf, buf, n);
284 dest = rpath + 1; /* It's an absolute symlink */
286 /* Back up to previous component, ignore if at root already: */
287 if (dest > rpath + 1)
288 while ((--dest)[-1] != '/');
292 if (dest > rpath + 1 && dest[-1] == '/')
299 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
303 int saved_errno = errno;
307 strcpy (resolved, rpath);
314 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
315 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
318 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
320 __old_realpath (const char *name, char *resolved)
322 if (resolved == NULL)
324 __set_errno (EINVAL);
328 return __realpath (name, resolved);
330 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
335 __canonicalize_file_name (const char *name)
337 return __realpath (name, NULL);
339 weak_alias (__canonicalize_file_name, canonicalize_file_name)
343 /* This declaration is solely to ensure that after preprocessing
344 this file is never empty. */