1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2003 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
24 /* Avoid a clash of our rpl_realpath() function with the prototype in
25 <stdlib.h> on Solaris 2.5.1. */
28 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
33 #include "canonicalize.h"
39 #if HAVE_UNISTD_H || defined _LIBC
45 #if HAVE_SYS_PARAM_H || defined _LIBC
46 # include <sys/param.h>
49 # define MAXSYMLINKS 20
56 # define __set_errno(e) errno = (e)
58 # define ENAMETOOLONG EINVAL
63 # include <shlib-compat.h>
65 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
66 # define versioned_symbol(lib, local, symbol, version)
67 # define compat_symbol(lib, local, symbol, version)
68 # define weak_alias(local, symbol)
69 # define __canonicalize_file_name canonicalize_file_name
70 # define __realpath rpl_realpath
75 /* We want the directory in Unix syntax, not in VMS syntax. */
76 # define __getcwd(buf, max) getcwd (buf, max, 0)
78 # define __getcwd getcwd
81 # define __getcwd(buf, max) getwd (buf)
83 # define __readlink readlink
84 /* On systems without symbolic links, call stat() instead of lstat(). */
85 # if !defined S_ISNLK && !HAVE_READLINK
90 /* Return the canonical absolute name of file NAME. A canonical name
91 does not contain any `.', `..' components nor any repeated path
92 separators ('/') or symlinks. All path components must exist. If
93 RESOLVED is null, the result is malloc'd; otherwise, if the
94 canonical name is PATH_MAX chars or more, returns null with `errno'
95 set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
96 returns the name in RESOLVED. If the name cannot be resolved and
97 RESOLVED is non-NULL, it contains the path of the first component
98 that cannot be resolved. If the path can be resolved, RESOLVED
99 holds the same value as the value returned. */
102 __realpath (const char *name
, char *resolved
)
104 char *rpath
, *dest
, *extra_buf
= NULL
;
105 const char *start
, *end
, *rpath_limit
;
111 /* As per Single Unix Specification V2 we must return an error if
112 either parameter is a null pointer. We extend this to allow
113 the RESOLVED parameter to be NULL in case the we are expected to
114 allocate the room for the return value. */
115 __set_errno (EINVAL
);
121 /* As per Single Unix Specification V2 we must return an error if
122 the name argument points to an empty string. */
123 __set_errno (ENOENT
);
130 path_max
= pathconf (name
, _PC_PATH_MAX
);
135 if (resolved
== NULL
)
137 rpath
= malloc (path_max
);
143 rpath_limit
= rpath
+ path_max
;
147 if (!__getcwd (rpath
, path_max
))
152 dest
= strchr (rpath
, '\0');
160 for (start
= end
= name
; *start
; start
= end
)
169 /* Skip sequence of multiple path-separators. */
170 while (*start
== '/')
173 /* Find end of path component. */
174 for (end
= start
; *end
&& *end
!= '/'; ++end
)
177 if (end
- start
== 0)
179 else if (end
- start
== 1 && start
[0] == '.')
181 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
183 /* Back up to previous component, ignore if at root already. */
184 if (dest
> rpath
+ 1)
185 while ((--dest
)[-1] != '/');
194 if (dest
+ (end
- start
) >= rpath_limit
)
196 ptrdiff_t dest_offset
= dest
- rpath
;
201 __set_errno (ENAMETOOLONG
);
202 if (dest
> rpath
+ 1)
207 new_size
= rpath_limit
- rpath
;
208 if (end
- start
+ 1 > path_max
)
209 new_size
+= end
- start
+ 1;
211 new_size
+= path_max
;
212 new_rpath
= (char *) realloc (rpath
, new_size
);
213 if (new_rpath
== NULL
)
216 rpath_limit
= rpath
+ new_size
;
218 dest
= rpath
+ dest_offset
;
222 dest
= __mempcpy (dest
, start
, end
- start
);
224 memcpy (dest
, start
, end
- start
);
230 if (__lxstat64 (_STAT_VER
, rpath
, &st
) < 0)
232 if (lstat (rpath
, &st
) < 0)
237 if (S_ISLNK (st
.st_mode
))
242 if (++num_links
> MAXSYMLINKS
)
248 buf
= allocsa (path_max
);
255 n
= __readlink (rpath
, buf
, path_max
);
258 int saved_errno
= errno
;
267 extra_buf
= allocsa (path_max
);
277 if ((long int) (n
+ len
) >= path_max
)
280 __set_errno (ENAMETOOLONG
);
284 /* Careful here, end may be a pointer into extra_buf... */
285 memmove (&extra_buf
[n
], end
, len
+ 1);
286 name
= end
= memcpy (extra_buf
, buf
, n
);
289 dest
= rpath
+ 1; /* It's an absolute symlink */
291 /* Back up to previous component, ignore if at root already: */
292 if (dest
> rpath
+ 1)
293 while ((--dest
)[-1] != '/');
298 if (dest
> rpath
+ 1 && dest
[-1] == '/')
305 return resolved
? memcpy (resolved
, rpath
, dest
- rpath
+ 1) : rpath
;
309 int saved_errno
= errno
;
313 strcpy (resolved
, rpath
);
321 versioned_symbol (libc
, __realpath
, realpath
, GLIBC_2_3
);
325 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
327 __old_realpath (const char *name
, char *resolved
)
329 if (resolved
== NULL
)
331 __set_errno (EINVAL
);
335 return __realpath (name
, resolved
);
337 compat_symbol (libc
, __old_realpath
, realpath
, GLIBC_2_0
);
342 __canonicalize_file_name (const char *name
)
344 return __realpath (name
, NULL
);
346 weak_alias (__canonicalize_file_name
, canonicalize_file_name
)
350 /* This declaration is solely to ensure that after preprocessing
351 this file is never empty. */