1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2020 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 <https://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
54 # include "filename.h"
55 # if defined _WIN32 && !defined __CYGWIN__
56 # define __getcwd _getcwd
59 /* When building the relocatable program wrapper, use the system's getcwd
60 function, not the gnulib override, otherwise we would get a link error.
64 # if defined VMS && !defined getcwd
65 /* We want the directory in Unix syntax, not in VMS syntax.
66 The gnulib override of 'getcwd' takes 2 arguments; the original VMS
67 'getcwd' takes 3 arguments. */
68 # define __getcwd(buf, max) getcwd (buf, max, 0)
70 # define __getcwd getcwd
73 # define __getcwd(buf, max) getwd (buf)
75 # define __readlink readlink
76 # define __set_errno(e) errno = (e)
79 # define MAXSYMLINKS SYMLOOP_MAX
81 # define MAXSYMLINKS 20
86 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
87 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
90 /* Define this independently so that stdint.h is not a prerequisite. */
92 # define SIZE_MAX ((size_t) -1)
95 #if !FUNC_REALPATH_WORKS || defined _LIBC
100 #if defined _WIN32 && ! defined __CYGWIN__
101 /* Avoid errno problem without using the malloc or realloc modules; see:
102 https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */
107 /* Return the canonical absolute name of file NAME. A canonical name
108 does not contain any ".", ".." components nor any repeated path
109 separators ('/') or symlinks. All path components must exist. If
110 RESOLVED is null, the result is malloc'd; otherwise, if the
111 canonical name is PATH_MAX chars or more, returns null with 'errno'
112 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
113 returns the name in RESOLVED. If the name cannot be resolved and
114 RESOLVED is non-NULL, it contains the path of the first component
115 that cannot be resolved. If the path can be resolved, RESOLVED
116 holds the same value as the value returned. */
119 __realpath (const char *name
, char *resolved
)
121 char *rpath
, *dest
, *extra_buf
= NULL
;
122 const char *start
, *end
, *rpath_limit
;
129 /* As per Single Unix Specification V2 we must return an error if
130 either parameter is a null pointer. We extend this to allow
131 the RESOLVED parameter to be NULL in case the we are expected to
132 allocate the room for the return value. */
133 __set_errno (EINVAL
);
139 /* As per Single Unix Specification V2 we must return an error if
140 the name argument points to an empty string. */
141 __set_errno (ENOENT
);
148 path_max
= pathconf (name
, _PC_PATH_MAX
);
153 if (resolved
== NULL
)
155 rpath
= malloc (path_max
);
164 rpath_limit
= rpath
+ path_max
;
166 /* This is always zero for Posix hosts, but can be 2 for MS-Windows
167 and MS-DOS X:/foo/bar file names. */
168 prefix_len
= FILE_SYSTEM_PREFIX_LEN (name
);
170 if (!IS_ABSOLUTE_FILE_NAME (name
))
172 if (!__getcwd (rpath
, path_max
))
177 dest
= strchr (rpath
, '\0');
179 prefix_len
= FILE_SYSTEM_PREFIX_LEN (rpath
);
186 memcpy (rpath
, name
, prefix_len
);
190 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
192 if (ISSLASH (name
[1]) && !ISSLASH (name
[2]) && !prefix_len
)
196 start
= name
+ prefix_len
;
199 for (end
= start
; *start
; start
= end
)
207 /* Skip sequence of multiple path-separators. */
208 while (ISSLASH (*start
))
211 /* Find end of path component. */
212 for (end
= start
; *end
&& !ISSLASH (*end
); ++end
)
215 if (end
- start
== 0)
217 else if (end
- start
== 1 && start
[0] == '.')
219 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
221 /* Back up to previous component, ignore if at root already. */
222 if (dest
> rpath
+ prefix_len
+ 1)
223 for (--dest
; dest
> rpath
&& !ISSLASH (dest
[-1]); --dest
)
225 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
226 && dest
== rpath
+ 1 && !prefix_len
227 && ISSLASH (*dest
) && !ISSLASH (dest
[1]))
234 if (!ISSLASH (dest
[-1]))
237 if (dest
+ (end
- start
) >= rpath_limit
)
239 ptrdiff_t dest_offset
= dest
- rpath
;
244 __set_errno (ENAMETOOLONG
);
245 if (dest
> rpath
+ prefix_len
+ 1)
250 new_size
= rpath_limit
- rpath
;
251 if (end
- start
+ 1 > path_max
)
252 new_size
+= end
- start
+ 1;
254 new_size
+= path_max
;
255 new_rpath
= (char *) realloc (rpath
, new_size
);
256 if (new_rpath
== NULL
)
262 rpath_limit
= rpath
+ new_size
;
264 dest
= rpath
+ dest_offset
;
268 dest
= __mempcpy (dest
, start
, end
- start
);
270 memcpy (dest
, start
, end
- start
);
275 /* FIXME: if lstat fails with errno == EOVERFLOW,
278 if (__lxstat64 (_STAT_VER
, rpath
, &st
) < 0)
280 if (lstat (rpath
, &st
) < 0)
284 if (S_ISLNK (st
.st_mode
))
290 if (++num_links
> MAXSYMLINKS
)
296 buf
= malloca (path_max
);
299 __set_errno (ENOMEM
);
303 n
= __readlink (rpath
, buf
, path_max
- 1);
306 int saved_errno
= errno
;
308 __set_errno (saved_errno
);
315 extra_buf
= malloca (path_max
);
319 __set_errno (ENOMEM
);
325 /* Check that n + len + 1 doesn't overflow and is <= path_max. */
326 if (n
>= SIZE_MAX
- len
|| n
+ len
>= path_max
)
329 __set_errno (ENAMETOOLONG
);
333 /* Careful here, end may be a pointer into extra_buf... */
334 memmove (&extra_buf
[n
], end
, len
+ 1);
335 name
= end
= memcpy (extra_buf
, buf
, n
);
337 if (IS_ABSOLUTE_FILE_NAME (buf
))
339 size_t pfxlen
= FILE_SYSTEM_PREFIX_LEN (buf
);
342 memcpy (rpath
, buf
, pfxlen
);
343 dest
= rpath
+ pfxlen
;
344 *dest
++ = '/'; /* It's an absolute symlink */
345 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
347 if (ISSLASH (buf
[1]) && !ISSLASH (buf
[2]) && !pfxlen
)
351 /* Install the new prefix to be in effect hereafter. */
356 /* Back up to previous component, ignore if at root
358 if (dest
> rpath
+ prefix_len
+ 1)
359 for (--dest
; dest
> rpath
&& !ISSLASH (dest
[-1]); --dest
)
361 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1
362 && ISSLASH (*dest
) && !ISSLASH (dest
[1]) && !prefix_len
)
366 else if (!S_ISDIR (st
.st_mode
) && *end
!= '\0')
368 __set_errno (ENOTDIR
);
373 if (dest
> rpath
+ prefix_len
+ 1 && ISSLASH (dest
[-1]))
375 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rpath
+ 1 && !prefix_len
376 && ISSLASH (*dest
) && !ISSLASH (dest
[1]))
387 int saved_errno
= errno
;
390 if (resolved
== NULL
)
392 __set_errno (saved_errno
);
396 versioned_symbol (libc
, __realpath
, realpath
, GLIBC_2_3
);
397 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
400 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
402 attribute_compat_text_section
403 __old_realpath (const char *name
, char *resolved
)
405 if (resolved
== NULL
)
407 __set_errno (EINVAL
);
411 return __realpath (name
, resolved
);
413 compat_symbol (libc
, __old_realpath
, realpath
, GLIBC_2_0
);
418 __canonicalize_file_name (const char *name
)
420 return __realpath (name
, NULL
);
422 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)
426 /* This declaration is solely to ensure that after preprocessing
427 this file is never empty. */