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