Update.
[glibc.git] / stdlib / canonicalize.c
blob889e24d0c00215625317b90dcfbafba4baf5cfd1
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 <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <errno.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 static char *
41 canonicalize (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 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))
80 goto error;
81 dest = strchr (rpath, '\0');
83 else
85 rpath[0] = '/';
86 dest = rpath + 1;
89 for (start = end = name; *start; start = end)
91 struct stat st;
92 int n;
94 /* Skip sequence of multiple path-separators. */
95 while (*start == '/')
96 ++start;
98 /* Find end of path component. */
99 for (end = start; *end && *end != '/'; ++end)
100 /* Nothing. */;
102 if (end - start == 0)
103 break;
104 else if (end - start == 1 && start[0] == '.')
105 /* nothing */;
106 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
108 /* Back up to previous component, ignore if at root already. */
109 if (dest > rpath + 1)
110 while ((--dest)[-1] != '/');
112 else
114 size_t new_size;
116 if (dest[-1] != '/')
117 *dest++ = '/';
119 if (dest + (end - start) >= rpath_limit)
121 ptrdiff_t dest_offset = dest - rpath;
123 if (resolved)
125 __set_errno (ENAMETOOLONG);
126 goto error;
128 new_size = rpath_limit - rpath;
129 if (end - start + 1 > path_max)
130 new_size += end - start + 1;
131 else
132 new_size += path_max;
133 rpath = realloc (rpath, new_size);
134 rpath_limit = rpath + new_size;
135 if (rpath == NULL)
136 return NULL;
138 dest = rpath + dest_offset;
141 dest = __mempcpy (dest, start, end - start);
142 *dest = '\0';
144 if (__lxstat (_STAT_VER, rpath, &st) < 0)
145 goto error;
147 if (S_ISLNK (st.st_mode))
149 char *buf = __alloca (path_max);
150 size_t len;
152 if (++num_links > MAXSYMLINKS)
154 __set_errno (ELOOP);
155 goto error;
158 n = __readlink (rpath, buf, path_max);
159 if (n < 0)
160 goto error;
161 buf[n] = '\0';
163 if (!extra_buf)
164 extra_buf = __alloca (path_max);
166 len = strlen (end);
167 if ((long int) (n + len) >= path_max)
169 __set_errno (ENAMETOOLONG);
170 goto error;
173 /* Careful here, end may be a pointer into extra_buf... */
174 memmove (&extra_buf[n], end, len + 1);
175 name = end = memcpy (extra_buf, buf, n);
177 if (buf[0] == '/')
178 dest = rpath + 1; /* It's an absolute symlink */
179 else
180 /* Back up to previous component, ignore if at root already: */
181 if (dest > rpath + 1)
182 while ((--dest)[-1] != '/');
186 if (dest > rpath + 1 && dest[-1] == '/')
187 --dest;
188 *dest = '\0';
190 return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
192 error:
193 if (resolved)
194 strcpy (resolved, rpath);
195 else
196 free (rpath);
197 return NULL;
201 char *
202 __realpath (const char *name, char *resolved)
204 if (resolved == NULL)
206 __set_errno (EINVAL);
207 return NULL;
210 return canonicalize (name, resolved);
212 weak_alias (__realpath, realpath)
215 char *
216 __canonicalize_file_name (const char *name)
218 return canonicalize (name, NULL);
220 weak_alias (__canonicalize_file_name, canonicalize_file_name)