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-2011 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/>. */
24 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
26 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
27 optimizes away the name == NULL test below. */
28 #define _GL_ARG_NONNULL(params)
37 #if HAVE_SYS_PARAM_H || defined _LIBC
38 # include <sys/param.h>
45 # include <shlib-compat.h>
47 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
48 # define versioned_symbol(lib, local, symbol, version) extern int dummy
49 # define compat_symbol(lib, local, symbol, version)
50 # define weak_alias(local, symbol)
51 # define __canonicalize_file_name canonicalize_file_name
52 # 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)
73 /* Use the system functions, not the gnulib overrides in this file. */
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;
110 /* As per Single Unix Specification V2 we must return an error if
111 either parameter is a null pointer. We extend this to allow
112 the RESOLVED parameter to be NULL in case the we are expected to
113 allocate the room for the return value. */
114 __set_errno (EINVAL);
120 /* As per Single Unix Specification V2 we must return an error if
121 the name argument points to an empty string. */
122 __set_errno (ENOENT);
129 path_max = pathconf (name, _PC_PATH_MAX);
134 if (resolved == NULL)
136 rpath = malloc (path_max);
139 /* It's easier to set errno to ENOMEM than to rely on the
140 'malloc-posix' gnulib module. */
147 rpath_limit = rpath + path_max;
151 if (!__getcwd (rpath, path_max))
156 dest = strchr (rpath, '\0');
162 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
166 for (start = end = name; *start; start = end)
175 /* Skip sequence of multiple path-separators. */
176 while (*start == '/')
179 /* Find end of path component. */
180 for (end = start; *end && *end != '/'; ++end)
183 if (end - start == 0)
185 else if (end - start == 1 && start[0] == '.')
187 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
189 /* Back up to previous component, ignore if at root already. */
190 if (dest > rpath + 1)
191 while ((--dest)[-1] != '/');
192 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
203 if (dest + (end - start) >= rpath_limit)
205 ptrdiff_t dest_offset = dest - rpath;
210 __set_errno (ENAMETOOLONG);
211 if (dest > rpath + 1)
216 new_size = rpath_limit - rpath;
217 if (end - start + 1 > path_max)
218 new_size += end - start + 1;
220 new_size += path_max;
221 new_rpath = (char *) realloc (rpath, new_size);
222 if (new_rpath == NULL)
224 /* It's easier to set errno to ENOMEM than to rely on the
225 'realloc-posix' gnulib module. */
230 rpath_limit = rpath + new_size;
232 dest = rpath + dest_offset;
236 dest = __mempcpy (dest, start, end - start);
238 memcpy (dest, start, end - start);
244 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
246 if (lstat (rpath, &st) < 0)
250 if (S_ISLNK (st.st_mode))
255 if (++num_links > MAXSYMLINKS)
261 buf = malloca (path_max);
268 n = __readlink (rpath, buf, path_max - 1);
271 int saved_errno = errno;
280 extra_buf = malloca (path_max);
290 if ((long int) (n + len) >= path_max)
293 __set_errno (ENAMETOOLONG);
297 /* Careful here, end may be a pointer into extra_buf... */
298 memmove (&extra_buf[n], end, len + 1);
299 name = end = memcpy (extra_buf, buf, n);
303 dest = rpath + 1; /* It's an absolute symlink */
304 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
309 /* Back up to previous component, ignore if at root
311 if (dest > rpath + 1)
312 while ((--dest)[-1] != '/');
313 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
318 else if (!S_ISDIR (st.st_mode) && *end != '\0')
320 __set_errno (ENOTDIR);
325 if (dest > rpath + 1 && dest[-1] == '/')
327 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/')
338 int saved_errno = errno;
341 if (resolved == NULL)
347 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
348 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
351 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
353 attribute_compat_text_section
354 __old_realpath (const char *name, char *resolved)
356 if (resolved == NULL)
358 __set_errno (EINVAL);
362 return __realpath (name, resolved);
364 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
369 __canonicalize_file_name (const char *name)
371 return __realpath (name, NULL);
373 weak_alias (__canonicalize_file_name, canonicalize_file_name)
377 /* This declaration is solely to ensure that after preprocessing
378 this file is never empty. */