1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996, 1997, 1998 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. */
25 #include <sys/param.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. */
41 canonicalize (const char *name
, char *resolved
)
43 char *rpath
, *dest
, *extra_buf
= NULL
;
44 const char *start
, *end
, *rpath_limit
;
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. */
60 /* As per Single Unix Specification V2 we must return an error if
61 the name argument points to an empty string. */
69 path_max
= pathconf (name
, _PC_PATH_MAX
);
74 rpath
= resolved
? __alloca (path_max
) : malloc (path_max
);
75 rpath_limit
= rpath
+ path_max
;
79 if (!__getcwd (rpath
, path_max
))
81 dest
= strchr (rpath
, '\0');
89 for (start
= end
= name
; *start
; start
= end
)
94 /* Skip sequence of multiple path-separators. */
95 while (*start
== '/') ++start
;
97 /* Find end of path component. */
98 for (end
= start
; *end
&& *end
!= '/'; ++end
);
100 if (end
- start
== 0)
102 else if (strncmp (start
, ".", end
- start
) == 0)
104 else if (strncmp (start
, "..", end
- start
) == 0)
106 /* Back up to previous component, ignore if at root already. */
107 if (dest
> rpath
+ 1)
108 while ((--dest
)[-1] != '/');
117 if (dest
+ (end
- start
) >= rpath_limit
)
121 __set_errno (ENAMETOOLONG
);
124 new_size
= rpath_limit
- rpath
;
125 if (end
- start
+ 1 > path_max
)
126 new_size
+= end
- start
+ 1;
128 new_size
+= path_max
;
129 rpath
= realloc (rpath
, new_size
);
130 rpath_limit
= rpath
+ new_size
;
135 dest
= __mempcpy (dest
, start
, end
- start
);
138 if (__lxstat (_STAT_VER
, rpath
, &st
) < 0)
141 if (S_ISLNK (st
.st_mode
))
143 char *buf
= __alloca (path_max
);
146 if (++num_links
> MAXSYMLINKS
)
152 n
= __readlink (rpath
, buf
, path_max
);
158 extra_buf
= __alloca (path_max
);
161 if ((long int) (n
+ len
) >= path_max
)
163 __set_errno (ENAMETOOLONG
);
167 /* Careful here, end may be a pointer into extra_buf... */
168 memmove (&extra_buf
[n
], end
, len
+ 1);
169 memcpy (extra_buf
, buf
, n
);
170 name
= end
= extra_buf
;
173 dest
= rpath
+ 1; /* It's an absolute symlink */
175 /* Back up to previous component, ignore if at root already: */
176 if (dest
> rpath
+ 1)
177 while ((--dest
)[-1] != '/');
181 if (dest
> rpath
+ 1 && dest
[-1] == '/')
185 return resolved
? strcpy (resolved
, rpath
) : rpath
;
189 strcpy (resolved
, rpath
);
197 __realpath (const char *name
, char *resolved
)
199 if (resolved
== NULL
)
201 __set_errno (EINVAL
);
205 return canonicalize (name
, resolved
);
207 weak_alias (__realpath
, realpath
)
211 __canonicalize_file_name (const char *name
)
213 return canonicalize (name
, NULL
);
215 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)