1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2003, 2005-2006 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Avoid a clash of our rpl_realpath() function with the prototype in
22 <stdlib.h> on Solaris 2.5.1. */
25 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
30 #include "canonicalize.h"
36 #if HAVE_UNISTD_H || defined _LIBC
42 #if HAVE_SYS_PARAM_H || defined _LIBC
43 # include <sys/param.h>
46 # define MAXSYMLINKS 20
53 # define __set_errno(e) errno = (e)
55 # define ENAMETOOLONG EINVAL
60 # include <shlib-compat.h>
62 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
63 # define versioned_symbol(lib, local, symbol, version)
64 # define compat_symbol(lib, local, symbol, version)
65 # define weak_alias(local, symbol)
66 # define __canonicalize_file_name canonicalize_file_name
67 # define __realpath rpl_realpath
72 /* We want the directory in Unix syntax, not in VMS syntax. */
73 # define __getcwd(buf, max) getcwd (buf, max, 0)
75 # define __getcwd getcwd
78 # define __getcwd(buf, max) getwd (buf)
80 # define __readlink readlink
81 /* On systems without symbolic links, call stat() instead of lstat(). */
82 # if !defined S_ISNLK && !HAVE_READLINK
87 /* Return the canonical absolute name of file NAME. A canonical name
88 does not contain any `.', `..' components nor any repeated path
89 separators ('/') or symlinks. All path components must exist. If
90 RESOLVED is null, the result is malloc'd; otherwise, if the
91 canonical name is PATH_MAX chars or more, returns null with `errno'
92 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
93 returns the name in RESOLVED. If the name cannot be resolved and
94 RESOLVED is non-NULL, it contains the path of the first component
95 that cannot be resolved. If the path can be resolved, RESOLVED
96 holds the same value as the value returned. */
99 __realpath (const char *name, char *resolved)
101 char *rpath, *dest, *extra_buf = NULL;
102 const char *start, *end, *rpath_limit;
110 /* As per Single Unix Specification V2 we must return an error if
111 either parameter is a null pointer. We extend this to allow
112 the RESOLVED parameter to be NULL in case the we are expected to
113 allocate the room for the return value. */
114 __set_errno (EINVAL);
120 /* As per Single Unix Specification V2 we must return an error if
121 the name argument points to an empty string. */
122 __set_errno (ENOENT);
129 path_max = pathconf (name, _PC_PATH_MAX);
134 if (resolved == NULL)
136 rpath = malloc (path_max);
142 rpath_limit = rpath + path_max;
146 if (!__getcwd (rpath, path_max))
151 dest = strchr (rpath, '\0');
159 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] != '/');
192 if (dest + (end - start) >= rpath_limit)
194 ptrdiff_t dest_offset = dest - rpath;
199 __set_errno (ENAMETOOLONG);
200 if (dest > rpath + 1)
205 new_size = rpath_limit - rpath;
206 if (end - start + 1 > path_max)
207 new_size += end - start + 1;
209 new_size += path_max;
210 new_rpath = (char *) realloc (rpath, new_size);
211 if (new_rpath == NULL)
214 rpath_limit = rpath + new_size;
216 dest = rpath + dest_offset;
220 dest = __mempcpy (dest, start, end - start);
222 memcpy (dest, start, end - start);
228 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
230 if (lstat (rpath, &st) < 0)
235 if (S_ISLNK (st.st_mode))
241 if (++num_links > MAXSYMLINKS)
247 buf = allocsa (path_max);
254 n = __readlink (rpath, buf, path_max);
257 int saved_errno = errno;
266 extra_buf = allocsa (path_max);
276 if ((long int) (n + len) >= path_max)
279 __set_errno (ENAMETOOLONG);
283 /* Careful here, end may be a pointer into extra_buf... */
284 memmove (&extra_buf[n], end, len + 1);
285 name = end = memcpy (extra_buf, buf, n);
288 dest = rpath + 1; /* It's an absolute symlink */
290 /* Back up to previous component, ignore if at root already: */
291 if (dest > rpath + 1)
292 while ((--dest)[-1] != '/');
297 if (dest > rpath + 1 && dest[-1] == '/')
304 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
308 int saved_errno = errno;
312 strcpy (resolved, rpath);
320 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
324 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
326 __old_realpath (const char *name, char *resolved)
328 if (resolved == NULL)
330 __set_errno (EINVAL);
334 return __realpath (name, resolved);
336 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
341 __canonicalize_file_name (const char *name)
343 return __realpath (name, NULL);
345 weak_alias (__canonicalize_file_name, canonicalize_file_name)
349 /* This declaration is solely to ensure that after preprocessing
350 this file is never empty. */