1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2016 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/>. */
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 optimizes away the name == NULL test below. */
21 # define _GL_ARG_NONNULL(params)
23 # define _GL_USE_STDLIB_ALLOC 1
27 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
36 #if HAVE_SYS_PARAM_H || defined _LIBC
37 # include <sys/param.h>
44 # include <shlib-compat.h>
46 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
47 # define versioned_symbol(lib, local, symbol, version) extern int dummy
48 # define compat_symbol(lib, local, symbol, version)
49 # define weak_alias(local, symbol)
50 # define __canonicalize_file_name canonicalize_file_name
51 # define __realpath realpath
57 /* When building the relocatable program wrapper, use the system's getcwd
58 function, not the gnulib override, otherwise we would get a link error.
63 /* We want the directory in Unix syntax, not in VMS syntax. */
64 # define __getcwd(buf, max) getcwd (buf, max, 0)
66 # define __getcwd getcwd
69 # define __getcwd(buf, max) getwd (buf)
71 # define __readlink readlink
72 # define __set_errno(e) errno = (e)
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 /* Define this independently so that stdint.h is not a prerequisite. */
88 # define SIZE_MAX ((size_t) -1)
91 #if !FUNC_REALPATH_WORKS || defined _LIBC
96 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
97 /* Avoid errno problem without using the malloc or realloc modules; see:
98 http://lists.gnu.org/archive/html/bug-gnulib/2016-08/msg00025.html */
103 /* Return the canonical absolute name of file NAME. A canonical name
104 does not contain any ".", ".." components nor any repeated path
105 separators ('/') or symlinks. All path components must exist. If
106 RESOLVED is null, the result is malloc'd; otherwise, if the
107 canonical name is PATH_MAX chars or more, returns null with 'errno'
108 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
109 returns the name in RESOLVED. If the name cannot be resolved and
110 RESOLVED is non-NULL, it contains the path of the first component
111 that cannot be resolved. If the path can be resolved, RESOLVED
112 holds the same value as the value returned. */
115 __realpath (const char *name
, char *resolved
)
117 char *rpath
, *dest
, *extra_buf
= NULL
;
118 const char *start
, *end
, *rpath_limit
;
125 /* As per Single Unix Specification V2 we must return an error if
126 either parameter is a null pointer. We extend this to allow
127 the RESOLVED parameter to be NULL in case the we are expected to
128 allocate the room for the return value. */
129 __set_errno (EINVAL
);
135 /* As per Single Unix Specification V2 we must return an error if
136 the name argument points to an empty string. */
137 __set_errno (ENOENT
);
144 path_max
= pathconf (name
, _PC_PATH_MAX
);
149 if (resolved
== NULL
)
151 rpath
= malloc (path_max
);
160 rpath_limit
= rpath
+ path_max
;
162 /* This is always zero for Posix hosts, but can be 2 for MS-Windows
163 and MS-DOS X:/foo/bar file names. */
164 prefix_len
= FILE_SYSTEM_PREFIX_LEN (name
);
166 if (!IS_ABSOLUTE_FILE_NAME (name
))
168 if (!__getcwd (rpath
, path_max
))
173 dest
= strchr (rpath
, '\0');
175 prefix_len
= FILE_SYSTEM_PREFIX_LEN (rpath
);
182 memcpy (rpath
, name
, prefix_len
);
186 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
188 if (ISSLASH (name
[1]) && !ISSLASH (name
[2]) && !prefix_len
)
192 start
= name
+ prefix_len
;
195 for (end
= start
; *start
; start
= end
)
203 /* Skip sequence of multiple path-separators. */
204 while (ISSLASH (*start
))
207 /* Find end of path component. */
208 for (end
= start
; *end
&& !ISSLASH (*end
); ++end
)
211 if (end
- start
== 0)
213 else if (end
- start
== 1 && start
[0] == '.')
215 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
217 /* Back up to previous component, ignore if at root already. */
218 if (dest
> rpath
+ prefix_len
+ 1)
219 for (--dest
; dest
> rpath
&& !ISSLASH (dest
[-1]); --dest
)
221 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
222 && dest
== rpath
+ 1 && !prefix_len
223 && ISSLASH (*dest
) && !ISSLASH (dest
[1]))
230 if (!ISSLASH (dest
[-1]))
233 if (dest
+ (end
- start
) >= rpath_limit
)
235 ptrdiff_t dest_offset
= dest
- rpath
;
240 __set_errno (ENAMETOOLONG
);
241 if (dest
> rpath
+ prefix_len
+ 1)
246 new_size
= rpath_limit
- rpath
;
247 if (end
- start
+ 1 > path_max
)
248 new_size
+= end
- start
+ 1;
250 new_size
+= path_max
;
251 new_rpath
= (char *) realloc (rpath
, new_size
);
252 if (new_rpath
== NULL
)
258 rpath_limit
= rpath
+ new_size
;
260 dest
= rpath
+ dest_offset
;
264 dest
= __mempcpy (dest
, start
, end
- start
);
266 memcpy (dest
, start
, end
- start
);
272 if (__lxstat64 (_STAT_VER
, rpath
, &st
) < 0)
274 if (lstat (rpath
, &st
) < 0)
278 if (S_ISLNK (st
.st_mode
))
284 if (++num_links
> MAXSYMLINKS
)
290 buf
= malloca (path_max
);
293 __set_errno (ENOMEM
);
297 n
= __readlink (rpath
, buf
, path_max
- 1);
300 int saved_errno
= errno
;
302 __set_errno (saved_errno
);
309 extra_buf
= malloca (path_max
);
313 __set_errno (ENOMEM
);
319 /* Check that n + len + 1 doesn't overflow and is <= path_max. */
320 if (n
>= SIZE_MAX
- len
|| n
+ len
>= path_max
)
323 __set_errno (ENAMETOOLONG
);
327 /* Careful here, end may be a pointer into extra_buf... */
328 memmove (&extra_buf
[n
], end
, len
+ 1);
329 name
= end
= memcpy (extra_buf
, buf
, n
);
331 if (IS_ABSOLUTE_FILE_NAME (buf
))
333 size_t pfxlen
= FILE_SYSTEM_PREFIX_LEN (buf
);
336 memcpy (rpath
, buf
, pfxlen
);
337 dest
= rpath
+ pfxlen
;
338 *dest
++ = '/'; /* It's an absolute symlink */
339 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
341 if (ISSLASH (buf
[1]) && !ISSLASH (buf
[2]) && !pfxlen
)
345 /* Install the new prefix to be in effect hereafter. */
350 /* Back up to previous component, ignore if at root
352 if (dest
> rpath
+ prefix_len
+ 1)
353 for (--dest
; dest
> rpath
&& !ISSLASH (dest
[-1]); --dest
)
355 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1
356 && ISSLASH (*dest
) && !ISSLASH (dest
[1]) && !prefix_len
)
360 else if (!S_ISDIR (st
.st_mode
) && *end
!= '\0')
362 __set_errno (ENOTDIR
);
367 if (dest
> rpath
+ prefix_len
+ 1 && ISSLASH (dest
[-1]))
369 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1 && !prefix_len
370 && ISSLASH (*dest
) && !ISSLASH (dest
[1]))
381 int saved_errno
= errno
;
384 if (resolved
== NULL
)
386 __set_errno (saved_errno
);
390 versioned_symbol (libc
, __realpath
, realpath
, GLIBC_2_3
);
391 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
394 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
396 attribute_compat_text_section
397 __old_realpath (const char *name
, char *resolved
)
399 if (resolved
== NULL
)
401 __set_errno (EINVAL
);
405 return __realpath (name
, resolved
);
407 compat_symbol (libc
, __old_realpath
, realpath
, GLIBC_2_0
);
412 __canonicalize_file_name (const char *name
)
414 return __realpath (name
, NULL
);
416 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)
420 /* This declaration is solely to ensure that after preprocessing
421 this file is never empty. */