]> git.cworth.org Git - tar/blob - gnu/canonicalize-lgpl.c
Imported Upstream version 1.24
[tar] / gnu / canonicalize-lgpl.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Return the canonical absolute name of a given file.
4    Copyright (C) 1996-2010 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef _LIBC
21 # include <config.h>
22 #endif
23
24 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
25
26 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
27    optimizes away the name == NULL test below.  */
28 #define _GL_ARG_NONNULL(params)
29
30 /* Specification.  */
31 #include <stdlib.h>
32
33 #include <alloca.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #if HAVE_SYS_PARAM_H || defined _LIBC
38 # include <sys/param.h>
39 #endif
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <stddef.h>
43
44 #ifdef _LIBC
45 # include <shlib-compat.h>
46 #else
47 # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
48 # define versioned_symbol(lib, local, symbol, version) extern int dummy
49 # define compat_symbol(lib, local, symbol, version)
50 # define weak_alias(local, symbol)
51 # define __canonicalize_file_name canonicalize_file_name
52 # define __realpath realpath
53 # include "pathmax.h"
54 # include "malloca.h"
55 # if HAVE_GETCWD
56 #  if IN_RELOCWRAPPER
57     /* When building the relocatable program wrapper, use the system's getcwd
58        function, not the gnulib override, otherwise we would get a link error.
59      */
60 #   undef getcwd
61 #  endif
62 #  ifdef VMS
63     /* We want the directory in Unix syntax, not in VMS syntax.  */
64 #   define __getcwd(buf, max) getcwd (buf, max, 0)
65 #  else
66 #   define __getcwd getcwd
67 #  endif
68 # else
69 #  define __getcwd(buf, max) getwd (buf)
70 # endif
71 # define __readlink readlink
72 # define __set_errno(e) errno = (e)
73 /* Use the system functions, not the gnulib overrides in this file.  */
74 # undef malloc
75 # ifndef MAXSYMLINKS
76 #  ifdef SYMLOOP_MAX
77 #   define MAXSYMLINKS SYMLOOP_MAX
78 #  else
79 #   define MAXSYMLINKS 20
80 #  endif
81 # endif
82 #endif
83
84 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
85 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
86 #endif
87
88 #if !FUNC_REALPATH_WORKS || defined _LIBC
89 /* Return the canonical absolute name of file NAME.  A canonical name
90    does not contain any `.', `..' components nor any repeated path
91    separators ('/') or symlinks.  All path components must exist.  If
92    RESOLVED is null, the result is malloc'd; otherwise, if the
93    canonical name is PATH_MAX chars or more, returns null with `errno'
94    set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
95    returns the name in RESOLVED.  If the name cannot be resolved and
96    RESOLVED is non-NULL, it contains the path of the first component
97    that cannot be resolved.  If the path can be resolved, RESOLVED
98    holds the same value as the value returned.  */
99
100 char *
101 __realpath (const char *name, char *resolved)
102 {
103   char *rpath, *dest, *extra_buf = NULL;
104   const char *start, *end, *rpath_limit;
105   long int path_max;
106   int num_links = 0;
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       if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
163         *dest++ = '/';
164     }
165
166   for (start = end = name; *start; start = end)
167     {
168 #ifdef _LIBC
169       struct stat64 st;
170 #else
171       struct stat st;
172 #endif
173       int n;
174
175       /* Skip sequence of multiple path-separators.  */
176       while (*start == '/')
177         ++start;
178
179       /* Find end of path component.  */
180       for (end = start; *end && *end != '/'; ++end)
181         /* Nothing.  */;
182
183       if (end - start == 0)
184         break;
185       else if (end - start == 1 && start[0] == '.')
186         /* nothing */;
187       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
188         {
189           /* Back up to previous component, ignore if at root already.  */
190           if (dest > rpath + 1)
191             while ((--dest)[-1] != '/');
192           if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
193               && *dest == '/')
194             dest++;
195         }
196       else
197         {
198           size_t new_size;
199
200           if (dest[-1] != '/')
201             *dest++ = '/';
202
203           if (dest + (end - start) >= rpath_limit)
204             {
205               ptrdiff_t dest_offset = dest - rpath;
206               char *new_rpath;
207
208               if (resolved)
209                 {
210                   __set_errno (ENAMETOOLONG);
211                   if (dest > rpath + 1)
212                     dest--;
213                   *dest = '\0';
214                   goto error;
215                 }
216               new_size = rpath_limit - rpath;
217               if (end - start + 1 > path_max)
218                 new_size += end - start + 1;
219               else
220                 new_size += path_max;
221               new_rpath = (char *) realloc (rpath, new_size);
222               if (new_rpath == NULL)
223                 {
224                   /* It's easier to set errno to ENOMEM than to rely on the
225                      'realloc-posix' gnulib module.  */
226                   errno = ENOMEM;
227                   goto error;
228                 }
229               rpath = new_rpath;
230               rpath_limit = rpath + new_size;
231
232               dest = rpath + dest_offset;
233             }
234
235 #ifdef _LIBC
236           dest = __mempcpy (dest, start, end - start);
237 #else
238           memcpy (dest, start, end - start);
239           dest += end - start;
240 #endif
241           *dest = '\0';
242
243 #ifdef _LIBC
244           if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
245 #else
246           if (lstat (rpath, &st) < 0)
247 #endif
248             goto error;
249
250           if (S_ISLNK (st.st_mode))
251             {
252               char *buf;
253               size_t len;
254
255               if (++num_links > MAXSYMLINKS)
256                 {
257                   __set_errno (ELOOP);
258                   goto error;
259                 }
260
261               buf = malloca (path_max);
262               if (!buf)
263                 {
264                   errno = ENOMEM;
265                   goto error;
266                 }
267
268               n = __readlink (rpath, buf, path_max - 1);
269               if (n < 0)
270                 {
271                   int saved_errno = errno;
272                   freea (buf);
273                   errno = saved_errno;
274                   goto error;
275                 }
276               buf[n] = '\0';
277
278               if (!extra_buf)
279                 {
280                   extra_buf = malloca (path_max);
281                   if (!extra_buf)
282                     {
283                       freea (buf);
284                       errno = ENOMEM;
285                       goto error;
286                     }
287                 }
288
289               len = strlen (end);
290               if ((long int) (n + len) >= path_max)
291                 {
292                   freea (buf);
293                   __set_errno (ENAMETOOLONG);
294                   goto error;
295                 }
296
297               /* Careful here, end may be a pointer into extra_buf... */
298               memmove (&extra_buf[n], end, len + 1);
299               name = end = memcpy (extra_buf, buf, n);
300
301               if (buf[0] == '/')
302                 {
303                   dest = rpath + 1;     /* It's an absolute symlink */
304                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
305                     *dest++ = '/';
306                 }
307               else
308                 {
309                   /* Back up to previous component, ignore if at root
310                      already: */
311                   if (dest > rpath + 1)
312                     while ((--dest)[-1] != '/');
313                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1
314                       && *dest == '/')
315                     dest++;
316                 }
317             }
318           else if (!S_ISDIR (st.st_mode) && *end != '\0')
319             {
320               __set_errno (ENOTDIR);
321               goto error;
322             }
323         }
324     }
325   if (dest > rpath + 1 && dest[-1] == '/')
326     --dest;
327   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && *dest == '/')
328     dest++;
329   *dest = '\0';
330
331   if (extra_buf)
332     freea (extra_buf);
333
334   return rpath;
335
336 error:
337   {
338     int saved_errno = errno;
339     if (extra_buf)
340       freea (extra_buf);
341     if (resolved == NULL)
342       free (rpath);
343     errno = saved_errno;
344   }
345   return NULL;
346 }
347 versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
348 #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */
349
350
351 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
352 char *
353 attribute_compat_text_section
354 __old_realpath (const char *name, char *resolved)
355 {
356   if (resolved == NULL)
357     {
358       __set_errno (EINVAL);
359       return NULL;
360     }
361
362   return __realpath (name, resolved);
363 }
364 compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
365 #endif
366
367
368 char *
369 __canonicalize_file_name (const char *name)
370 {
371   return __realpath (name, NULL);
372 }
373 weak_alias (__canonicalize_file_name, canonicalize_file_name)
374
375 #else
376
377 /* This declaration is solely to ensure that after preprocessing
378    this file is never empty.  */
379 typedef int dummy;
380
381 #endif