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. */
29 #include <sys/types.h>
30 #if defined(HAVE_UNISTD_H) || defined(STDC_HEADERS)
36 #include <limits.h> /* for PATH_MAX */
38 #include <sys/param.h> /* for MAXPATHLEN */
49 #include <sys/stat.h> /* for S_IFLNK */
53 #define PATH_MAX _POSIX_PATH_MAX
56 #define PATH_MAX MAXPATHLEN
63 #define MAX_READLINKS 32
66 char *xrealpath(const char *path, char resolved_path [])
68 char *xrealpath(path, resolved_path)
70 char resolved_path [];
73 char copy_path[PATH_MAX];
74 char *new_path = resolved_path;
78 char link_path[PATH_MAX];
82 /* Make a copy of the source path since we may need to modify it. */
83 strcpy(copy_path, path);
85 max_path = copy_path + PATH_MAX - 2;
88 ** In NT we have two different cases: (1) the path name begins
89 ** with a drive letter, e.g., "C:"; and (2) the path name begins
90 ** with just a slash, which roots to the current drive. In the
91 ** first case we are going to leave things alone, in the second
92 ** case we will prepend the drive letter to the given path.
93 ** Note: So far in testing, I'm only seeing case #1, even though
94 ** I've tried to get the other cases to happen.
95 ** August Hill, 31 Aug 1997.
97 ** Check for a driver letter...C:/...
99 if (*(path + 1) == ':')
101 strncpy(new_path, path, 3);
107 ** No drive letter, but a beginning slash? Prepend the drive
110 else if (*path == '/')
112 getcwd(new_path, PATH_MAX - 1);
118 ** Just a path name, prepend the current directory
122 getcwd(new_path, PATH_MAX - 1);
123 new_path += strlen(new_path);
124 if (new_path[-1] != '/')
129 /* If it's a relative pathname use getwd for starters. */
133 getcwd(new_path, PATH_MAX - 1);
137 new_path += strlen(new_path);
138 if (new_path[-1] != '/')
147 /* Expand each slash-separated pathname component. */
148 while (*path != '\0')
150 /* Ignore stray "/". */
160 if (path[1] == '\0' || path[1] == '/')
168 if (path[2] == '\0' || path[2] == '/')
172 /* Ignore ".." at root. */
173 if (new_path == resolved_path + 1)
176 /* Handle ".." by backing up. */
177 while ((--new_path)[-1] != '/')
184 /* Safely copy the next pathname component. */
185 while (*path != '\0' && *path != '/')
189 errno = ENAMETOOLONG;
192 *new_path++ = *path++;
196 /* See if latest pathname component is a symlink. */
198 n = readlink(resolved_path, link_path, PATH_MAX - 1);
202 /* EINVAL means the file exists but isn't a symlink. */
208 /* Protect against infinite loops. */
209 if (readlinks++ > MAX_READLINKS)
215 /* Note: readlink doesn't add the null byte. */
218 if (*link_path == '/')
219 /* Start over for an absolute symlink. */
220 new_path = resolved_path;
222 /* Otherwise back up over this component. */
223 while (*(--new_path) != '/')
226 /* Safe sex check. */
227 if (strlen(path) + n >= PATH_MAX)
229 errno = ENAMETOOLONG;
233 /* Insert symlink contents into path. */
234 strcat(link_path, path);
235 strcpy(copy_path, link_path);
242 /* Delete trailing slash but don't whomp a lone slash. */
243 if (new_path != resolved_path + 1 && new_path[-1] == '/')
246 /* Make sure it's null terminated. */
248 return resolved_path;