update.
[chise/xemacs-chise.git.1] / src / realpath.c
1 /*
2  * realpath.c -- canonicalize pathname by removing symlinks
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: Not in FSF. */
24
25 #include <config.h>
26 #include "lisp.h"
27 #include <errno.h>
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #if defined (HAVE_SYS_PARAM_H) && !defined (WIN32_NATIVE)
34 #include <sys/param.h>
35 #endif
36
37 #ifdef WIN32_NATIVE
38 #include <direct.h>
39 #endif
40
41 #include <sys/stat.h>                   /* for S_IFLNK */
42
43 #if defined(WIN32_NATIVE) || defined(CYGWIN)
44 #define WIN32_FILENAMES
45 #endif
46
47 /* First char after start of absolute filename. */
48 #define ABS_START(name) (name + ABS_LENGTH (name))
49
50 #if defined (WIN32_NATIVE)
51 /* Length of start of absolute filename. */
52 # define ABS_LENGTH(name) (win32_abs_start (name))
53 static int win32_abs_start (const char * name);
54 /* System dependent version of readlink. */
55 # define system_readlink win32_readlink
56 #else
57 # ifdef CYGWIN
58 #  ifdef WIN32_FILENAMES
59 #   define ABS_LENGTH(name) (win32_abs_start (name))
60 static int win32_abs_start (const char * name);
61 #  else
62 #   define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
63                              (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
64 #  endif
65 #  define system_readlink cygwin_readlink
66 # else
67 #  define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
68 #  define system_readlink readlink
69 # endif /* CYGWIN */
70 #endif /* WIN32_NATIVE */
71
72 #if defined (WIN32_NATIVE) || defined (CYGWIN)
73 #include "syswindows.h"
74 /* Emulate readlink on win32 - finds real name (i.e. correct case) of
75    a file. UNC servers and shares are lower-cased. Directories must be
76    given without trailing '/'. One day, this could read Win2K's
77    reparse points. */
78 static int
79 win32_readlink (const char * name, char * buf, int size)
80 {
81   WIN32_FIND_DATA find_data;
82   HANDLE dir_handle = NULL;
83   int len = 0;
84   int err = 0;
85   const char* lastname;
86   int count = 0;
87   const char* tmp;
88   char* res = NULL;
89   
90   assert (*name);
91   
92   /* Sort of check we have a valid filename. */
93   /* #### can we have escaped shell operators in a Windows filename? */
94   if (strpbrk (name, "|<>\"") || strlen (name) >= MAX_PATH)
95     {
96       errno = EIO;
97       return -1;
98     }
99   /* #### can we have escaped wildcards in a Windows filename? */
100   else if (strpbrk (name, "*?"))
101     {
102       errno = EINVAL;           /* this valid path can't be a symlink */
103       return -1;
104     }
105   
106   /* Find start of filename */
107   lastname = name + strlen (name);
108   while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
109     --lastname;
110
111   /* Count slashes in unc path */
112   if (ABS_LENGTH (name) == 2)
113     for (tmp = name; *tmp; tmp++)
114       if (IS_DIRECTORY_SEP (*tmp))
115         count++;
116
117   if (count >= 2 && count < 4)
118     {
119       /* UNC server or share name: just copy lowercased name. */
120       res = find_data.cFileName;
121       for (tmp = lastname; *tmp; tmp++)
122         *res++ = tolower (*tmp);
123       *res = '\0';
124     }
125   else
126     dir_handle = FindFirstFile (name, &find_data);
127
128   if (res || dir_handle != INVALID_HANDLE_VALUE)
129     {
130       if ((len = strlen (find_data.cFileName)) < size)
131         {
132           if (strcmp (lastname, find_data.cFileName) == 0)
133             /* Signal that the name is already OK. */
134             err = EINVAL;
135           else
136             memcpy (buf, find_data.cFileName, len + 1);
137         }
138       else
139         err = ENAMETOOLONG;
140       if (!res) FindClose (dir_handle);
141     }
142   else
143     err = ENOENT;
144
145   errno = err;
146   return err ? -1 : len;
147 }
148 #endif /* WIN32_NATIVE || CYGWIN */
149
150 #ifdef CYGWIN
151 /* Call readlink and try to find out the correct case for the file. */
152 static int
153 cygwin_readlink (const char * name, char * buf, int size)
154 {
155   int n = readlink (name, buf, size);
156   if (n < 0 && errno == EINVAL)
157     {
158       /* The file may exist, but isn't a symlink. Try to find the
159          right name. */
160       char* tmp = alloca (cygwin_posix_to_win32_path_list_buf_size (name));
161       cygwin_posix_to_win32_path_list (name, tmp);
162       n = win32_readlink (tmp, buf, size);
163     }
164   return n;
165 }
166 #endif /* CYGWIN */
167
168 #ifdef WIN32_FILENAMES
169 #ifndef ELOOP
170 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
171 #endif
172 /* Length of start of absolute filename. */
173 static int 
174 win32_abs_start (const char * name)
175 {
176   if (isalpha (*name) && IS_DEVICE_SEP (name[1])
177       && IS_DIRECTORY_SEP (name[2]))
178     return 3;
179   else if (IS_DIRECTORY_SEP (*name))
180     return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
181   else 
182     return 0;
183 }
184 #endif /* WIN32_NATIVE */
185
186 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
187 #undef getcwd
188 #define getcwd(buffer, len) getwd (buffer)
189 #endif
190
191 #ifndef PATH_MAX
192 # if defined (_POSIX_PATH_MAX)
193 #  define PATH_MAX _POSIX_PATH_MAX
194 # elif defined (MAXPATHLEN)
195 #  define PATH_MAX MAXPATHLEN
196 # else
197 #  define PATH_MAX 1024
198 # endif
199 #endif
200
201 #define MAX_READLINKS 32
202
203 char * xrealpath (const char *path, char resolved_path []);
204 char *
205 xrealpath (const char *path, char resolved_path [])
206 {
207   char copy_path[PATH_MAX];
208   char *new_path = resolved_path;
209   char *max_path;
210 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
211   int readlinks = 0;
212   char link_path[PATH_MAX];
213   int n;
214   int abslen = ABS_LENGTH (path);
215 #endif
216
217   /* Make a copy of the source path since we may need to modify it. */
218   strcpy (copy_path, path);
219   path = copy_path;
220   max_path = copy_path + PATH_MAX - 2;
221
222   if (0)
223     ;
224 #ifdef WIN32_FILENAMES
225   /* Check for c:/... or //server/... */
226   else if (abslen == 3 || abslen == 2)
227     {
228       /* Make sure drive letter is lowercased. */
229       if (abslen == 3) {
230         *new_path = tolower (*path);
231         new_path++;
232         path++;
233         abslen--;
234       }
235       /* Coerce directory chars. */
236       while (abslen-- > 0) {
237         if (IS_DIRECTORY_SEP (*path))
238           *new_path++ = DIRECTORY_SEP;
239         else
240           *new_path++ = *path;
241         path++;
242       }
243     }
244 #endif
245 #ifdef WIN32_NATIVE
246   /* No drive letter, but a beginning slash? Prepend drive letter. */
247   else if (abslen == 1)
248     {
249       getcwd (new_path, PATH_MAX - 1);
250       new_path += 3;
251       path++;
252     }
253   /* Just a path name, prepend the current directory */
254   else if (1)
255     {
256       getcwd (new_path, PATH_MAX - 1);
257       new_path += strlen (new_path);
258       if (!IS_DIRECTORY_SEP (new_path[-1]))
259         *new_path++ = DIRECTORY_SEP;
260     }
261 #else
262   /* If it's a relative pathname use getcwd for starters. */
263   else if (abslen == 0)
264     {
265       getcwd (new_path, PATH_MAX - 1);
266       new_path += strlen (new_path);
267       if (!IS_DIRECTORY_SEP (new_path[-1]))
268         *new_path++ = DIRECTORY_SEP;
269     }
270   else
271     {
272       /* Copy first directory sep. May have two on cygwin. */
273       strncpy (new_path, path, abslen);
274       new_path += abslen;
275       path += abslen;
276     }
277 #endif
278   /* Expand each slash-separated pathname component. */
279   while (*path != '\0')
280     {
281       /* Ignore stray "/". */
282       if (IS_DIRECTORY_SEP (*path))
283         {
284           path++;
285           continue;
286         }
287
288       if (*path == '.')
289         {
290           /* Ignore ".". */
291           if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
292             {
293               path++;
294               continue;
295             }
296
297           /* Handle ".." */
298           if (path[1] == '.' &&
299               (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
300             {
301               path += 2;
302
303               /* Ignore ".." at root. */
304               if (new_path == ABS_START (resolved_path))
305                 continue;
306
307               /* Handle ".." by backing up. */
308               --new_path;
309               while (!IS_DIRECTORY_SEP (new_path[-1]))
310                 --new_path;
311               continue;
312             }
313         }
314
315       /* Safely copy the next pathname component. */
316       while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
317         {
318           if (path > max_path)
319             {
320               errno = ENAMETOOLONG;
321               return NULL;
322             }
323           *new_path++ = *path++;
324         }
325
326 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
327       /* See if latest pathname component is a symlink. */
328       *new_path = '\0';
329       n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
330
331       if (n < 0)
332         {
333           /* EINVAL means the file exists but isn't a symlink. */
334 #ifdef CYGWIN
335           if (errno != EINVAL && errno != ENOENT)
336 #else
337           if (errno != EINVAL) 
338 #endif
339             return NULL;
340         }
341       else
342         {
343           /* Protect against infinite loops. */
344           if (readlinks++ > MAX_READLINKS)
345             {
346               errno = ELOOP;
347               return NULL;
348             }
349
350           /* Note: readlink doesn't add the null byte. */
351           link_path[n] = '\0';
352           
353           if (ABS_LENGTH (link_path) > 0)
354             /* Start over for an absolute symlink. */
355             new_path = resolved_path + ABS_LENGTH (link_path) - 1;
356           else
357             /* Otherwise back up over this component. */
358             for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
359               assert (new_path > resolved_path);
360
361           /* Safe sex check. */
362           if (strlen(path) + n >= PATH_MAX)
363             {
364               errno = ENAMETOOLONG;
365               return NULL;
366             }
367
368           /* Insert symlink contents into path. */
369           strcat(link_path, path);
370           strcpy(copy_path, link_path);
371           path = copy_path;
372         }
373 #endif /* S_IFLNK || WIN32_NATIVE */
374       *new_path++ = DIRECTORY_SEP;
375     }
376
377   /* Delete trailing slash but don't whomp a lone slash. */
378   if (new_path != ABS_START (resolved_path) && IS_DIRECTORY_SEP (new_path[-1]))
379     new_path--;
380
381   /* Make sure it's null terminated. */
382   *new_path = '\0';
383
384   return resolved_path;
385 }