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. */
27 #include <sys/types.h>
35 #include <limits.h> /* for PATH_MAX */
37 #include <sys/param.h> /* for MAXPATHLEN */
44 #include <sys/stat.h> /* for S_IFLNK */
48 #define PATH_MAX _POSIX_PATH_MAX
51 #define PATH_MAX MAXPATHLEN
58 #define MAX_READLINKS 32
61 xrealpath (const char *path, char resolved_path [])
63 char copy_path[PATH_MAX];
64 char *new_path = resolved_path;
68 char link_path[PATH_MAX];
72 /* Make a copy of the source path since we may need to modify it. */
73 strcpy(copy_path, path);
75 max_path = copy_path + PATH_MAX - 2;
78 ** In NT we have two different cases: (1) the path name begins
79 ** with a drive letter, e.g., "C:"; and (2) the path name begins
80 ** with just a slash, which roots to the current drive. In the
81 ** first case we are going to leave things alone, in the second
82 ** case we will prepend the drive letter to the given path.
83 ** Note: So far in testing, I'm only seeing case #1, even though
84 ** I've tried to get the other cases to happen.
85 ** August Hill, 31 Aug 1997.
87 ** Check for a driver letter...C:/...
89 if (*(path + 1) == ':')
91 strncpy(new_path, path, 3);
97 ** No drive letter, but a beginning slash? Prepend the drive
100 else if (*path == '/')
102 getcwd(new_path, PATH_MAX - 1);
108 ** Just a path name, prepend the current directory
112 getcwd(new_path, PATH_MAX - 1);
113 new_path += strlen(new_path);
114 if (new_path[-1] != '/')
119 /* If it's a relative pathname use getwd for starters. */
123 getcwd(new_path, PATH_MAX - 1);
127 new_path += strlen(new_path);
128 if (new_path[-1] != '/')
137 /* Expand each slash-separated pathname component. */
138 while (*path != '\0')
140 /* Ignore stray "/". */
150 if (path[1] == '\0' || path[1] == '/')
158 if (path[2] == '\0' || path[2] == '/')
162 /* Ignore ".." at root. */
163 if (new_path == resolved_path + 1)
166 /* Handle ".." by backing up. */
167 while ((--new_path)[-1] != '/')
174 /* Safely copy the next pathname component. */
175 while (*path != '\0' && *path != '/')
179 errno = ENAMETOOLONG;
182 *new_path++ = *path++;
186 /* See if latest pathname component is a symlink. */
188 n = readlink(resolved_path, link_path, PATH_MAX - 1);
192 /* EINVAL means the file exists but isn't a symlink. */
198 /* Protect against infinite loops. */
199 if (readlinks++ > MAX_READLINKS)
205 /* Note: readlink doesn't add the null byte. */
208 if (*link_path == '/')
209 /* Start over for an absolute symlink. */
210 new_path = resolved_path;
212 /* Otherwise back up over this component. */
213 while (*(--new_path) != '/')
216 /* Safe sex check. */
217 if (strlen(path) + n >= PATH_MAX)
219 errno = ENAMETOOLONG;
223 /* Insert symlink contents into path. */
224 strcat(link_path, path);
225 strcpy(copy_path, link_path);
232 /* Delete trailing slash but don't whomp a lone slash. */
233 if (new_path != resolved_path + 1 && new_path[-1] == '/')
236 /* Make sure it's null terminated. */
238 return resolved_path;