9bfb44f9829b30d00c161afd5db0c8d3dc691327
[platform/upstream/m4.git] / lib / canonicalize-lgpl.c
1 /* Return the canonical absolute name of a given file.
2    Copyright (C) 1996-2011 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 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
25    optimizes away the name == NULL test below.  */
26 #define _GL_ARG_NONNULL(params)
27
28 /* Specification.  */
29 #include <stdlib.h>
30
31 #include <alloca.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #if HAVE_SYS_PARAM_H || defined _LIBC
36 # include <sys/param.h>
37 #endif
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <stddef.h>
41
42 #ifdef _LIBC
43 # include <shlib-compat.h>
44 #else
45 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
46 # define versioned_symbol(lib, local, symbol, version) extern int dummy
47 # define compat_symbol(lib, local, symbol, version)
48 # define weak_alias(local, symbol)
49 # define __canonicalize_file_name canonicalize_file_name
50 # define __realpath realpath
51 # include "pathmax.h"
52 # include "malloca.h"
53 # if HAVE_GETCWD
54 #  if IN_RELOCWRAPPER
55     /* When building the relocatable program wrapper, use the system's getcwd
56        function, not the gnulib override, otherwise we would get a link error.
57      */
58 #   undef getcwd
59 #  endif
60 #  ifdef VMS
61     /* We want the directory in Unix syntax, not in VMS syntax.  */
62 #   define __getcwd(buf, max) getcwd (buf, max, 0)
63 #  else
64 #   define __getcwd getcwd
65 #  endif
66 # else
67 #  define __getcwd(buf, max) getwd (buf)
68 # endif
69 # define __readlink readlink
70 # define __set_errno(e) errno = (e)
71 /* Use the system functions, not the gnulib overrides in this file.  */
72 # undef malloc
73 # ifndef MAXSYMLINKS
74 #  ifdef SYMLOOP_MAX
75 #   define MAXSYMLINKS SYMLOOP_MAX
76 #  else
77 #   define MAXSYMLINKS 20
78 #  endif
79 # endif
80 #endif
81
82 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
83 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
84 #endif
85
86 #if !FUNC_REALPATH_WORKS || defined _LIBC
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   int num_links = 0;
105
106   if (name == NULL)
107     {
108       /* As per Single Unix Specification V2 we must return an error if
109          either parameter is a null pointer.  We extend this to allow
110          the RESOLVED parameter to be NULL in case the we are expected to
111          allocate the room for the return value.  */
112       __set_errno (EINVAL);
113       return NULL;
114     }
115
116   if (name[0] == '\0')
117     {
118       /* As per Single Unix Specification V2 we must return an error if
119          the name argument points to an empty string.  */
120       __set_errno (ENOENT);
121       return NULL;
122     }
123
124 #ifdef PATH_MAX
125   path_max = PATH_MAX;
126 #else
127   path_max = pathconf (name, _PC_PATH_MAX);
128   if (path_max <= 0)
129     path_max = 1024;
130 #endif
131
132   if (resolved == NULL)
133     {
134       rpath = malloc (path_max);
135       if (rpath == NULL)
136         {
137           /* It's easier to set errno to ENOMEM than to rely on the
138              'malloc-posix' gnulib module.  */
139           errno = ENOMEM;
140           return NULL;
141         }
142     }
143   else
144     rpath = resolved;
145   rpath_limit = rpath + path_max;
146
147   if (name[0] != '/')
148     {
149       if (!__getcwd (rpath, path_max))
150         {
151           rpath[0] = '\0';
152           goto error;
153         }
154       dest = strchr (rpath, '\0');
155     }
156   else
157     {
158       rpath[0] = '/';
159       dest = rpath + 1;
160       if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
161         *dest++ = '/';
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       int n;
172
173       /* Skip sequence of multiple path-separators.  */
174       while (*start == '/')
175         ++start;
176
177       /* Find end of path component.  */
178       for (end = start; *end && *end != '/'; ++end)
179         /* Nothing.  */;
180
181       if (end - start == 0)
182         break;
183       else if (end - start == 1 && start[0] == '.')
184         /* nothing */;
185       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
186         {
187           /* Back up to previous component, ignore if at root already.  */
188           if (dest > rpath + 1)
189             while ((--dest)[-1] != '/');
190           if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
191               && *dest == '/')
192             dest++;
193         }
194       else
195         {
196           size_t new_size;
197
198           if (dest[-1] != '/')
199             *dest++ = '/';
200
201           if (dest + (end - start) >= rpath_limit)
202             {
203               ptrdiff_t dest_offset = dest - rpath;
204               char *new_rpath;
205
206               if (resolved)
207                 {
208                   __set_errno (ENAMETOOLONG);
209                   if (dest > rpath + 1)
210                     dest--;
211                   *dest = '\0';
212                   goto error;
213                 }
214               new_size = rpath_limit - rpath;
215               if (end - start + 1 > path_max)
216                 new_size += end - start + 1;
217               else
218                 new_size += path_max;
219               new_rpath = (char *) realloc (rpath, new_size);
220               if (new_rpath == NULL)
221                 {
222                   /* It's easier to set errno to ENOMEM than to rely on the
223                      'realloc-posix' gnulib module.  */
224                   errno = ENOMEM;
225                   goto error;
226                 }
227               rpath = new_rpath;
228               rpath_limit = rpath + new_size;
229
230               dest = rpath + dest_offset;
231             }
232
233 #ifdef _LIBC
234           dest = __mempcpy (dest, start, end - start);
235 #else
236           memcpy (dest, start, end - start);
237           dest += end - start;
238 #endif
239           *dest = '\0';
240
241 #ifdef _LIBC
242           if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
243 #else
244           if (lstat (rpath, &st) < 0)
245 #endif
246             goto error;
247
248           if (S_ISLNK (st.st_mode))
249             {
250               char *buf;
251               size_t len;
252
253               if (++num_links > MAXSYMLINKS)
254                 {
255                   __set_errno (ELOOP);
256                   goto error;
257                 }
258
259               buf = malloca (path_max);
260               if (!buf)
261                 {
262                   errno = ENOMEM;
263                   goto error;
264                 }
265
266               n = __readlink (rpath, buf, path_max - 1);
267               if (n < 0)
268                 {
269                   int saved_errno = errno;
270                   freea (buf);
271                   errno = saved_errno;
272                   goto error;
273                 }
274               buf[n] = '\0';
275
276               if (!extra_buf)
277                 {
278                   extra_buf = malloca (path_max);
279                   if (!extra_buf)
280                     {
281                       freea (buf);
282                       errno = ENOMEM;
283                       goto error;
284                     }
285                 }
286
287               len = strlen (end);
288               if ((long int) (n + len) >= path_max)
289                 {
290                   freea (buf);
291                   __set_errno (ENAMETOOLONG);
292                   goto error;
293                 }
294
295               /* Careful here, end may be a pointer into extra_buf... */
296               memmove (&extra_buf[n], end, len + 1);
297               name = end = memcpy (extra_buf, buf, n);
298
299               if (buf[0] == '/')
300                 {
301                   dest = rpath + 1;     /* It's an absolute symlink */
302                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
303                     *dest++ = '/';
304                 }
305               else
306                 {
307                   /* Back up to previous component, ignore if at root
308                      already: */
309                   if (dest > rpath + 1)
310                     while ((--dest)[-1] != '/');
311                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
312                       && *dest == '/')
313                     dest++;
314                 }
315             }
316           else if (!S_ISDIR (st.st_mode) && *end != '\0')
317             {
318               __set_errno (ENOTDIR);
319               goto error;
320             }
321         }
322     }
323   if (dest > rpath + 1 && dest[-1] == '/')
324     --dest;
325   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/')
326     dest++;
327   *dest = '\0';
328
329   if (extra_buf)
330     freea (extra_buf);
331
332   return rpath;
333
334 error:
335   {
336     int saved_errno = errno;
337     if (extra_buf)
338       freea (extra_buf);
339     if (resolved == NULL)
340       free (rpath);
341     errno = saved_errno;
342   }
343   return NULL;
344 }
345 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
346 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
347
348
349 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
350 char *
351 attribute_compat_text_section
352 __old_realpath (const char *name, char *resolved)
353 {
354   if (resolved == NULL)
355     {
356       __set_errno (EINVAL);
357       return NULL;
358     }
359
360   return __realpath (name, resolved);
361 }
362 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
363 #endif
364
365
366 char *
367 __canonicalize_file_name (const char *name)
368 {
369   return __realpath (name, NULL);
370 }
371 weak_alias (__canonicalize_file_name, canonicalize_file_name)
372
373 #else
374
375 /* This declaration is solely to ensure that after preprocessing
376    this file is never empty.  */
377 typedef int dummy;
378
379 #endif