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