2 * realpath.c -- canonicalize pathname by removing symlinks
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
6 This file is part of XEmacs.
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
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
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. */
23 /* Synched up with: Not in FSF. */
33 #if defined (HAVE_SYS_PARAM_H) && !defined (WIN32_NATIVE)
34 #include <sys/param.h>
41 #include <sys/stat.h> /* for S_IFLNK */
43 #if defined(WIN32_NATIVE) || defined(CYGWIN)
44 #define WIN32_FILENAMES
47 /* First char after start of absolute filename. */
48 #define ABS_START(name) (name + ABS_LENGTH (name))
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
58 # ifdef WIN32_FILENAMES
59 # define ABS_LENGTH(name) (win32_abs_start (name))
60 static int win32_abs_start (const char * name);
62 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
63 (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
65 # define system_readlink cygwin_readlink
67 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
68 # define system_readlink readlink
70 #endif /* WIN32_NATIVE */
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
79 win32_readlink (const char * name, char * buf, int size)
81 WIN32_FIND_DATA find_data;
82 HANDLE dir_handle = NULL;
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)
99 /* #### can we have escaped wildcards in a Windows filename? */
100 else if (strpbrk (name, "*?"))
102 errno = EINVAL; /* this valid path can't be a symlink */
106 /* Find start of filename */
107 lastname = name + strlen (name);
108 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
111 /* Count slashes in unc path */
112 if (ABS_LENGTH (name) == 2)
113 for (tmp = name; *tmp; tmp++)
114 if (IS_DIRECTORY_SEP (*tmp))
117 if (count >= 2 && count < 4)
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);
126 dir_handle = FindFirstFile (name, &find_data);
128 if (res || dir_handle != INVALID_HANDLE_VALUE)
130 if ((len = strlen (find_data.cFileName)) < size)
132 if (strcmp (lastname, find_data.cFileName) == 0)
133 /* Signal that the name is already OK. */
136 memcpy (buf, find_data.cFileName, len + 1);
140 if (!res) FindClose (dir_handle);
146 return err ? -1 : len;
148 #endif /* WIN32_NATIVE || CYGWIN */
151 /* Call readlink and try to find out the correct case for the file. */
153 cygwin_readlink (const char * name, char * buf, int size)
155 int n = readlink (name, buf, size);
156 if (n < 0 && errno == EINVAL)
158 /* The file may exist, but isn't a symlink. Try to find the
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);
168 #ifdef WIN32_FILENAMES
170 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
172 /* Length of start of absolute filename. */
174 win32_abs_start (const char * name)
176 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
177 && IS_DIRECTORY_SEP (name[2]))
179 else if (IS_DIRECTORY_SEP (*name))
180 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
184 #endif /* WIN32_NATIVE */
186 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
188 #define getcwd(buffer, len) getwd (buffer)
192 # if defined (_POSIX_PATH_MAX)
193 # define PATH_MAX _POSIX_PATH_MAX
194 # elif defined (MAXPATHLEN)
195 # define PATH_MAX MAXPATHLEN
197 # define PATH_MAX 1024
201 #define MAX_READLINKS 32
203 char * xrealpath (const char *path, char resolved_path []);
205 xrealpath (const char *path, char resolved_path [])
207 char copy_path[PATH_MAX];
208 char *new_path = resolved_path;
210 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
212 char link_path[PATH_MAX];
214 int abslen = ABS_LENGTH (path);
217 /* Make a copy of the source path since we may need to modify it. */
218 strcpy (copy_path, path);
220 max_path = copy_path + PATH_MAX - 2;
224 #ifdef WIN32_FILENAMES
225 /* Check for c:/... or //server/... */
226 else if (abslen == 3 || abslen == 2)
228 /* Make sure drive letter is lowercased. */
230 *new_path = tolower (*path);
235 /* Coerce directory chars. */
236 while (abslen-- > 0) {
237 if (IS_DIRECTORY_SEP (*path))
238 *new_path++ = DIRECTORY_SEP;
246 /* No drive letter, but a beginning slash? Prepend drive letter. */
247 else if (abslen == 1)
249 getcwd (new_path, PATH_MAX - 1);
253 /* Just a path name, prepend the current directory */
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;
262 /* If it's a relative pathname use getcwd for starters. */
263 else if (abslen == 0)
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;
272 /* Copy first directory sep. May have two on cygwin. */
273 strncpy (new_path, path, abslen);
278 /* Expand each slash-separated pathname component. */
279 while (*path != '\0')
281 /* Ignore stray "/". */
282 if (IS_DIRECTORY_SEP (*path))
291 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
298 if (path[1] == '.' &&
299 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
303 /* Ignore ".." at root. */
304 if (new_path == ABS_START (resolved_path))
307 /* Handle ".." by backing up. */
309 while (!IS_DIRECTORY_SEP (new_path[-1]))
315 /* Safely copy the next pathname component. */
316 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
320 errno = ENAMETOOLONG;
323 *new_path++ = *path++;
326 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
327 /* See if latest pathname component is a symlink. */
329 n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
333 /* EINVAL means the file exists but isn't a symlink. */
335 if (errno != EINVAL && errno != ENOENT)
343 /* Protect against infinite loops. */
344 if (readlinks++ > MAX_READLINKS)
350 /* Note: readlink doesn't add the null byte. */
353 if (ABS_LENGTH (link_path) > 0)
354 /* Start over for an absolute symlink. */
355 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
357 /* Otherwise back up over this component. */
358 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
359 assert (new_path > resolved_path);
361 /* Safe sex check. */
362 if (strlen(path) + n >= PATH_MAX)
364 errno = ENAMETOOLONG;
368 /* Insert symlink contents into path. */
369 strcat(link_path, path);
370 strcpy(copy_path, link_path);
373 #endif /* S_IFLNK || WIN32_NATIVE */
374 *new_path++ = DIRECTORY_SEP;
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]))
381 /* Make sure it's null terminated. */
384 return resolved_path;