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"
44 # define __set_errno(Val) errno = (Val)
48 #include "xreadlink.h"
50 #if !HAVE_CANONICALIZE_FILE_NAME
51 /* Return the canonical absolute name of file NAME. A canonical name
52 does not contain any `.', `..' components nor any repeated file name
53 separators ('/') or symlinks. All components must exist.
54 The result is malloc'd. */
57 canonicalize_file_name (const char *name)
61 char *resolved, *extra_buf = NULL;
77 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
78 relative names into absolute ones, so prepend the working
79 directory if the file name is not absolute. */
84 if (!(wd = xgetcwd ()))
87 extra_buf = file_name_concat (wd, name, NULL);
92 resolved_size = strlen (name);
95 resolved_size = 2 * resolved_size + 1;
96 resolved = xmalloc (resolved_size);
97 resolved_len = resolvepath (name, resolved, resolved_size);
104 if (resolved_len < resolved_size)
111 /* NUL-terminate the resulting name. */
112 resolved[resolved_len] = '\0';
118 return canonicalize_filename_mode (name, CAN_EXISTING);
120 # endif /* !HAVE_RESOLVEPATH */
122 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
124 /* Return the canonical absolute name of file NAME. A canonical name
125 does not contain any `.', `..' components nor any repeated file name
126 separators ('/') or symlinks. Whether components must exist
127 or not depends on canonicalize mode. The result is malloc'd. */
130 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
132 char *rname, *dest, *extra_buf = NULL;
135 char const *rname_limit;
136 size_t extra_len = 0;
137 struct cycle_check_state cycle_state;
141 __set_errno (EINVAL);
147 __set_errno (ENOENT);
156 dest = strchr (rname, '\0');
157 if (dest - rname < PATH_MAX)
159 char *p = xrealloc (rname, PATH_MAX);
160 dest = p + (dest - rname);
162 rname_limit = rname + PATH_MAX;
171 rname = xmalloc (PATH_MAX);
172 rname_limit = rname + PATH_MAX;
177 cycle_check_init (&cycle_state);
178 for (start = end = name; *start; start = end)
180 /* Skip sequence of multiple file name separators. */
181 while (*start == '/')
184 /* Find end of component. */
185 for (end = start; *end && *end != '/'; ++end)
188 if (end - start == 0)
190 else if (end - start == 1 && start[0] == '.')
192 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
194 /* Back up to previous component, ignore if at root already. */
195 if (dest > rname + 1)
196 while ((--dest)[-1] != '/');
205 if (dest + (end - start) >= rname_limit)
207 ptrdiff_t dest_offset = dest - rname;
208 size_t new_size = rname_limit - rname;
210 if (end - start + 1 > PATH_MAX)
211 new_size += end - start + 1;
213 new_size += PATH_MAX;
214 rname = xrealloc (rname, new_size);
215 rname_limit = rname + new_size;
217 dest = rname + dest_offset;
220 dest = memcpy (dest, start, end - start);
224 if (lstat (rname, &st) != 0)
226 if (can_mode == CAN_EXISTING)
228 if (can_mode == CAN_ALL_BUT_LAST && *end)
233 if (S_ISLNK (st.st_mode))
238 if (cycle_check (&cycle_state, &st))
241 if (can_mode == CAN_MISSING)
247 buf = xreadlink (rname, st.st_size);
250 if (can_mode == CAN_MISSING)
262 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
263 extra_buf = xmalloc (extra_len);
265 else if ((n + len + 1) > extra_len)
267 extra_len = n + len + 1;
268 extra_buf = xrealloc (extra_buf, extra_len);
271 /* Careful here, end may be a pointer into extra_buf... */
272 memmove (&extra_buf[n], end, len + 1);
273 name = end = memcpy (extra_buf, buf, n);
276 dest = rname + 1; /* It's an absolute symlink */
278 /* Back up to previous component, ignore if at root already: */
279 if (dest > rname + 1)
280 while ((--dest)[-1] != '/');
286 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
294 if (dest > rname + 1 && dest[-1] == '/')