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