1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 #if defined STDC_HEADERS || defined HAVE_STRING_H
36 # include <sys/param.h>
47 #include "cycle-check.h"
48 #include "path-concat.h"
49 #include "stat-macros.h"
54 # define __set_errno(Val) errno = (Val)
57 /* If __PTRDIFF_TYPE__ is
58 defined, as with GNU C, use that; that way we don't pollute the
59 namespace with <stddef.h>'s symbols. Otherwise, if <stddef.h> is
60 available, include it and use ptrdiff_t. In traditional C, long is
61 the best that we can do. */
63 #ifdef __PTRDIFF_TYPE__
64 # define PTR_INT_TYPE __PTRDIFF_TYPE__
68 # define PTR_INT_TYPE ptrdiff_t
70 # define PTR_INT_TYPE long
74 #include "canonicalize.h"
76 #include "xreadlink.h"
78 #if !HAVE_CANONICALIZE_FILE_NAME
79 /* Return the canonical absolute name of file NAME. A canonical name
80 does not contain any `.', `..' components nor any repeated path
81 separators ('/') or symlinks. All path components must exist.
82 The result is malloc'd. */
85 canonicalize_file_name (const char *name)
89 char *resolved, *extra_buf = NULL;
101 __set_errno (ENOENT);
105 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
106 relative names into absolute ones, so prepend the working
107 directory if the path is not absolute. */
112 if (!(wd = xgetcwd ()))
115 extra_buf = path_concat (wd, name, NULL);
120 resolved_size = strlen (name);
123 resolved_size = 2 * resolved_size + 1;
124 resolved = xmalloc (resolved_size);
125 resolved_len = resolvepath (name, resolved, resolved_size);
126 if (resolved_len < 0)
132 if (resolved_len < resolved_size)
139 /* NUL-terminate the resulting name. */
140 resolved[resolved_len] = '\0';
146 return canonicalize_filename_mode (name, CAN_EXISTING);
148 # endif /* !HAVE_RESOLVEPATH */
150 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
152 /* Return the canonical absolute name of file NAME. A canonical name
153 does not contain any `.', `..' components nor any repeated path
154 separators ('/') or symlinks. Whether path components must exist
155 or not depends on canonicalize mode. The result is malloc'd. */
158 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
160 char *rpath, *dest, *extra_buf = NULL;
161 const char *start, *end, *rpath_limit;
162 size_t extra_len = 0;
163 struct cycle_check_state cycle_state;
167 __set_errno (EINVAL);
173 __set_errno (ENOENT);
182 dest = strchr (rpath, '\0');
183 if (dest - rpath < PATH_MAX)
185 char *p = xrealloc (rpath, PATH_MAX);
186 dest = p + (dest - rpath);
188 rpath_limit = rpath + PATH_MAX;
197 rpath = xmalloc (PATH_MAX);
198 rpath_limit = rpath + PATH_MAX;
203 cycle_check_init (&cycle_state);
204 for (start = end = name; *start; start = end)
206 /* Skip sequence of multiple path-separators. */
207 while (*start == '/')
210 /* Find end of path component. */
211 for (end = start; *end && *end != '/'; ++end)
214 if (end - start == 0)
216 else if (end - start == 1 && start[0] == '.')
218 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
220 /* Back up to previous component, ignore if at root already. */
221 if (dest > rpath + 1)
222 while ((--dest)[-1] != '/');
231 if (dest + (end - start) >= rpath_limit)
233 PTR_INT_TYPE dest_offset = dest - rpath;
234 size_t new_size = rpath_limit - rpath;
236 if (end - start + 1 > PATH_MAX)
237 new_size += end - start + 1;
239 new_size += PATH_MAX;
240 rpath = xrealloc (rpath, new_size);
241 rpath_limit = rpath + new_size;
243 dest = rpath + dest_offset;
246 dest = memcpy (dest, start, end - start);
250 if (lstat (rpath, &st) < 0)
252 if (can_mode == CAN_EXISTING)
254 if (can_mode == CAN_ALL_BUT_LAST && *end)
259 if (S_ISLNK (st.st_mode))
264 if (cycle_check (&cycle_state, &st))
267 if (can_mode == CAN_MISSING)
273 buf = xreadlink (rpath, st.st_size);
276 if (can_mode == CAN_MISSING)
288 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
289 extra_buf = xmalloc (extra_len);
291 else if ((n + len + 1) > extra_len)
293 extra_len = n + len + 1;
294 extra_buf = xrealloc (extra_buf, extra_len);
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);
302 dest = rpath + 1; /* It's an absolute symlink */
304 /* Back up to previous component, ignore if at root already: */
305 if (dest > rpath + 1)
306 while ((--dest)[-1] != '/');
312 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
320 if (dest > rpath + 1 && dest[-1] == '/')