1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2004 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "stat-macros.h"
77 #include "xreadlink.h"
79 #if !HAVE_CANONICALIZE_FILE_NAME
80 /* Return the canonical absolute name of file NAME. A canonical name
81 does not contain any `.', `..' components nor any repeated path
82 separators ('/') or symlinks. All path components must exist.
83 The result is malloc'd. */
86 canonicalize_file_name (const char *name)
90 char *resolved, *extra_buf = NULL;
102 __set_errno (ENOENT);
106 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
107 relative names into absolute ones, so prepend the working
108 directory if the path is not absolute. */
113 if (!(wd = xgetcwd ()))
116 extra_buf = path_concat (wd, name, NULL);
121 resolved_size = strlen (name);
124 resolved_size = 2 * resolved_size + 1;
125 resolved = xmalloc (resolved_size);
126 resolved_len = resolvepath (name, resolved, resolved_size);
127 if (resolved_len < 0)
133 if (resolved_len < resolved_size)
140 /* NUL-terminate the resulting name. */
141 resolved[resolved_len] = '\0';
147 return canonicalize_filename_mode (name, CAN_EXISTING);
149 # endif /* !HAVE_RESOLVEPATH */
151 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
153 /* Return the canonical absolute name of file NAME. A canonical name
154 does not contain any `.', `..' components nor any repeated path
155 separators ('/') or symlinks. Whether path components must exist
156 or not depends on canonicalize mode. The result is malloc'd. */
159 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
161 char *rpath, *dest, *extra_buf = NULL;
162 const char *start, *end, *rpath_limit;
163 size_t extra_len = 0;
164 struct cycle_check_state cycle_state;
168 __set_errno (EINVAL);
174 __set_errno (ENOENT);
183 dest = strchr (rpath, '\0');
184 if (dest - rpath < PATH_MAX)
186 char *p = xrealloc (rpath, PATH_MAX);
187 dest = p + (dest - rpath);
189 rpath_limit = rpath + PATH_MAX;
198 rpath = xmalloc (PATH_MAX);
199 rpath_limit = rpath + PATH_MAX;
204 cycle_check_init (&cycle_state);
205 for (start = end = name; *start; start = end)
207 /* Skip sequence of multiple path-separators. */
208 while (*start == '/')
211 /* Find end of path component. */
212 for (end = start; *end && *end != '/'; ++end)
215 if (end - start == 0)
217 else if (end - start == 1 && start[0] == '.')
219 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
221 /* Back up to previous component, ignore if at root already. */
222 if (dest > rpath + 1)
223 while ((--dest)[-1] != '/');
232 if (dest + (end - start) >= rpath_limit)
234 PTR_INT_TYPE dest_offset = dest - rpath;
235 size_t new_size = rpath_limit - rpath;
237 if (end - start + 1 > PATH_MAX)
238 new_size += end - start + 1;
240 new_size += PATH_MAX;
241 rpath = xrealloc (rpath, new_size);
242 rpath_limit = rpath + new_size;
244 dest = rpath + dest_offset;
247 dest = memcpy (dest, start, end - start);
251 if (lstat (rpath, &st) < 0)
253 if (can_mode == CAN_EXISTING)
255 if (can_mode == CAN_ALL_BUT_LAST && *end)
260 if (S_ISLNK (st.st_mode))
265 if (cycle_check (&cycle_state, &st))
268 if (can_mode == CAN_MISSING)
274 buf = xreadlink (rpath, st.st_size);
277 if (can_mode == CAN_MISSING)
289 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
290 extra_buf = xmalloc (extra_len);
292 else if ((n + len + 1) > extra_len)
294 extra_len = n + len + 1;
295 extra_buf = xrealloc (extra_buf, extra_len);
298 /* Careful here, end may be a pointer into extra_buf... */
299 memmove (&extra_buf[n], end, len + 1);
300 name = end = memcpy (extra_buf, buf, n);
303 dest = rpath + 1; /* It's an absolute symlink */
305 /* Back up to previous component, ignore if at root already: */
306 if (dest > rpath + 1)
307 while ((--dest)[-1] != '/');
313 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
321 if (dest > rpath + 1 && dest[-1] == '/')