.
[coreutils.git] / lib / canonicalize.c
blob6e58b282dd4cba63bf7c112ebf6c53e23611df04
1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2001, 2002, 2003 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)
7 any later version.
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. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 #else
26 void free ();
27 #endif
29 #if defined STDC_HEADERS || defined HAVE_STRING_H
30 # include <string.h>
31 #else
32 # include <strings.h>
33 #endif
35 #if HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
39 #include <sys/stat.h>
41 #include <errno.h>
43 #include "path-concat.h"
44 #include "xalloc.h"
45 #include "xgetcwd.h"
47 #ifndef errno
48 extern int errno;
49 #endif
51 #ifndef __set_errno
52 # define __set_errno(Val) errno = (Val)
53 #endif
55 #if !HAVE_RESOLVEPATH
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__
65 # else
66 # ifdef HAVE_STDDEF_H
67 # include <stddef.h>
68 # define PTR_INT_TYPE ptrdiff_t
69 # else
70 # define PTR_INT_TYPE long
71 # endif
72 # endif
74 # include "pathmax.h"
75 # include "xreadlink.h"
77 # ifdef STAT_MACROS_BROKEN
78 # undef S_ISLNK
79 # endif
81 # ifndef S_ISLNK
82 # ifdef S_IFLNK
83 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
84 # endif
85 # endif
87 #endif /* !HAVE_RESOLVEPATH */
89 /* Return the canonical absolute name of file NAME. A canonical name
90 does not contain any `.', `..' components nor any repeated path
91 separators ('/') or symlinks. All path components must exist.
92 The result is malloc'd. */
94 char *
95 canonicalize_file_name (const char *name)
97 #if HAVE_RESOLVEPATH
99 char *resolved, *extra_buf = NULL;
100 size_t resolved_size;
101 ssize_t resolved_len;
103 #else /* !HAVE_RESOLVEPATH */
105 char *rpath, *dest, *extra_buf = NULL;
106 const char *start, *end, *rpath_limit;
107 size_t extra_len = 0;
108 int num_links = 0;
110 #endif /* !HAVE_RESOLVEPATH */
112 if (name == NULL)
114 __set_errno (EINVAL);
115 return NULL;
118 if (name[0] == '\0')
120 __set_errno (ENOENT);
121 return NULL;
124 #if HAVE_RESOLVEPATH
126 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
127 relative names into absolute ones, so prepend the working
128 directory if the path is not absolute. */
129 if (name[0] != '/')
131 char *wd;
133 if (!(wd = xgetcwd ()))
134 return NULL;
136 extra_buf = path_concat (wd, name, NULL);
137 if (!extra_buf)
138 xalloc_die ();
140 name = extra_buf;
141 free (wd);
144 resolved_size = strlen (name);
145 while (1)
147 resolved_size = 2 * resolved_size + 1;
148 resolved = xmalloc (resolved_size);
149 resolved_len = resolvepath (name, resolved, resolved_size);
150 if (resolved_len < 0)
152 free (resolved);
153 free (extra_buf);
154 return NULL;
156 if (resolved_len < resolved_size)
157 break;
158 free (resolved);
161 free (extra_buf);
163 /* NUL-terminate the resulting name. */
164 resolved[resolved_len] = '\0';
166 return resolved;
168 #else /* !HAVE_RESOLVEPATH */
170 if (name[0] != '/')
172 rpath = xgetcwd ();
173 if (!rpath)
174 return NULL;
175 dest = strchr (rpath, '\0');
176 if (dest - rpath < PATH_MAX)
178 char *p = xrealloc (rpath, PATH_MAX);
179 dest = p + (dest - rpath);
180 rpath = p;
181 rpath_limit = rpath + PATH_MAX;
183 else
185 rpath_limit = dest;
188 else
190 rpath = xmalloc (PATH_MAX);
191 rpath_limit = rpath + PATH_MAX;
192 rpath[0] = '/';
193 dest = rpath + 1;
196 for (start = end = name; *start; start = end)
198 /* Skip sequence of multiple path-separators. */
199 while (*start == '/')
200 ++start;
202 /* Find end of path component. */
203 for (end = start; *end && *end != '/'; ++end)
204 /* Nothing. */;
206 if (end - start == 0)
207 break;
208 else if (end - start == 1 && start[0] == '.')
209 /* nothing */;
210 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
212 /* Back up to previous component, ignore if at root already. */
213 if (dest > rpath + 1)
214 while ((--dest)[-1] != '/');
216 else
218 struct stat st;
220 if (dest[-1] != '/')
221 *dest++ = '/';
223 if (dest + (end - start) >= rpath_limit)
225 PTR_INT_TYPE dest_offset = dest - rpath;
226 size_t new_size = rpath_limit - rpath;
228 if (end - start + 1 > PATH_MAX)
229 new_size += end - start + 1;
230 else
231 new_size += PATH_MAX;
232 rpath = xrealloc (rpath, new_size);
233 rpath_limit = rpath + new_size;
235 dest = rpath + dest_offset;
238 dest = memcpy (dest, start, end - start);
239 dest += end - start;
240 *dest = '\0';
242 if (lstat (rpath, &st) < 0)
243 goto error;
245 # ifdef S_ISLNK
246 if (S_ISLNK (st.st_mode))
248 char *buf;
249 size_t n, len;
251 # ifdef MAXSYMLINKS
252 if (++num_links > MAXSYMLINKS)
254 __set_errno (ELOOP);
255 goto error;
257 # endif /* MAXSYMLINKS */
259 buf = xreadlink (rpath);
260 if (!buf)
261 goto error;
263 n = strlen (buf);
264 len = strlen (end);
266 if (!extra_len)
268 extra_len =
269 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
270 extra_buf = xmalloc (extra_len);
272 else if ((n + len + 1) > extra_len)
274 extra_len = n + len + 1;
275 extra_buf = xrealloc (extra_buf, extra_len);
278 /* Careful here, end may be a pointer into extra_buf... */
279 memmove (&extra_buf[n], end, len + 1);
280 name = end = memcpy (extra_buf, buf, n);
282 if (buf[0] == '/')
283 dest = rpath + 1; /* It's an absolute symlink */
284 else
285 /* Back up to previous component, ignore if at root already: */
286 if (dest > rpath + 1)
287 while ((--dest)[-1] != '/');
289 free (buf);
291 # endif /* S_ISLNK */
294 if (dest > rpath + 1 && dest[-1] == '/')
295 --dest;
296 *dest = '\0';
298 free (extra_buf);
299 return rpath;
301 error:
302 free (extra_buf);
303 free (rpath);
304 return NULL;
305 #endif /* !HAVE_RESOLVEPATH */