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