canonicalize, canonicalize-lgpl: use <stdlib.h>
[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 /* Return the canonical absolute name of file NAME.  A canonical name
76    does not contain any `.', `..' components nor any repeated path
77    separators ('/') or symlinks.  All path components must exist.  If
78    RESOLVED is null, the result is malloc'd; otherwise, if the
79    canonical name is PATH_MAX chars or more, returns null with `errno'
80    set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
81    returns the name in RESOLVED.  If the name cannot be resolved and
82    RESOLVED is non-NULL, it contains the path of the first component
83    that cannot be resolved.  If the path can be resolved, RESOLVED
84    holds the same value as the value returned.  */
85
86 char *
87 __realpath (const char *name, char *resolved)
88 {
89   char *rpath, *dest, *extra_buf = NULL;
90   const char *start, *end, *rpath_limit;
91   long int path_max;
92   int num_links = 0;
93
94   if (name == NULL)
95     {
96       /* As per Single Unix Specification V2 we must return an error if
97          either parameter is a null pointer.  We extend this to allow
98          the RESOLVED parameter to be NULL in case the we are expected to
99          allocate the room for the return value.  */
100       __set_errno (EINVAL);
101       return NULL;
102     }
103
104   if (name[0] == '\0')
105     {
106       /* As per Single Unix Specification V2 we must return an error if
107          the name argument points to an empty string.  */
108       __set_errno (ENOENT);
109       return NULL;
110     }
111
112 #ifdef PATH_MAX
113   path_max = PATH_MAX;
114 #else
115   path_max = pathconf (name, _PC_PATH_MAX);
116   if (path_max <= 0)
117     path_max = 1024;
118 #endif
119
120   if (resolved == NULL)
121     {
122       rpath = malloc (path_max);
123       if (rpath == NULL)
124         {
125           /* It's easier to set errno to ENOMEM than to rely on the
126              'malloc-posix' gnulib module.  */
127           errno = ENOMEM;
128           return NULL;
129         }
130     }
131   else
132     rpath = resolved;
133   rpath_limit = rpath + path_max;
134
135   if (name[0] != '/')
136     {
137       if (!__getcwd (rpath, path_max))
138         {
139           rpath[0] = '\0';
140           goto error;
141         }
142       dest = strchr (rpath, '\0');
143     }
144   else
145     {
146       rpath[0] = '/';
147       dest = rpath + 1;
148     }
149
150   for (start = end = name; *start; start = end)
151     {
152 #ifdef _LIBC
153       struct stat64 st;
154 #else
155       struct stat st;
156 #endif
157
158       /* Skip sequence of multiple path-separators.  */
159       while (*start == '/')
160         ++start;
161
162       /* Find end of path component.  */
163       for (end = start; *end && *end != '/'; ++end)
164         /* Nothing.  */;
165
166       if (end - start == 0)
167         break;
168       else if (end - start == 1 && start[0] == '.')
169         /* nothing */;
170       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
171         {
172           /* Back up to previous component, ignore if at root already.  */
173           if (dest > rpath + 1)
174             while ((--dest)[-1] != '/');
175         }
176       else
177         {
178           size_t new_size;
179
180           if (dest[-1] != '/')
181             *dest++ = '/';
182
183           if (dest + (end - start) >= rpath_limit)
184             {
185               ptrdiff_t dest_offset = dest - rpath;
186               char *new_rpath;
187
188               if (resolved)
189                 {
190                   __set_errno (ENAMETOOLONG);
191                   if (dest > rpath + 1)
192                     dest--;
193                   *dest = '\0';
194                   goto error;
195                 }
196               new_size = rpath_limit - rpath;
197               if (end - start + 1 > path_max)
198                 new_size += end - start + 1;
199               else
200                 new_size += path_max;
201               new_rpath = (char *) realloc (rpath, new_size);
202               if (new_rpath == NULL)
203                 {
204                   /* It's easier to set errno to ENOMEM than to rely on the
205                      'realloc-posix' gnulib module.  */
206                   errno = ENOMEM;
207                   goto error;
208                 }
209               rpath = new_rpath;
210               rpath_limit = rpath + new_size;
211
212               dest = rpath + dest_offset;
213             }
214
215 #ifdef _LIBC
216           dest = __mempcpy (dest, start, end - start);
217 #else
218           memcpy (dest, start, end - start);
219           dest += end - start;
220 #endif
221           *dest = '\0';
222
223 #ifdef _LIBC
224           if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
225 #else
226           if (lstat (rpath, &st) < 0)
227 #endif
228             goto error;
229
230           if (S_ISLNK (st.st_mode))
231             {
232               char *buf;
233               size_t len;
234               int n;
235
236               if (++num_links > MAXSYMLINKS)
237                 {
238                   __set_errno (ELOOP);
239                   goto error;
240                 }
241
242               buf = malloca (path_max);
243               if (!buf)
244                 {
245                   errno = ENOMEM;
246                   goto error;
247                 }
248
249               n = __readlink (rpath, buf, path_max - 1);
250               if (n < 0)
251                 {
252                   int saved_errno = errno;
253                   freea (buf);
254                   errno = saved_errno;
255                   goto error;
256                 }
257               buf[n] = '\0';
258
259               if (!extra_buf)
260                 {
261                   extra_buf = malloca (path_max);
262                   if (!extra_buf)
263                     {
264                       freea (buf);
265                       errno = ENOMEM;
266                       goto error;
267                     }
268                 }
269
270               len = strlen (end);
271               if ((long int) (n + len) >= path_max)
272                 {
273                   freea (buf);
274                   __set_errno (ENAMETOOLONG);
275                   goto error;
276                 }
277
278               /* Careful here, end may be a pointer into extra_buf... */
279               memmove (&extra_buf[n], end, len + 1);
280               name = end = memcpy (extra_buf, buf, n);
281
282               if (buf[0] == '/')
283                 dest = rpath + 1;       /* It's an absolute symlink */
284               else
285                 /* Back up to previous component, ignore if at root already: */
286                 if (dest > rpath + 1)
287                   while ((--dest)[-1] != '/');
288             }
289         }
290     }
291   if (dest > rpath + 1 && dest[-1] == '/')
292     --dest;
293   *dest = '\0';
294
295   if (extra_buf)
296     freea (extra_buf);
297
298   return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
299
300 error:
301   {
302     int saved_errno = errno;
303     if (extra_buf)
304       freea (extra_buf);
305     if (resolved)
306       strcpy (resolved, rpath);
307     else
308       free (rpath);
309     errno = saved_errno;
310   }
311   return NULL;
312 }
313 #ifdef _LIBC
314 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
315 #endif
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