* lib/canonicalize.c (ELOOP): Define if not already defined.
[gnulib.git] / lib / canonicalize.c
1 /* Return the canonical absolute name of a given file.
2    Copyright (C) 1996-2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; see the file COPYING.
16    If not, write to the Free Software Foundation,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 #include "canonicalize.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #if HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
29
30 #include <sys/stat.h>
31
32 #include <unistd.h>
33
34 #include <errno.h>
35 #include <stddef.h>
36
37 #include "cycle-check.h"
38 #include "filenamecat.h"
39 #include "stat-macros.h"
40 #include "xalloc.h"
41 #include "xgetcwd.h"
42
43 #ifndef ELOOP
44 # define ELOOP 0
45 #endif
46 #ifndef __set_errno
47 # define __set_errno(Val) errno = (Val)
48 #endif
49
50 #include "pathmax.h"
51 #include "xreadlink.h"
52
53 #if !HAVE_CANONICALIZE_FILE_NAME
54 /* Return the canonical absolute name of file NAME.  A canonical name
55    does not contain any `.', `..' components nor any repeated file name
56    separators ('/') or symlinks.  All components must exist.
57    The result is malloc'd.  */
58
59 char *
60 canonicalize_file_name (const char *name)
61 {
62 # if HAVE_RESOLVEPATH
63
64   char *resolved, *extra_buf = NULL;
65   size_t resolved_size;
66   ssize_t resolved_len;
67
68   if (name == NULL)
69     {
70       __set_errno (EINVAL);
71       return NULL;
72     }
73
74   if (name[0] == '\0')
75     {
76       __set_errno (ENOENT);
77       return NULL;
78     }
79
80   /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
81      relative names into absolute ones, so prepend the working
82      directory if the file name is not absolute.  */
83   if (name[0] != '/')
84     {
85       char *wd;
86
87       if (!(wd = xgetcwd ()))
88         return NULL;
89
90       extra_buf = file_name_concat (wd, name, NULL);
91       name = extra_buf;
92       free (wd);
93     }
94
95   resolved_size = strlen (name);
96   while (1)
97     {
98       resolved_size = 2 * resolved_size + 1;
99       resolved = xmalloc (resolved_size);
100       resolved_len = resolvepath (name, resolved, resolved_size);
101       if (resolved_len < 0)
102         {
103           free (resolved);
104           free (extra_buf);
105           return NULL;
106         }
107       if (resolved_len < resolved_size)
108         break;
109       free (resolved);
110     }
111
112   free (extra_buf);
113
114   /* NUL-terminate the resulting name.  */
115   resolved[resolved_len] = '\0';
116
117   return resolved;
118
119 # else
120
121   return canonicalize_filename_mode (name, CAN_EXISTING);
122
123 # endif /* !HAVE_RESOLVEPATH */
124 }
125 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
126
127 /* Return the canonical absolute name of file NAME.  A canonical name
128    does not contain any `.', `..' components nor any repeated file name
129    separators ('/') or symlinks.  Whether components must exist
130    or not depends on canonicalize mode.  The result is malloc'd.  */
131
132 char *
133 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
134 {
135   char *rname, *dest, *extra_buf = NULL;
136   char const *start;
137   char const *end;
138   char const *rname_limit;
139   size_t extra_len = 0;
140   struct cycle_check_state cycle_state;
141
142   if (name == NULL)
143     {
144       __set_errno (EINVAL);
145       return NULL;
146     }
147
148   if (name[0] == '\0')
149     {
150       __set_errno (ENOENT);
151       return NULL;
152     }
153
154   if (name[0] != '/')
155     {
156       rname = xgetcwd ();
157       if (!rname)
158         return NULL;
159       dest = strchr (rname, '\0');
160       if (dest - rname < PATH_MAX)
161         {
162           char *p = xrealloc (rname, PATH_MAX);
163           dest = p + (dest - rname);
164           rname = p;
165           rname_limit = rname + PATH_MAX;
166         }
167       else
168         {
169           rname_limit = dest;
170         }
171     }
172   else
173     {
174       rname = xmalloc (PATH_MAX);
175       rname_limit = rname + PATH_MAX;
176       rname[0] = '/';
177       dest = rname + 1;
178     }
179
180   cycle_check_init (&cycle_state);
181   for (start = end = name; *start; start = end)
182     {
183       /* Skip sequence of multiple file name separators.  */
184       while (*start == '/')
185         ++start;
186
187       /* Find end of component.  */
188       for (end = start; *end && *end != '/'; ++end)
189         /* Nothing.  */;
190
191       if (end - start == 0)
192         break;
193       else if (end - start == 1 && start[0] == '.')
194         /* nothing */;
195       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
196         {
197           /* Back up to previous component, ignore if at root already.  */
198           if (dest > rname + 1)
199             while ((--dest)[-1] != '/');
200         }
201       else
202         {
203           struct stat st;
204
205           if (dest[-1] != '/')
206             *dest++ = '/';
207
208           if (dest + (end - start) >= rname_limit)
209             {
210               ptrdiff_t dest_offset = dest - rname;
211               size_t new_size = rname_limit - rname;
212
213               if (end - start + 1 > PATH_MAX)
214                 new_size += end - start + 1;
215               else
216                 new_size += PATH_MAX;
217               rname = xrealloc (rname, new_size);
218               rname_limit = rname + new_size;
219
220               dest = rname + dest_offset;
221             }
222
223           dest = memcpy (dest, start, end - start);
224           dest += end - start;
225           *dest = '\0';
226
227           if (lstat (rname, &st) != 0)
228             {
229               if (can_mode == CAN_EXISTING)
230                 goto error;
231               if (can_mode == CAN_ALL_BUT_LAST && *end)
232                 goto error;
233               st.st_mode = 0;
234             }
235
236           if (S_ISLNK (st.st_mode))
237             {
238               char *buf;
239               size_t n, len;
240
241               if (cycle_check (&cycle_state, &st))
242                 {
243                   __set_errno (ELOOP);
244                   if (can_mode == CAN_MISSING)
245                     continue;
246                   else
247                     goto error;
248                 }
249
250               buf = xreadlink (rname, st.st_size);
251               if (!buf)
252                 {
253                   if (can_mode == CAN_MISSING)
254                     continue;
255                   else
256                     goto error;
257                 }
258
259               n = strlen (buf);
260               len = strlen (end);
261
262               if (!extra_len)
263                 {
264                   extra_len =
265                     ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
266                   extra_buf = xmalloc (extra_len);
267                 }
268               else if ((n + len + 1) > extra_len)
269                 {
270                   extra_len = n + len + 1;
271                   extra_buf = xrealloc (extra_buf, extra_len);
272                 }
273
274               /* Careful here, end may be a pointer into extra_buf... */
275               memmove (&extra_buf[n], end, len + 1);
276               name = end = memcpy (extra_buf, buf, n);
277
278               if (buf[0] == '/')
279                 dest = rname + 1;       /* It's an absolute symlink */
280               else
281                 /* Back up to previous component, ignore if at root already: */
282                 if (dest > rname + 1)
283                   while ((--dest)[-1] != '/');
284
285               free (buf);
286             }
287           else
288             {
289               if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
290                 {
291                   errno = ENOTDIR;
292                   goto error;
293                 }
294             }
295         }
296     }
297   if (dest > rname + 1 && dest[-1] == '/')
298     --dest;
299   *dest = '\0';
300
301   free (extra_buf);
302   return rname;
303
304 error:
305   free (extra_buf);
306   free (rname);
307   return NULL;
308 }