Update.
[glibc.git] / stdlib / canonicalize.c
blob3394048fb30536d165c9c5c83d1aaa022c0db020
1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <sys/param.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <stddef.h>
29 #include <shlib-compat.h>
31 /* Return the canonical absolute name of file NAME. A canonical name
32 does not contain any `.', `..' components nor any repeated path
33 separators ('/') or symlinks. All path components must exist. If
34 RESOLVED is null, the result is malloc'd; otherwise, if the
35 canonical name is PATH_MAX chars or more, returns null with `errno'
36 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
37 returns the name in RESOLVED. If the name cannot be resolved and
38 RESOLVED is non-NULL, it contains the path of the first component
39 that cannot be resolved. If the path can be resolved, RESOLVED
40 holds the same value as the value returned. */
42 char *
43 __realpath (const char *name, char *resolved)
45 char *rpath, *dest, *extra_buf = NULL;
46 const char *start, *end, *rpath_limit;
47 long int path_max;
48 int num_links = 0;
50 if (name == NULL)
52 /* As per Single Unix Specification V2 we must return an error if
53 either parameter is a null pointer. We extend this to allow
54 the RESOLVED parameter to be NULL in case the we are expected to
55 allocate the room for the return value. */
56 __set_errno (EINVAL);
57 return NULL;
60 if (name[0] == '\0')
62 /* As per Single Unix Specification V2 we must return an error if
63 the name argument points to an empty string. */
64 __set_errno (ENOENT);
65 return NULL;
68 #ifdef PATH_MAX
69 path_max = PATH_MAX;
70 #else
71 path_max = pathconf (name, _PC_PATH_MAX);
72 if (path_max <= 0)
73 path_max = 1024;
74 #endif
76 rpath = resolved ? __alloca (path_max) : malloc (path_max);
77 rpath_limit = rpath + path_max;
79 if (name[0] != '/')
81 if (!__getcwd (rpath, path_max))
83 rpath[0] = '\0';
84 goto error;
86 dest = strchr (rpath, '\0');
88 else
90 rpath[0] = '/';
91 dest = rpath + 1;
94 for (start = end = name; *start; start = end)
96 struct stat64 st;
97 int n;
99 /* Skip sequence of multiple path-separators. */
100 while (*start == '/')
101 ++start;
103 /* Find end of path component. */
104 for (end = start; *end && *end != '/'; ++end)
105 /* Nothing. */;
107 if (end - start == 0)
108 break;
109 else if (end - start == 1 && start[0] == '.')
110 /* nothing */;
111 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
113 /* Back up to previous component, ignore if at root already. */
114 if (dest > rpath + 1)
115 while ((--dest)[-1] != '/');
117 else
119 size_t new_size;
121 if (dest[-1] != '/')
122 *dest++ = '/';
124 if (dest + (end - start) >= rpath_limit)
126 ptrdiff_t dest_offset = dest - rpath;
127 char *new_rpath;
129 if (resolved)
131 __set_errno (ENAMETOOLONG);
132 if (dest > rpath + 1)
133 dest--;
134 *dest = '\0';
135 goto error;
137 new_size = rpath_limit - rpath;
138 if (end - start + 1 > path_max)
139 new_size += end - start + 1;
140 else
141 new_size += path_max;
142 new_rpath = (char *) realloc (rpath, new_size);
143 if (new_rpath == NULL)
144 goto error;
145 rpath = new_rpath;
146 rpath_limit = rpath + new_size;
148 dest = rpath + dest_offset;
151 dest = __mempcpy (dest, start, end - start);
152 *dest = '\0';
154 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
155 goto error;
157 if (S_ISLNK (st.st_mode))
159 char *buf = __alloca (path_max);
160 size_t len;
162 if (++num_links > MAXSYMLINKS)
164 __set_errno (ELOOP);
165 goto error;
168 n = __readlink (rpath, buf, path_max);
169 if (n < 0)
170 goto error;
171 buf[n] = '\0';
173 if (!extra_buf)
174 extra_buf = __alloca (path_max);
176 len = strlen (end);
177 if ((long int) (n + len) >= path_max)
179 __set_errno (ENAMETOOLONG);
180 goto error;
183 /* Careful here, end may be a pointer into extra_buf... */
184 memmove (&extra_buf[n], end, len + 1);
185 name = end = memcpy (extra_buf, buf, n);
187 if (buf[0] == '/')
188 dest = rpath + 1; /* It's an absolute symlink */
189 else
190 /* Back up to previous component, ignore if at root already: */
191 if (dest > rpath + 1)
192 while ((--dest)[-1] != '/');
196 if (dest > rpath + 1 && dest[-1] == '/')
197 --dest;
198 *dest = '\0';
200 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
202 error:
203 if (resolved)
204 strcpy (resolved, rpath);
205 else
206 free (rpath);
207 return NULL;
209 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
212 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
213 char *
214 __old_realpath (const char *name, char *resolved)
216 if (resolved == NULL)
218 __set_errno (EINVAL);
219 return NULL;
222 return __realpath (name, resolved);
224 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
225 #endif
228 char *
229 __canonicalize_file_name (const char *name)
231 return __realpath (name, NULL);
233 weak_alias (__canonicalize_file_name, canonicalize_file_name)