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