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