1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Return the canonical absolute name of a given file.
4 Copyright (C) 1996-2013 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
22 optimizes away the name == NULL test below. */
23 # define _GL_ARG_NONNULL(params)
25 # define _GL_USE_STDLIB_ALLOC 1
29 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
38 #if HAVE_SYS_PARAM_H || defined _LIBC
39 # include <sys/param.h>
46 # include <shlib-compat.h>
48 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
49 # define versioned_symbol(lib, local, symbol, version) extern int dummy
50 # define compat_symbol(lib, local, symbol, version)
51 # define weak_alias(local, symbol)
52 # define __canonicalize_file_name canonicalize_file_name
53 # define __realpath realpath
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.
65 /* We want the directory in Unix syntax, not in VMS syntax. */
66 # define __getcwd(buf, max) getcwd (buf, max, 0)
68 # define __getcwd getcwd
71 # define __getcwd(buf, max) getwd (buf)
73 # define __readlink readlink
74 # define __set_errno(e) errno = (e)
77 # define MAXSYMLINKS SYMLOOP_MAX
79 # define MAXSYMLINKS 20
84 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
85 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
88 #if !FUNC_REALPATH_WORKS || defined _LIBC
89 /* Return the canonical absolute name of file NAME. A canonical name
90 does not contain any ".", ".." components nor any repeated path
91 separators ('/') or symlinks. All path components must exist. If
92 RESOLVED is null, the result is malloc'd; otherwise, if the
93 canonical name is PATH_MAX chars or more, returns null with 'errno'
94 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
95 returns the name in RESOLVED. If the name cannot be resolved and
96 RESOLVED is non-NULL, it contains the path of the first component
97 that cannot be resolved. If the path can be resolved, RESOLVED
98 holds the same value as the value returned. */
101 __realpath (const char *name, char *resolved)
103 char *rpath, *dest, *extra_buf = NULL;
104 const char *start, *end, *rpath_limit;
111 /* As per Single Unix Specification V2 we must return an error if
112 either parameter is a null pointer. We extend this to allow
113 the RESOLVED parameter to be NULL in case the we are expected to
114 allocate the room for the return value. */
115 __set_errno (EINVAL);
121 /* As per Single Unix Specification V2 we must return an error if
122 the name argument points to an empty string. */
123 __set_errno (ENOENT);
130 path_max = pathconf (name, _PC_PATH_MAX);
135 if (resolved == NULL)
137 rpath = malloc (path_max);
140 /* It's easier to set errno to ENOMEM than to rely on the
141 'malloc-posix' gnulib module. */
148 rpath_limit = rpath + path_max;
150 /* This is always zero for Posix hosts, but can be 2 for MS-Windows
151 and MS-DOS X:/foo/bar file names. */
152 prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
154 if (!IS_ABSOLUTE_FILE_NAME (name))
156 if (!__getcwd (rpath, path_max))
161 dest = strchr (rpath, '\0');
163 prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath);
170 memcpy (rpath, name, prefix_len);
174 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
176 if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len)
180 start = name + prefix_len;
183 for (end = start; *start; start = end)
192 /* Skip sequence of multiple path-separators. */
193 while (ISSLASH (*start))
196 /* Find end of path component. */
197 for (end = start; *end && !ISSLASH (*end); ++end)
200 if (end - start == 0)
202 else if (end - start == 1 && start[0] == '.')
204 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
206 /* Back up to previous component, ignore if at root already. */
207 if (dest > rpath + prefix_len + 1)
208 for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest)
210 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
211 && dest == rpath + 1 && !prefix_len
212 && ISSLASH (*dest) && !ISSLASH (dest[1]))
219 if (!ISSLASH (dest[-1]))
222 if (dest + (end - start) >= rpath_limit)
224 ptrdiff_t dest_offset = dest - rpath;
229 __set_errno (ENAMETOOLONG);
230 if (dest > rpath + prefix_len + 1)
235 new_size = rpath_limit - rpath;
236 if (end - start + 1 > path_max)
237 new_size += end - start + 1;
239 new_size += path_max;
240 new_rpath = (char *) realloc (rpath, new_size);
241 if (new_rpath == NULL)
243 /* It's easier to set errno to ENOMEM than to rely on the
244 'realloc-posix' gnulib module. */
249 rpath_limit = rpath + new_size;
251 dest = rpath + dest_offset;
255 dest = __mempcpy (dest, start, end - start);
257 memcpy (dest, start, end - start);
263 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
265 if (lstat (rpath, &st) < 0)
269 if (S_ISLNK (st.st_mode))
274 if (++num_links > MAXSYMLINKS)
280 buf = malloca (path_max);
287 n = __readlink (rpath, buf, path_max - 1);
290 int saved_errno = errno;
299 extra_buf = malloca (path_max);
309 if ((long int) (n + len) >= path_max)
312 __set_errno (ENAMETOOLONG);
316 /* Careful here, end may be a pointer into extra_buf... */
317 memmove (&extra_buf[n], end, len + 1);
318 name = end = memcpy (extra_buf, buf, n);
320 if (IS_ABSOLUTE_FILE_NAME (buf))
322 size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf);
325 memcpy (rpath, buf, pfxlen);
326 dest = rpath + pfxlen;
327 *dest++ = '/'; /* It's an absolute symlink */
328 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
330 if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen)
334 /* Install the new prefix to be in effect hereafter. */
339 /* Back up to previous component, ignore if at root
341 if (dest > rpath + prefix_len + 1)
342 for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest)
344 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
345 && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len)
349 else if (!S_ISDIR (st.st_mode) && *end != '\0')
351 __set_errno (ENOTDIR);
356 if (dest > rpath + prefix_len + 1 && ISSLASH (dest[-1]))
358 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len
359 && ISSLASH (*dest) && !ISSLASH (dest[1]))
370 int saved_errno = errno;
373 if (resolved == NULL)
379 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
380 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
383 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
385 attribute_compat_text_section
386 __old_realpath (const char *name, char *resolved)
388 if (resolved == NULL)
390 __set_errno (EINVAL);
394 return __realpath (name, resolved);
396 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
401 __canonicalize_file_name (const char *name)
403 return __realpath (name, NULL);
405 weak_alias (__canonicalize_file_name, canonicalize_file_name)
409 /* This declaration is solely to ensure that after preprocessing
410 this file is never empty. */