1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2006 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. */
21 #include "canonicalize.h"
27 # include <sys/param.h>
37 #include "cycle-check.h"
38 #include "filenamecat.h"
39 #include "stat-macros.h"
47 # define __set_errno(Val) errno = (Val)
51 #include "xreadlink.h"
53 #if !HAVE_CANONICALIZE_FILE_NAME
54 /* Return the canonical absolute name of file NAME. A canonical name
55 does not contain any `.', `..' components nor any repeated file name
56 separators ('/') or symlinks. All components must exist.
57 The result is malloc'd. */
60 canonicalize_file_name (const char *name)
64 char *resolved, *extra_buf = NULL;
80 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
81 relative names into absolute ones, so prepend the working
82 directory if the file name is not absolute. */
87 if (!(wd = xgetcwd ()))
90 extra_buf = file_name_concat (wd, name, NULL);
95 resolved_size = strlen (name);
98 resolved_size = 2 * resolved_size + 1;
99 resolved = xmalloc (resolved_size);
100 resolved_len = resolvepath (name, resolved, resolved_size);
101 if (resolved_len < 0)
107 if (resolved_len < resolved_size)
114 /* NUL-terminate the resulting name. */
115 resolved[resolved_len] = '\0';
121 return canonicalize_filename_mode (name, CAN_EXISTING);
123 # endif /* !HAVE_RESOLVEPATH */
125 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
127 /* Return the canonical absolute name of file NAME. A canonical name
128 does not contain any `.', `..' components nor any repeated file name
129 separators ('/') or symlinks. Whether components must exist
130 or not depends on canonicalize mode. The result is malloc'd. */
133 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
135 char *rname, *dest, *extra_buf = NULL;
138 char const *rname_limit;
139 size_t extra_len = 0;
140 struct cycle_check_state cycle_state;
144 __set_errno (EINVAL);
150 __set_errno (ENOENT);
159 dest = strchr (rname, '\0');
160 if (dest - rname < PATH_MAX)
162 char *p = xrealloc (rname, PATH_MAX);
163 dest = p + (dest - rname);
165 rname_limit = rname + PATH_MAX;
174 rname = xmalloc (PATH_MAX);
175 rname_limit = rname + PATH_MAX;
180 cycle_check_init (&cycle_state);
181 for (start = end = name; *start; start = end)
183 /* Skip sequence of multiple file name separators. */
184 while (*start == '/')
187 /* Find end of component. */
188 for (end = start; *end && *end != '/'; ++end)
191 if (end - start == 0)
193 else if (end - start == 1 && start[0] == '.')
195 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
197 /* Back up to previous component, ignore if at root already. */
198 if (dest > rname + 1)
199 while ((--dest)[-1] != '/');
208 if (dest + (end - start) >= rname_limit)
210 ptrdiff_t dest_offset = dest - rname;
211 size_t new_size = rname_limit - rname;
213 if (end - start + 1 > PATH_MAX)
214 new_size += end - start + 1;
216 new_size += PATH_MAX;
217 rname = xrealloc (rname, new_size);
218 rname_limit = rname + new_size;
220 dest = rname + dest_offset;
223 dest = memcpy (dest, start, end - start);
227 if (lstat (rname, &st) != 0)
229 if (can_mode == CAN_EXISTING)
231 if (can_mode == CAN_ALL_BUT_LAST && *end)
236 if (S_ISLNK (st.st_mode))
241 if (cycle_check (&cycle_state, &st))
244 if (can_mode == CAN_MISSING)
250 buf = xreadlink (rname, st.st_size);
253 if (can_mode == CAN_MISSING)
265 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
266 extra_buf = xmalloc (extra_len);
268 else if ((n + len + 1) > extra_len)
270 extra_len = n + len + 1;
271 extra_buf = xrealloc (extra_buf, extra_len);
274 /* Careful here, end may be a pointer into extra_buf... */
275 memmove (&extra_buf[n], end, len + 1);
276 name = end = memcpy (extra_buf, buf, n);
279 dest = rname + 1; /* It's an absolute symlink */
281 /* Back up to previous component, ignore if at root already: */
282 if (dest > rname + 1)
283 while ((--dest)[-1] != '/');
289 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
297 if (dest > rname + 1 && dest[-1] == '/')