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