1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2007 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"
46 # define __set_errno(Val) errno = (Val)
50 #include "areadlink.h"
52 #if !HAVE_CANONICALIZE_FILE_NAME
53 /* Return the canonical absolute name of file NAME. A canonical name
54 does not contain any `.', `..' components nor any repeated file name
55 separators ('/') or symlinks. All components must exist.
56 The result is malloc'd. */
59 canonicalize_file_name (const char *name)
63 char *resolved, *extra_buf = NULL;
79 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
80 relative names into absolute ones, so prepend the working
81 directory if the file name is not absolute. */
86 if (!(wd = xgetcwd ()))
89 extra_buf = file_name_concat (wd, name, NULL);
94 resolved_size = strlen (name);
97 resolved_size = 2 * resolved_size + 1;
98 resolved = xmalloc (resolved_size);
99 resolved_len = resolvepath (name, resolved, resolved_size);
100 if (resolved_len < 0)
106 if (resolved_len < resolved_size)
113 /* NUL-terminate the resulting name. */
114 resolved[resolved_len] = '\0';
120 return canonicalize_filename_mode (name, CAN_EXISTING);
122 # endif /* !HAVE_RESOLVEPATH */
124 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
126 /* Return the canonical absolute name of file NAME. A canonical name
127 does not contain any `.', `..' components nor any repeated file name
128 separators ('/') or symlinks. Whether components must exist
129 or not depends on canonicalize mode. The result is malloc'd. */
132 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
134 char *rname, *dest, *extra_buf = NULL;
137 char const *rname_limit;
138 size_t extra_len = 0;
139 struct cycle_check_state cycle_state;
143 __set_errno (EINVAL);
149 __set_errno (ENOENT);
158 dest = strchr (rname, '\0');
159 if (dest - rname < PATH_MAX)
161 char *p = xrealloc (rname, PATH_MAX);
162 dest = p + (dest - rname);
164 rname_limit = rname + PATH_MAX;
173 rname = xmalloc (PATH_MAX);
174 rname_limit = rname + PATH_MAX;
179 cycle_check_init (&cycle_state);
180 for (start = end = name; *start; start = end)
182 /* Skip sequence of multiple file name separators. */
183 while (*start == '/')
186 /* Find end of component. */
187 for (end = start; *end && *end != '/'; ++end)
190 if (end - start == 0)
192 else if (end - start == 1 && start[0] == '.')
194 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
196 /* Back up to previous component, ignore if at root already. */
197 if (dest > rname + 1)
198 while ((--dest)[-1] != '/');
207 if (dest + (end - start) >= rname_limit)
209 ptrdiff_t dest_offset = dest - rname;
210 size_t new_size = rname_limit - rname;
212 if (end - start + 1 > PATH_MAX)
213 new_size += end - start + 1;
215 new_size += PATH_MAX;
216 rname = xrealloc (rname, new_size);
217 rname_limit = rname + new_size;
219 dest = rname + dest_offset;
222 dest = memcpy (dest, start, end - start);
226 if (lstat (rname, &st) != 0)
228 if (can_mode == CAN_EXISTING)
230 if (can_mode == CAN_ALL_BUT_LAST && *end)
235 if (S_ISLNK (st.st_mode))
240 if (cycle_check (&cycle_state, &st))
243 if (can_mode == CAN_MISSING)
249 buf = areadlink_with_size (rname, st.st_size);
252 if (can_mode == CAN_MISSING && errno != ENOMEM)
264 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
265 extra_buf = xmalloc (extra_len);
267 else if ((n + len + 1) > extra_len)
269 extra_len = n + len + 1;
270 extra_buf = xrealloc (extra_buf, extra_len);
273 /* Careful here, end may be a pointer into extra_buf... */
274 memmove (&extra_buf[n], end, len + 1);
275 name = end = memcpy (extra_buf, buf, n);
278 dest = rname + 1; /* It's an absolute symlink */
280 /* Back up to previous component, ignore if at root already: */
281 if (dest > rname + 1)
282 while ((--dest)[-1] != '/');
288 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
296 if (dest > rname + 1 && dest[-1] == '/')