1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2011 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 /* When building the relocatable program wrapper, use the system's getcwd
56 function, not the gnulib override, otherwise we would get a link error.
61 /* We want the directory in Unix syntax, not in VMS syntax. */
62 # define __getcwd(buf, max) getcwd (buf, max, 0)
64 # define __getcwd getcwd
67 # define __getcwd(buf, max) getwd (buf)
69 # define __readlink readlink
70 # define __set_errno(e) errno = (e)
71 /* Use the system functions, not the gnulib overrides in this file. */
75 # define MAXSYMLINKS SYMLOOP_MAX
77 # define MAXSYMLINKS 20
82 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
83 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
86 #if !FUNC_REALPATH_WORKS || defined _LIBC
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
;
108 /* As per Single Unix Specification V2 we must return an error if
109 either parameter is a null pointer. We extend this to allow
110 the RESOLVED parameter to be NULL in case the we are expected to
111 allocate the room for the return value. */
112 __set_errno (EINVAL
);
118 /* As per Single Unix Specification V2 we must return an error if
119 the name argument points to an empty string. */
120 __set_errno (ENOENT
);
127 path_max
= pathconf (name
, _PC_PATH_MAX
);
132 if (resolved
== NULL
)
134 rpath
= malloc (path_max
);
137 /* It's easier to set errno to ENOMEM than to rely on the
138 'malloc-posix' gnulib module. */
145 rpath_limit
= rpath
+ path_max
;
149 if (!__getcwd (rpath
, path_max
))
154 dest
= strchr (rpath
, '\0');
160 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& name
[1] == '/')
164 for (start
= end
= name
; *start
; start
= end
)
173 /* Skip sequence of multiple path-separators. */
174 while (*start
== '/')
177 /* Find end of path component. */
178 for (end
= start
; *end
&& *end
!= '/'; ++end
)
181 if (end
- start
== 0)
183 else if (end
- start
== 1 && start
[0] == '.')
185 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
187 /* Back up to previous component, ignore if at root already. */
188 if (dest
> rpath
+ 1)
189 while ((--dest
)[-1] != '/');
190 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1
201 if (dest
+ (end
- start
) >= rpath_limit
)
203 ptrdiff_t dest_offset
= dest
- rpath
;
208 __set_errno (ENAMETOOLONG
);
209 if (dest
> rpath
+ 1)
214 new_size
= rpath_limit
- rpath
;
215 if (end
- start
+ 1 > path_max
)
216 new_size
+= end
- start
+ 1;
218 new_size
+= path_max
;
219 new_rpath
= (char *) realloc (rpath
, new_size
);
220 if (new_rpath
== NULL
)
222 /* It's easier to set errno to ENOMEM than to rely on the
223 'realloc-posix' gnulib module. */
228 rpath_limit
= rpath
+ new_size
;
230 dest
= rpath
+ dest_offset
;
234 dest
= __mempcpy (dest
, start
, end
- start
);
236 memcpy (dest
, start
, end
- start
);
242 if (__lxstat64 (_STAT_VER
, rpath
, &st
) < 0)
244 if (lstat (rpath
, &st
) < 0)
248 if (S_ISLNK (st
.st_mode
))
253 if (++num_links
> MAXSYMLINKS
)
259 buf
= malloca (path_max
);
266 n
= __readlink (rpath
, buf
, path_max
- 1);
269 int saved_errno
= errno
;
278 extra_buf
= malloca (path_max
);
288 if ((long int) (n
+ len
) >= path_max
)
291 __set_errno (ENAMETOOLONG
);
295 /* Careful here, end may be a pointer into extra_buf... */
296 memmove (&extra_buf
[n
], end
, len
+ 1);
297 name
= end
= memcpy (extra_buf
, buf
, n
);
301 dest
= rpath
+ 1; /* It's an absolute symlink */
302 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& buf
[1] == '/')
307 /* Back up to previous component, ignore if at root
309 if (dest
> rpath
+ 1)
310 while ((--dest
)[-1] != '/');
311 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1
316 else if (!S_ISDIR (st
.st_mode
) && *end
!= '\0')
318 __set_errno (ENOTDIR
);
323 if (dest
> rpath
+ 1 && dest
[-1] == '/')
325 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1 && *dest
== '/')
336 int saved_errno
= errno
;
339 if (resolved
== NULL
)
345 versioned_symbol (libc
, __realpath
, realpath
, GLIBC_2_3
);
346 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
349 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
351 attribute_compat_text_section
352 __old_realpath (const char *name
, char *resolved
)
354 if (resolved
== NULL
)
356 __set_errno (EINVAL
);
360 return __realpath (name
, resolved
);
362 compat_symbol (libc
, __old_realpath
, realpath
, GLIBC_2_0
);
367 __canonicalize_file_name (const char *name
)
369 return __realpath (name
, NULL
);
371 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)
375 /* This declaration is solely to ensure that after preprocessing
376 this file is never empty. */