Update.
[glibc.git] / stdlib / canonicalize.c
blobc859288147224bd77eb684335d3a27083467ecfa
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 /* Return the canonical absolute name of file NAME. A canonical name
30 does not contain any `.', `..' components nor any repeated path
31 separators ('/') or symlinks. All path components must exist. If
32 RESOLVED is null, the result is malloc'd; otherwise, if the
33 canonical name is PATH_MAX chars or more, returns null with `errno'
34 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
35 returns the name in RESOLVED. If the name cannot be resolved and
36 RESOLVED is non-NULL, it contains the path of the first component
37 that cannot be resolved. If the path can be resolved, RESOLVED
38 holds the same value as the value returned. */
40 char *
41 __realpath (const char *name, char *resolved)
43 char *rpath, *dest, *extra_buf = NULL;
44 const char *start, *end, *rpath_limit;
45 long int path_max;
46 int num_links = 0;
48 if (name == NULL)
50 /* As per Single Unix Specification V2 we must return an error if
51 either parameter is a null pointer. We extend this to allow
52 the RESOLVED parameter to be NULL in case the we are expected to
53 allocate the room for the return value. */
54 __set_errno (EINVAL);
55 return NULL;
58 if (name[0] == '\0')
60 /* As per Single Unix Specification V2 we must return an error if
61 the name argument points to an empty string. */
62 __set_errno (ENOENT);
63 return NULL;
66 #ifdef PATH_MAX
67 path_max = PATH_MAX;
68 #else
69 path_max = pathconf (name, _PC_PATH_MAX);
70 if (path_max <= 0)
71 path_max = 1024;
72 #endif
74 rpath = resolved ? __alloca (path_max) : malloc (path_max);
75 rpath_limit = rpath + path_max;
77 if (name[0] != '/')
79 if (!__getcwd (rpath, path_max))
81 rpath[0] = '\0';
82 goto error;
84 dest = strchr (rpath, '\0');
86 else
88 rpath[0] = '/';
89 dest = rpath + 1;
92 for (start = end = name; *start; start = end)
94 struct stat64 st;
95 int n;
97 /* Skip sequence of multiple path-separators. */
98 while (*start == '/')
99 ++start;
101 /* Find end of path component. */
102 for (end = start; *end && *end != '/'; ++end)
103 /* Nothing. */;
105 if (end - start == 0)
106 break;
107 else if (end - start == 1 && start[0] == '.')
108 /* nothing */;
109 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
111 /* Back up to previous component, ignore if at root already. */
112 if (dest > rpath + 1)
113 while ((--dest)[-1] != '/');
115 else
117 size_t new_size;
119 if (dest[-1] != '/')
120 *dest++ = '/';
122 if (dest + (end - start) >= rpath_limit)
124 ptrdiff_t dest_offset = dest - rpath;
125 char *new_rpath;
127 if (resolved)
129 __set_errno (ENAMETOOLONG);
130 if (dest > rpath + 1)
131 dest--;
132 *dest = '\0';
133 goto error;
135 new_size = rpath_limit - rpath;
136 if (end - start + 1 > path_max)
137 new_size += end - start + 1;
138 else
139 new_size += path_max;
140 new_rpath = (char *) realloc (rpath, new_size);
141 if (new_rpath == NULL)
142 goto error;
143 rpath = new_rpath;
144 rpath_limit = rpath + new_size;
146 dest = rpath + dest_offset;
149 dest = __mempcpy (dest, start, end - start);
150 *dest = '\0';
152 if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
153 goto error;
155 if (S_ISLNK (st.st_mode))
157 char *buf = __alloca (path_max);
158 size_t len;
160 if (++num_links > MAXSYMLINKS)
162 __set_errno (ELOOP);
163 goto error;
166 n = __readlink (rpath, buf, path_max);
167 if (n < 0)
168 goto error;
169 buf[n] = '\0';
171 if (!extra_buf)
172 extra_buf = __alloca (path_max);
174 len = strlen (end);
175 if ((long int) (n + len) >= path_max)
177 __set_errno (ENAMETOOLONG);
178 goto error;
181 /* Careful here, end may be a pointer into extra_buf... */
182 memmove (&extra_buf[n], end, len + 1);
183 name = end = memcpy (extra_buf, buf, n);
185 if (buf[0] == '/')
186 dest = rpath + 1; /* It's an absolute symlink */
187 else
188 /* Back up to previous component, ignore if at root already: */
189 if (dest > rpath + 1)
190 while ((--dest)[-1] != '/');
194 if (dest > rpath + 1 && dest[-1] == '/')
195 --dest;
196 *dest = '\0';
198 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
200 error:
201 if (resolved)
202 strcpy (resolved, rpath);
203 else
204 free (rpath);
205 return NULL;
207 weak_alias (__realpath, realpath)
210 char *
211 __canonicalize_file_name (const char *name)
213 return __realpath (name, NULL);
215 weak_alias (__canonicalize_file_name, canonicalize_file_name)