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 /* Avoid a clash of our rpl_realpath() function with the prototype in
21 <stdlib.h> on Solaris 2.5.1. */
24 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
29 #include "canonicalize.h"
35 #if HAVE_UNISTD_H || defined _LIBC
41 #if HAVE_SYS_PARAM_H || defined _LIBC
42 # include <sys/param.h>
45 # define MAXSYMLINKS 20
52 # define __set_errno(e) errno = (e)
56 # include <shlib-compat.h>
58 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
59 # define versioned_symbol(lib, local, symbol, version)
60 # define compat_symbol(lib, local, symbol, version)
61 # define weak_alias(local, symbol)
62 # define __canonicalize_file_name canonicalize_file_name
63 # define __realpath rpl_realpath
68 /* We want the directory in Unix syntax, not in VMS syntax. */
69 # define __getcwd(buf, max) getcwd (buf, max, 0)
71 # define __getcwd getcwd
74 # define __getcwd(buf, max) getwd (buf)
76 # define __readlink readlink
77 /* On systems without symbolic links, call stat() instead of lstat(). */
78 # if !defined S_ISLNK && !HAVE_READLINK
83 /* Return the canonical absolute name of file NAME. A canonical name
84 does not contain any `.', `..' components nor any repeated path
85 separators ('/') or symlinks. All path components must exist. If
86 RESOLVED is null, the result is malloc'd; otherwise, if the
87 canonical name is PATH_MAX chars or more, returns null with `errno'
88 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
89 returns the name in RESOLVED. If the name cannot be resolved and
90 RESOLVED is non-NULL, it contains the path of the first component
91 that cannot be resolved. If the path can be resolved, RESOLVED
92 holds the same value as the value returned. */
95 __realpath (const char *name, char *resolved)
97 char *rpath, *dest, *extra_buf = NULL;
98 const char *start, *end, *rpath_limit;
106 /* As per Single Unix Specification V2 we must return an error if
107 either parameter is a null pointer. We extend this to allow
108 the RESOLVED parameter to be NULL in case the we are expected to
109 allocate the room for the return value. */
110 __set_errno (EINVAL);
116 /* As per Single Unix Specification V2 we must return an error if
117 the name argument points to an empty string. */
118 __set_errno (ENOENT);
125 path_max = pathconf (name, _PC_PATH_MAX);
130 if (resolved == NULL)
132 rpath = malloc (path_max);
135 /* It's easier to set errno to ENOMEM than to rely on the
136 'malloc-posix' gnulib module. */
143 rpath_limit = rpath + path_max;
147 if (!__getcwd (rpath, path_max))
152 dest = strchr (rpath, '\0');
160 for (start = end = name; *start; start = end)
168 /* Skip sequence of multiple path-separators. */
169 while (*start == '/')
172 /* Find end of path component. */
173 for (end = start; *end && *end != '/'; ++end)
176 if (end - start == 0)
178 else if (end - start == 1 && start[0] == '.')
180 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
182 /* Back up to previous component, ignore if at root already. */
183 if (dest > rpath + 1)
184 while ((--dest)[-1] != '/');
193 if (dest + (end - start) >= rpath_limit)
195 ptrdiff_t dest_offset = dest - rpath;
200 __set_errno (ENAMETOOLONG);
201 if (dest > rpath + 1)
206 new_size = rpath_limit - rpath;
207 if (end - start + 1 > path_max)
208 new_size += end - start + 1;
210 new_size += path_max;
211 new_rpath = (char *) realloc (rpath, new_size);
212 if (new_rpath == NULL)
214 /* It's easier to set errno to ENOMEM than to rely on the
215 'realloc-posix' gnulib module. */
220 rpath_limit = rpath + new_size;
222 dest = rpath + dest_offset;
226 dest = __mempcpy (dest, start, end - start);
228 memcpy (dest, start, end - start);
234 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
236 if (lstat (rpath, &st) < 0)
241 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);
294 dest = rpath + 1; /* It's an absolute symlink */
296 /* Back up to previous component, ignore if at root already: */
297 if (dest > rpath + 1)
298 while ((--dest)[-1] != '/');
303 if (dest > rpath + 1 && dest[-1] == '/')
310 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
314 int saved_errno = errno;
318 strcpy (resolved, rpath);
326 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
330 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
332 __old_realpath (const char *name, char *resolved)
334 if (resolved == NULL)
336 __set_errno (EINVAL);
340 return __realpath (name, resolved);
342 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
347 __canonicalize_file_name (const char *name)
349 return __realpath (name, NULL);
351 weak_alias (__canonicalize_file_name, canonicalize_file_name)
355 /* This declaration is solely to ensure that after preprocessing
356 this file is never empty. */