canonicalize-lgpl: use native realpath if it works
[gnulib.git] / lib / canonicalize-lgpl.c
1 /* Return the canonical absolute name of a given file.
2    Copyright (C) 1996-2003, 2005-2009 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 #if !HAVE_CANONICALIZE_FILE_NAME || defined _LIBC
21
22 #include <alloca.h>
23
24 /* Specification.  */
25 #include <stdlib.h>
26
27 #include <stddef.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <limits.h>
32
33 #if HAVE_SYS_PARAM_H || defined _LIBC
34 # include <sys/param.h>
35 #endif
36 #ifndef MAXSYMLINKS
37 # ifdef SYMLOOP_MAX
38 #  define MAXSYMLINKS SYMLOOP_MAX
39 # else
40 #  define MAXSYMLINKS 20
41 # endif
42 #endif
43
44 #include <sys/stat.h>
45
46 #include <errno.h>
47 #ifndef _LIBC
48 # define __set_errno(e) errno = (e)
49 #endif
50
51 #ifdef _LIBC
52 # include <shlib-compat.h>
53 #else
54 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
55 # define versioned_symbol(lib, local, symbol, version)
56 # define compat_symbol(lib, local, symbol, version)
57 # define weak_alias(local, symbol)
58 # define __canonicalize_file_name canonicalize_file_name
59 # define __realpath realpath
60 # include "pathmax.h"
61 # include "malloca.h"
62 # if HAVE_GETCWD
63 #  ifdef VMS
64     /* We want the directory in Unix syntax, not in VMS syntax.  */
65 #   define __getcwd(buf, max) getcwd (buf, max, 0)
66 #  else
67 #   define __getcwd getcwd
68 #  endif
69 # else
70 #  define __getcwd(buf, max) getwd (buf)
71 # endif
72 # define __readlink readlink
73 #endif
74
75 #if !FUNC_REALPATH_WORKS || defined _LIBC
76 /* Return the canonical absolute name of file NAME.  A canonical name
77    does not contain any `.', `..' components nor any repeated path
78    separators ('/') or symlinks.  All path components must exist.  If
79    RESOLVED is null, the result is malloc'd; otherwise, if the
80    canonical name is PATH_MAX chars or more, returns null with `errno'
81    set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
82    returns the name in RESOLVED.  If the name cannot be resolved and
83    RESOLVED is non-NULL, it contains the path of the first component
84    that cannot be resolved.  If the path can be resolved, RESOLVED
85    holds the same value as the value returned.  */
86
87 char *
88 __realpath (const char *name, char *resolved)
89 {
90   char *rpath, *dest, *extra_buf = NULL;
91   const char *start, *end, *rpath_limit;
92   long int path_max;
93   int num_links = 0;
94
95   if (name == NULL)
96     {
97       /* As per Single Unix Specification V2 we must return an error if
98          either parameter is a null pointer.  We extend this to allow
99          the RESOLVED parameter to be NULL in case the we are expected to
100          allocate the room for the return value.  */
101       __set_errno (EINVAL);
102       return NULL;
103     }
104
105   if (name[0] == '\0')
106     {
107       /* As per Single Unix Specification V2 we must return an error if
108          the name argument points to an empty string.  */
109       __set_errno (ENOENT);
110       return NULL;
111     }
112
113 #ifdef PATH_MAX
114   path_max = PATH_MAX;
115 #else
116   path_max = pathconf (name, _PC_PATH_MAX);
117   if (path_max <= 0)
118     path_max = 1024;
119 #endif
120
121   if (resolved == NULL)
122     {
123       rpath = malloc (path_max);
124       if (rpath == NULL)
125         {
126           /* It's easier to set errno to ENOMEM than to rely on the
127              'malloc-posix' gnulib module.  */
128           errno = ENOMEM;
129           return NULL;
130         }
131     }
132   else
133     rpath = resolved;
134   rpath_limit = rpath + path_max;
135
136   if (name[0] != '/')
137     {
138       if (!__getcwd (rpath, path_max))
139         {
140           rpath[0] = '\0';
141           goto error;
142         }
143       dest = strchr (rpath, '\0');
144     }
145   else
146     {
147       rpath[0] = '/';
148       dest = rpath + 1;
149     }
150
151   for (start = end = name; *start; start = end)
152     {
153 #ifdef _LIBC
154       struct stat64 st;
155 #else
156       struct stat st;
157 #endif
158
159       /* Skip sequence of multiple path-separators.  */
160       while (*start == '/')
161         ++start;
162
163       /* Find end of path component.  */
164       for (end = start; *end && *end != '/'; ++end)
165         /* Nothing.  */;
166
167       if (end - start == 0)
168         break;
169       else if (end - start == 1 && start[0] == '.')
170         /* nothing */;
171       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
172         {
173           /* Back up to previous component, ignore if at root already.  */
174           if (dest > rpath + 1)
175             while ((--dest)[-1] != '/');
176         }
177       else
178         {
179           size_t new_size;
180
181           if (dest[-1] != '/')
182             *dest++ = '/';
183
184           if (dest + (end - start) >= rpath_limit)
185             {
186               ptrdiff_t dest_offset = dest - rpath;
187               char *new_rpath;
188
189               if (resolved)
190                 {
191                   __set_errno (ENAMETOOLONG);
192                   if (dest > rpath + 1)
193                     dest--;
194                   *dest = '\0';
195                   goto error;
196                 }
197               new_size = rpath_limit - rpath;
198               if (end - start + 1 > path_max)
199                 new_size += end - start + 1;
200               else
201                 new_size += path_max;
202               new_rpath = (char *) realloc (rpath, new_size);
203               if (new_rpath == NULL)
204                 {
205                   /* It's easier to set errno to ENOMEM than to rely on the
206                      'realloc-posix' gnulib module.  */
207                   errno = ENOMEM;
208                   goto error;
209                 }
210               rpath = new_rpath;
211               rpath_limit = rpath + new_size;
212
213               dest = rpath + dest_offset;
214             }
215
216 #ifdef _LIBC
217           dest = __mempcpy (dest, start, end - start);
218 #else
219           memcpy (dest, start, end - start);
220           dest += end - start;
221 #endif
222           *dest = '\0';
223
224 #ifdef _LIBC
225           if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
226 #else
227           if (lstat (rpath, &st) < 0)
228 #endif
229             goto error;
230
231           if (S_ISLNK (st.st_mode))
232             {
233               char *buf;
234               size_t len;
235               int n;
236
237               if (++num_links > MAXSYMLINKS)
238                 {
239                   __set_errno (ELOOP);
240                   goto error;
241                 }
242
243               buf = malloca (path_max);
244               if (!buf)
245                 {
246                   errno = ENOMEM;
247                   goto error;
248                 }
249
250               n = __readlink (rpath, buf, path_max - 1);
251               if (n < 0)
252                 {
253                   int saved_errno = errno;
254                   freea (buf);
255                   errno = saved_errno;
256                   goto error;
257                 }
258               buf[n] = '\0';
259
260               if (!extra_buf)
261                 {
262                   extra_buf = malloca (path_max);
263                   if (!extra_buf)
264                     {
265                       freea (buf);
266                       errno = ENOMEM;
267                       goto error;
268                     }
269                 }
270
271               len = strlen (end);
272               if ((long int) (n + len) >= path_max)
273                 {
274                   freea (buf);
275                   __set_errno (ENAMETOOLONG);
276                   goto error;
277                 }
278
279               /* Careful here, end may be a pointer into extra_buf... */
280               memmove (&extra_buf[n], end, len + 1);
281               name = end = memcpy (extra_buf, buf, n);
282
283               if (buf[0] == '/')
284                 dest = rpath + 1;       /* It's an absolute symlink */
285               else
286                 /* Back up to previous component, ignore if at root already: */
287                 if (dest > rpath + 1)
288                   while ((--dest)[-1] != '/');
289             }
290         }
291     }
292   if (dest > rpath + 1 && dest[-1] == '/')
293     --dest;
294   *dest = '\0';
295
296   if (extra_buf)
297     freea (extra_buf);
298
299   return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
300
301 error:
302   {
303     int saved_errno = errno;
304     if (extra_buf)
305       freea (extra_buf);
306     if (resolved)
307       strcpy (resolved, rpath);
308     else
309       free (rpath);
310     errno = saved_errno;
311   }
312   return NULL;
313 }
314 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
315 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
316
317
318 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
319 char *
320 __old_realpath (const char *name, char *resolved)
321 {
322   if (resolved == NULL)
323     {
324       __set_errno (EINVAL);
325       return NULL;
326     }
327
328   return __realpath (name, resolved);
329 }
330 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
331 #endif
332
333
334 char *
335 __canonicalize_file_name (const char *name)
336 {
337   return __realpath (name, NULL);
338 }
339 weak_alias (__canonicalize_file_name, canonicalize_file_name)
340
341 #else
342
343 /* This declaration is solely to ensure that after preprocessing
344    this file is never empty.  */
345 typedef int dummy;
346
347 #endif