Update.
[glibc.git] / stdlib / canonicalize.c
blobe4f7c9f62895a0b403783137641bc9859fd6937e
1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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>
28 /* Return the canonical absolute name of file NAME. A canonical name
29 does not contain any `.', `..' components nor any repeated path
30 separators ('/') or symlinks. All path components must exist. If
31 RESOLVED is null, the result is malloc'd; otherwise, if the
32 canonical name is PATH_MAX chars or more, returns null with `errno'
33 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
34 returns the name in RESOLVED. If the name cannot be resolved and
35 RESOLVED is non-NULL, it contains the path of the first component
36 that cannot be resolved. If the path can be resolved, RESOLVED
37 holds the same value as the value returned. */
39 static char *
40 canonicalize (const char *name, char *resolved)
42 char *rpath, *dest, *extra_buf = NULL;
43 const char *start, *end, *rpath_limit;
44 long int path_max;
45 int num_links = 0;
47 if (name == NULL)
49 /* As per Single Unix Specification V2 we must return an error if
50 either parameter is a null pointer. We extend this to allow
51 the RESOLVED parameter be NULL in case the we are expected to
52 allocate the room for the return value. */
53 __set_errno (EINVAL);
54 return NULL;
57 if (name[0] == '\0')
59 /* As per Single Unix Specification V2 we must return an error if
60 the name argument points to an empty string. */
61 __set_errno (ENOENT);
62 return NULL;
65 #ifdef PATH_MAX
66 path_max = PATH_MAX;
67 #else
68 path_max = pathconf (name, _PC_PATH_MAX);
69 if (path_max <= 0)
70 path_max = 1024;
71 #endif
73 rpath = resolved ? __alloca (path_max) : malloc (path_max);
74 rpath_limit = rpath + path_max;
76 if (name[0] != '/')
78 if (!__getcwd (rpath, path_max))
79 goto error;
80 dest = strchr (rpath, '\0');
82 else
84 rpath[0] = '/';
85 dest = rpath + 1;
88 for (start = end = name; *start; start = end)
90 struct stat st;
91 int n;
93 /* Skip sequence of multiple path-separators. */
94 while (*start == '/')
95 ++start;
97 /* Find end of path component. */
98 for (end = start; *end && *end != '/'; ++end)
99 /* Nothing. */;
101 if (end - start == 0)
102 break;
103 else if (end - start == 1 && start[0] == '.')
104 /* nothing */;
105 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
107 /* Back up to previous component, ignore if at root already. */
108 if (dest > rpath + 1)
109 while ((--dest)[-1] != '/');
111 else
113 size_t new_size;
115 if (dest[-1] != '/')
116 *dest++ = '/';
118 if (dest + (end - start) >= rpath_limit)
120 ptrdiff_t dest_offset = dest - rpath;
122 if (resolved)
124 __set_errno (ENAMETOOLONG);
125 goto error;
127 new_size = rpath_limit - rpath;
128 if (end - start + 1 > path_max)
129 new_size += end - start + 1;
130 else
131 new_size += path_max;
132 rpath = realloc (rpath, new_size);
133 rpath_limit = rpath + new_size;
134 if (rpath == NULL)
135 return NULL;
137 dest = rpath + dest_offset;
140 dest = __mempcpy (dest, start, end - start);
141 *dest = '\0';
143 if (__lxstat (_STAT_VER, rpath, &st) < 0)
144 goto error;
146 if (S_ISLNK (st.st_mode))
148 char *buf = __alloca (path_max);
149 size_t len;
151 if (++num_links > MAXSYMLINKS)
153 __set_errno (ELOOP);
154 goto error;
157 n = __readlink (rpath, buf, path_max);
158 if (n < 0)
159 goto error;
160 buf[n] = '\0';
162 if (!extra_buf)
163 extra_buf = __alloca (path_max);
165 len = strlen (end);
166 if ((long int) (n + len) >= path_max)
168 __set_errno (ENAMETOOLONG);
169 goto error;
172 /* Careful here, end may be a pointer into extra_buf... */
173 memmove (&extra_buf[n], end, len + 1);
174 name = end = memcpy (extra_buf, buf, n);
176 if (buf[0] == '/')
177 dest = rpath + 1; /* It's an absolute symlink */
178 else
179 /* Back up to previous component, ignore if at root already: */
180 if (dest > rpath + 1)
181 while ((--dest)[-1] != '/');
185 if (dest > rpath + 1 && dest[-1] == '/')
186 --dest;
187 *dest = '\0';
189 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
191 error:
192 if (resolved)
193 strcpy (resolved, rpath);
194 else
195 free (rpath);
196 return NULL;
200 char *
201 __realpath (const char *name, char *resolved)
203 if (resolved == NULL)
205 __set_errno (EINVAL);
206 return NULL;
209 return canonicalize (name, resolved);
211 weak_alias (__realpath, realpath)
214 char *
215 __canonicalize_file_name (const char *name)
217 return canonicalize (name, NULL);
219 weak_alias (__canonicalize_file_name, canonicalize_file_name)