Update.
[glibc.git] / stdlib / canonicalize.c
blob0985e250b8b62c310d85cdc65ed547f3010e4637
1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996 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 #ifdef PATH_MAX
49 path_max = PATH_MAX;
50 #else
51 path_max = pathconf (name, _PC_PATH_MAX);
52 if (path_max <= 0)
53 path_max = 1024;
54 #endif
56 rpath = resolved ? __alloca (path_max) : malloc (path_max);
57 rpath_limit = rpath + path_max;
59 if (name[0] != '/')
61 if (!getcwd (rpath, path_max))
62 goto error;
63 dest = strchr (rpath, '\0');
65 else
67 rpath[0] = '/';
68 dest = rpath + 1;
71 for (start = end = name; *start; start = end)
73 struct stat st;
74 int n;
76 /* skip sequence of multiple path-separators: */
77 while (*start == '/') ++start;
79 /* find end of path component: */
80 for (end = start; *end && *end != '/'; ++end);
82 if (end - start == 0)
83 break;
84 else if (strncmp (start, ".", end - start) == 0)
85 /* nothing */;
86 else if (strncmp (start, "..", end - start) == 0) {
87 /* back up to previous component, ignore if at root already: */
88 if (dest > rpath + 1)
89 while ((--dest)[-1] != '/');
90 } else
92 size_t new_size;
94 if (dest[-1] != '/')
95 *dest++ = '/';
97 if (dest + (end - start) >= rpath_limit)
99 if (resolved)
101 __set_errno (ENAMETOOLONG);
102 goto error;
104 new_size = rpath_limit - rpath;
105 if (end - start + 1 > path_max)
106 new_size += end - start + 1;
107 else
108 new_size += path_max;
109 rpath = realloc (rpath, new_size);
110 rpath_limit = rpath + new_size;
111 if (!rpath)
112 return NULL;
115 memcpy (dest, start, end - start);
116 dest += end - start;
117 *dest = '\0';
119 if (__lstat (rpath, &st) < 0)
120 goto error;
122 if (S_ISLNK (st.st_mode))
124 char *buf = __alloca (path_max);
126 if (++num_links > MAXSYMLINKS)
128 __set_errno (ELOOP);
129 goto error;
132 n = readlink (rpath, buf, path_max);
133 if (n < 0)
134 goto error;
135 buf[n] = '\0';
137 if (!extra_buf)
138 extra_buf = __alloca (path_max);
140 if ((long int) (n + strlen (end)) >= path_max)
142 __set_errno (ENAMETOOLONG);
143 goto error;
146 /* careful here, end may be a pointer into extra_buf... */
147 strcat (buf, end);
148 strcpy (extra_buf, buf);
149 name = end = extra_buf;
151 if (buf[0] == '/')
152 dest = rpath + 1; /* it's an absolute symlink */
153 else
154 /* back up to previous component, ignore if at root already: */
155 if (dest > rpath + 1)
156 while ((--dest)[-1] != '/');
158 else
159 num_links = 0;
162 if (dest > rpath + 1 && dest[-1] == '/')
163 --dest;
164 *dest = '\0';
166 return resolved ? strcpy (resolved, rpath) : rpath;
168 error:
169 if (resolved)
170 strcpy (resolved, rpath);
171 else
172 free (rpath);
173 return NULL;
175 weak_alias (canonicalize, realpath)
178 char *
179 canonicalize_file_name (const char *name)
181 return canonicalize (name, NULL);