This commit was generated by cvs2svn to compensate for changes in r5209,
[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   if (strpbrk (name, "*?|<>\"") || strlen (name) >= MAX_PATH)
94     {
95       errno = EIO;
96       return -1;
97     }
98   
99   /* Find start of filename */
100   lastname = name + strlen (name);
101   while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
102     --lastname;
103
104   /* Count slashes in unc path */
105   if (ABS_LENGTH (name) == 2)
106     for (tmp = name; *tmp; tmp++)
107       if (IS_DIRECTORY_SEP (*tmp))
108         count++;
109
110   if (count >= 2 && count < 4)
111     {
112       /* UNC server or share name: just copy lowercased name. */
113       res = find_data.cFileName;
114       for (tmp = lastname; *tmp; tmp++)
115         *res++ = tolower (*tmp);
116       *res = '\0';
117     }
118   else
119     dir_handle = FindFirstFile (name, &find_data);
120
121   if (res || dir_handle != INVALID_HANDLE_VALUE)
122     {
123       if ((len = strlen (find_data.cFileName)) < size)
124         {
125           if (strcmp (lastname, find_data.cFileName) == 0)
126             /* Signal that the name is already OK. */
127             err = EINVAL;
128           else
129             memcpy (buf, find_data.cFileName, len + 1);
130         }
131       else
132         err = ENAMETOOLONG;
133       if (!res) FindClose (dir_handle);
134     }
135   else
136     err = ENOENT;
137
138   errno = err;
139   return err ? -1 : len;
140 }
141 #endif /* WIN32_NATIVE || CYGWIN */
142
143 #ifdef CYGWIN
144 /* Call readlink and try to find out the correct case for the file. */
145 static int
146 cygwin_readlink (const char * name, char * buf, int size)
147 {
148   int n = readlink (name, buf, size);
149   if (n < 0 && errno == EINVAL)
150     {
151       /* The file may exist, but isn't a symlink. Try to find the
152          right name. */
153       char* tmp = alloca (cygwin_posix_to_win32_path_list_buf_size (name));
154       cygwin_posix_to_win32_path_list (name, tmp);
155       n = win32_readlink (tmp, buf, size);
156     }
157   return n;
158 }
159 #endif /* CYGWIN */
160
161 #ifdef WIN32_FILENAMES
162 #ifndef ELOOP
163 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
164 #endif
165 /* Length of start of absolute filename. */
166 static int 
167 win32_abs_start (const char * name)
168 {
169   if (isalpha (*name) && IS_DEVICE_SEP (name[1])
170       && IS_DIRECTORY_SEP (name[2]))
171     return 3;
172   else if (IS_DIRECTORY_SEP (*name))
173     return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
174   else 
175     return 0;
176 }
177 #endif /* WIN32_NATIVE */
178
179 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
180 #undef getcwd
181 #define getcwd(buffer, len) getwd (buffer)
182 #endif
183
184 #ifndef PATH_MAX
185 # if defined (_POSIX_PATH_MAX)
186 #  define PATH_MAX _POSIX_PATH_MAX
187 # elif defined (MAXPATHLEN)
188 #  define PATH_MAX MAXPATHLEN
189 # else
190 #  define PATH_MAX 1024
191 # endif
192 #endif
193
194 #define MAX_READLINKS 32
195
196 char * xrealpath (const char *path, char resolved_path []);
197 char *
198 xrealpath (const char *path, char resolved_path [])
199 {
200   char copy_path[PATH_MAX];
201   char *new_path = resolved_path;
202   char *max_path;
203 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
204   int readlinks = 0;
205   char link_path[PATH_MAX];
206   int n;
207   int abslen = ABS_LENGTH (path);
208 #endif
209
210   /* Make a copy of the source path since we may need to modify it. */
211   strcpy (copy_path, path);
212   path = copy_path;
213   max_path = copy_path + PATH_MAX - 2;
214
215   if (0)
216     ;
217 #ifdef WIN32_FILENAMES
218   /* Check for c:/... or //server/... */
219   else if (abslen == 3 || abslen == 2)
220     {
221       /* Make sure drive letter is lowercased. */
222       if (abslen == 3) {
223         *new_path = tolower (*path);
224         new_path++;
225         path++;
226         abslen--;
227       }
228       /* Coerce directory chars. */
229       while (abslen-- > 0) {
230         if (IS_DIRECTORY_SEP (*path))
231           *new_path++ = DIRECTORY_SEP;
232         else
233           *new_path++ = *path;
234         path++;
235       }
236     }
237 #endif
238 #ifdef WIN32_NATIVE
239   /* No drive letter, but a beginning slash? Prepend drive letter. */
240   else if (abslen == 1)
241     {
242       getcwd (new_path, PATH_MAX - 1);
243       new_path += 3;
244       path++;
245     }
246   /* Just a path name, prepend the current directory */
247   else if (1)
248     {
249       getcwd (new_path, PATH_MAX - 1);
250       new_path += strlen (new_path);
251       if (!IS_DIRECTORY_SEP (new_path[-1]))
252         *new_path++ = DIRECTORY_SEP;
253     }
254 #else
255   /* If it's a relative pathname use getcwd for starters. */
256   else if (abslen == 0)
257     {
258       getcwd (new_path, PATH_MAX - 1);
259       new_path += strlen (new_path);
260       if (!IS_DIRECTORY_SEP (new_path[-1]))
261         *new_path++ = DIRECTORY_SEP;
262     }
263   else
264     {
265       /* Copy first directory sep. May have two on cygwin. */
266       strncpy (new_path, path, abslen);
267       new_path += abslen;
268       path += abslen;
269     }
270 #endif
271   /* Expand each slash-separated pathname component. */
272   while (*path != '\0')
273     {
274       /* Ignore stray "/". */
275       if (IS_DIRECTORY_SEP (*path))
276         {
277           path++;
278           continue;
279         }
280
281       if (*path == '.')
282         {
283           /* Ignore ".". */
284           if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
285             {
286               path++;
287               continue;
288             }
289
290           /* Handle ".." */
291           if (path[1] == '.' &&
292               (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
293             {
294               path += 2;
295
296               /* Ignore ".." at root. */
297               if (new_path == ABS_START (resolved_path))
298                 continue;
299
300               /* Handle ".." by backing up. */
301               --new_path;
302               while (!IS_DIRECTORY_SEP (new_path[-1]))
303                 --new_path;
304               continue;
305             }
306         }
307
308       /* Safely copy the next pathname component. */
309       while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
310         {
311           if (path > max_path)
312             {
313               errno = ENAMETOOLONG;
314               return NULL;
315             }
316           *new_path++ = *path++;
317         }
318
319 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
320       /* See if latest pathname component is a symlink. */
321       *new_path = '\0';
322       n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
323
324       if (n < 0)
325         {
326           /* EINVAL means the file exists but isn't a symlink. */
327 #ifdef CYGWIN
328           if (errno != EINVAL && errno != ENOENT)
329 #else
330           if (errno != EINVAL) 
331 #endif
332             return NULL;
333         }
334       else
335         {
336           /* Protect against infinite loops. */
337           if (readlinks++ > MAX_READLINKS)
338             {
339               errno = ELOOP;
340               return NULL;
341             }
342
343           /* Note: readlink doesn't add the null byte. */
344           link_path[n] = '\0';
345           
346           if (ABS_LENGTH (link_path) > 0)
347             /* Start over for an absolute symlink. */
348             new_path = resolved_path + ABS_LENGTH (link_path) - 1;
349           else
350             /* Otherwise back up over this component. */
351             for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
352               assert (new_path > resolved_path);
353
354           /* Safe sex check. */
355           if (strlen(path) + n >= PATH_MAX)
356             {
357               errno = ENAMETOOLONG;
358               return NULL;
359             }
360
361           /* Insert symlink contents into path. */
362           strcat(link_path, path);
363           strcpy(copy_path, link_path);
364           path = copy_path;
365         }
366 #endif /* S_IFLNK || WIN32_NATIVE */
367       *new_path++ = DIRECTORY_SEP;
368     }
369
370   /* Delete trailing slash but don't whomp a lone slash. */
371   if (new_path != ABS_START (resolved_path) && IS_DIRECTORY_SEP (new_path[-1]))
372     new_path--;
373
374   /* Make sure it's null terminated. */
375   *new_path = '\0';
376
377   return resolved_path;
378 }