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 if (strpbrk (name, "*?|<>\"") || strlen (name) >= MAX_PATH)
99 /* Find start of filename */
100 lastname = name + strlen (name);
101 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
104 /* Count slashes in unc path */
105 if (ABS_LENGTH (name) == 2)
106 for (tmp = name; *tmp; tmp++)
107 if (IS_DIRECTORY_SEP (*tmp))
110 if (count >= 2 && count < 4)
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);
119 dir_handle = FindFirstFile (name, &find_data);
121 if (res || dir_handle != INVALID_HANDLE_VALUE)
123 if ((len = strlen (find_data.cFileName)) < size)
125 if (strcmp (lastname, find_data.cFileName) == 0)
126 /* Signal that the name is already OK. */
129 memcpy (buf, find_data.cFileName, len + 1);
133 if (!res) FindClose (dir_handle);
139 return err ? -1 : len;
141 #endif /* WIN32_NATIVE || CYGWIN */
144 /* Call readlink and try to find out the correct case for the file. */
146 cygwin_readlink (const char * name, char * buf, int size)
148 int n = readlink (name, buf, size);
149 if (n < 0 && errno == EINVAL)
151 /* The file may exist, but isn't a symlink. Try to find the
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);
161 #ifdef WIN32_FILENAMES
163 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
165 /* Length of start of absolute filename. */
167 win32_abs_start (const char * name)
169 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
170 && IS_DIRECTORY_SEP (name[2]))
172 else if (IS_DIRECTORY_SEP (*name))
173 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
177 #endif /* WIN32_NATIVE */
179 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
181 #define getcwd(buffer, len) getwd (buffer)
185 # if defined (_POSIX_PATH_MAX)
186 # define PATH_MAX _POSIX_PATH_MAX
187 # elif defined (MAXPATHLEN)
188 # define PATH_MAX MAXPATHLEN
190 # define PATH_MAX 1024
194 #define MAX_READLINKS 32
196 char * xrealpath (const char *path, char resolved_path []);
198 xrealpath (const char *path, char resolved_path [])
200 char copy_path[PATH_MAX];
201 char *new_path = resolved_path;
203 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
205 char link_path[PATH_MAX];
207 int abslen = ABS_LENGTH (path);
210 /* Make a copy of the source path since we may need to modify it. */
211 strcpy (copy_path, path);
213 max_path = copy_path + PATH_MAX - 2;
217 #ifdef WIN32_FILENAMES
218 /* Check for c:/... or //server/... */
219 else if (abslen == 3 || abslen == 2)
221 /* Make sure drive letter is lowercased. */
223 *new_path = tolower (*path);
228 /* Coerce directory chars. */
229 while (abslen-- > 0) {
230 if (IS_DIRECTORY_SEP (*path))
231 *new_path++ = DIRECTORY_SEP;
239 /* No drive letter, but a beginning slash? Prepend drive letter. */
240 else if (abslen == 1)
242 getcwd (new_path, PATH_MAX - 1);
246 /* Just a path name, prepend the current directory */
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;
255 /* If it's a relative pathname use getcwd for starters. */
256 else if (abslen == 0)
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;
265 /* Copy first directory sep. May have two on cygwin. */
266 strncpy (new_path, path, abslen);
271 /* Expand each slash-separated pathname component. */
272 while (*path != '\0')
274 /* Ignore stray "/". */
275 if (IS_DIRECTORY_SEP (*path))
284 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
291 if (path[1] == '.' &&
292 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
296 /* Ignore ".." at root. */
297 if (new_path == ABS_START (resolved_path))
300 /* Handle ".." by backing up. */
302 while (!IS_DIRECTORY_SEP (new_path[-1]))
308 /* Safely copy the next pathname component. */
309 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
313 errno = ENAMETOOLONG;
316 *new_path++ = *path++;
319 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
320 /* See if latest pathname component is a symlink. */
322 n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
326 /* EINVAL means the file exists but isn't a symlink. */
328 if (errno != EINVAL && errno != ENOENT)
336 /* Protect against infinite loops. */
337 if (readlinks++ > MAX_READLINKS)
343 /* Note: readlink doesn't add the null byte. */
346 if (ABS_LENGTH (link_path) > 0)
347 /* Start over for an absolute symlink. */
348 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
350 /* Otherwise back up over this component. */
351 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
352 assert (new_path > resolved_path);
354 /* Safe sex check. */
355 if (strlen(path) + n >= PATH_MAX)
357 errno = ENAMETOOLONG;
361 /* Insert symlink contents into path. */
362 strcat(link_path, path);
363 strcpy(copy_path, link_path);
366 #endif /* S_IFLNK || WIN32_NATIVE */
367 *new_path++ = DIRECTORY_SEP;
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]))
374 /* Make sure it's null terminated. */
377 return resolved_path;