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 Lesser 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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)
67 # define MAXSYMLINKS SYMLOOP_MAX
69 # define MAXSYMLINKS 20
74 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
75 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
78 #if !FUNC_REALPATH_WORKS || defined _LIBC
79 /* Return the canonical absolute name of file NAME. A canonical name
80 does not contain any `.', `..' components nor any repeated path
81 separators ('/') or symlinks. All path components must exist. If
82 RESOLVED is null, the result is malloc'd; otherwise, if the
83 canonical name is PATH_MAX chars or more, returns null with `errno'
84 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
85 returns the name in RESOLVED. If the name cannot be resolved and
86 RESOLVED is non-NULL, it contains the path of the first component
87 that cannot be resolved. If the path can be resolved, RESOLVED
88 holds the same value as the value returned. */
91 __realpath (const char *name
, char *resolved
)
93 char *rpath
, *dest
, *extra_buf
= NULL
;
94 const char *start
, *end
, *rpath_limit
;
100 /* As per Single Unix Specification V2 we must return an error if
101 either parameter is a null pointer. We extend this to allow
102 the RESOLVED parameter to be NULL in case the we are expected to
103 allocate the room for the return value. */
104 __set_errno (EINVAL
);
110 /* As per Single Unix Specification V2 we must return an error if
111 the name argument points to an empty string. */
112 __set_errno (ENOENT
);
119 path_max
= pathconf (name
, _PC_PATH_MAX
);
124 if (resolved
== NULL
)
126 rpath
= malloc (path_max
);
129 /* It's easier to set errno to ENOMEM than to rely on the
130 'malloc-posix' gnulib module. */
137 rpath_limit
= rpath
+ path_max
;
141 if (!__getcwd (rpath
, path_max
))
146 dest
= strchr (rpath
, '\0');
152 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& name
[1] == '/')
156 for (start
= end
= name
; *start
; start
= end
)
165 /* Skip sequence of multiple path-separators. */
166 while (*start
== '/')
169 /* Find end of path component. */
170 for (end
= start
; *end
&& *end
!= '/'; ++end
)
173 if (end
- start
== 0)
175 else if (end
- start
== 1 && start
[0] == '.')
177 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
179 /* Back up to previous component, ignore if at root already. */
180 if (dest
> rpath
+ 1)
181 while ((--dest
)[-1] != '/');
182 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 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)
240 if (S_ISLNK (st
.st_mode
))
245 if (++num_links
> MAXSYMLINKS
)
251 buf
= malloca (path_max
);
258 n
= __readlink (rpath
, buf
, path_max
- 1);
261 int saved_errno
= errno
;
270 extra_buf
= malloca (path_max
);
280 if ((long int) (n
+ len
) >= path_max
)
283 __set_errno (ENAMETOOLONG
);
287 /* Careful here, end may be a pointer into extra_buf... */
288 memmove (&extra_buf
[n
], end
, len
+ 1);
289 name
= end
= memcpy (extra_buf
, buf
, n
);
293 dest
= rpath
+ 1; /* It's an absolute symlink */
294 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& buf
[1] == '/')
299 /* Back up to previous component, ignore if at root
301 if (dest
> rpath
+ 1)
302 while ((--dest
)[-1] != '/');
303 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1
308 else if (!S_ISDIR (st
.st_mode
) && *end
!= '\0')
310 __set_errno (ENOTDIR
);
315 if (dest
> rpath
+ 1 && dest
[-1] == '/')
317 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1 && *dest
== '/')
328 int saved_errno
= errno
;
331 if (resolved
== NULL
)
337 versioned_symbol (libc
, __realpath
, realpath
, GLIBC_2_3
);
338 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
341 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
343 attribute_compat_text_section
344 __old_realpath (const char *name
, char *resolved
)
346 if (resolved
== NULL
)
348 __set_errno (EINVAL
);
352 return __realpath (name
, resolved
);
354 compat_symbol (libc
, __old_realpath
, realpath
, GLIBC_2_0
);
359 __canonicalize_file_name (const char *name
)
361 return __realpath (name
, NULL
);
363 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)
367 /* This declaration is solely to ensure that after preprocessing
368 this file is never empty. */