0
|
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 #ifdef HAVE_CONFIG_H
|
|
26 #include <config.h>
|
|
27 #endif
|
|
28
|
|
29 #include <sys/types.h>
|
|
30 #if defined(HAVE_UNISTD_H) || defined(STDC_HEADERS)
|
|
31 #include <unistd.h>
|
|
32 #endif
|
|
33 #include <stdio.h>
|
|
34 #include <string.h>
|
|
35 #ifdef _POSIX_VERSION
|
|
36 #include <limits.h> /* for PATH_MAX */
|
|
37 #else
|
|
38 #include <sys/param.h> /* for MAXPATHLEN */
|
|
39 #endif
|
|
40 #include <errno.h>
|
|
41 #ifndef STDC_HEADERS
|
|
42 extern int errno;
|
|
43 #endif
|
|
44
|
|
45 #include <sys/stat.h> /* for S_IFLNK */
|
|
46
|
|
47 #ifndef PATH_MAX
|
|
48 #ifdef _POSIX_VERSION
|
|
49 #define PATH_MAX _POSIX_PATH_MAX
|
|
50 #else
|
|
51 #ifdef MAXPATHLEN
|
|
52 #define PATH_MAX MAXPATHLEN
|
|
53 #else
|
|
54 #define PATH_MAX 1024
|
|
55 #endif
|
|
56 #endif
|
|
57 #endif
|
|
58
|
|
59 #define MAX_READLINKS 32
|
|
60
|
|
61 #ifdef __STDC__
|
205
|
62 char *realpath(const char *path, char resolved_path [])
|
0
|
63 #else
|
|
64 char *realpath(path, resolved_path)
|
205
|
65 const char *path;
|
0
|
66 char resolved_path [];
|
|
67 #endif
|
|
68 {
|
|
69 char copy_path[PATH_MAX];
|
|
70 char link_path[PATH_MAX];
|
|
71 char *new_path = resolved_path;
|
|
72 char *max_path;
|
|
73 int readlinks = 0;
|
|
74 int n;
|
|
75
|
|
76 /* Make a copy of the source path since we may need to modify it. */
|
|
77 strcpy(copy_path, path);
|
|
78 path = copy_path;
|
|
79 max_path = copy_path + PATH_MAX - 2;
|
195
|
80 #ifdef WINDOWSNT
|
|
81 /*
|
|
82 ** In NT we have two different cases: (1) the path name begins
|
|
83 ** with a drive letter, e.g., "C:"; and (2) the path name begins
|
|
84 ** with just a slash, which roots to the current drive. In the
|
|
85 ** first case we are going to leave things alone, in the second
|
|
86 ** case we will prepend the drive letter to the given path.
|
|
87 ** Note: So far in testing, I'm only seeing case #1, even though
|
|
88 ** I've tried to get the other cases to happen.
|
|
89 ** August Hill, 31 Aug 1997.
|
|
90 **
|
|
91 ** Check for a driver letter...C:/...
|
|
92 */
|
|
93 if (*(path + 1) == ':')
|
|
94 {
|
|
95 strncpy(new_path, path, 3);
|
|
96 new_path += 3;
|
|
97 path += 3;
|
|
98 }
|
|
99
|
|
100 /*
|
|
101 ** No drive letter, but a beginning slash? Prepend the drive
|
|
102 ** letter...
|
|
103 */
|
|
104 else if (*path == '/')
|
|
105 {
|
|
106 getcwd(new_path, PATH_MAX - 1);
|
|
107 new_path += 3;
|
|
108 path++;
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 ** Just a path name, prepend the current directory
|
|
113 */
|
|
114 else
|
|
115 {
|
|
116 getcwd(new_path, PATH_MAX - 1);
|
|
117 new_path += strlen(new_path);
|
|
118 if (new_path[-1] != '/')
|
|
119 *new_path++ = '/';
|
|
120 }
|
|
121
|
|
122 #else
|
0
|
123 /* If it's a relative pathname use getwd for starters. */
|
|
124 if (*path != '/')
|
|
125 {
|
|
126 #ifdef HAVE_GETCWD
|
|
127 getcwd(new_path, PATH_MAX - 1);
|
|
128 #else
|
|
129 getwd(new_path);
|
|
130 #endif
|
|
131 new_path += strlen(new_path);
|
|
132 if (new_path[-1] != '/')
|
|
133 *new_path++ = '/';
|
|
134 }
|
|
135 else
|
|
136 {
|
|
137 *new_path++ = '/';
|
|
138 path++;
|
|
139 }
|
195
|
140 #endif
|
0
|
141 /* Expand each slash-separated pathname component. */
|
|
142 while (*path != '\0')
|
|
143 {
|
|
144 /* Ignore stray "/". */
|
|
145 if (*path == '/')
|
|
146 {
|
|
147 path++;
|
|
148 continue;
|
|
149 }
|
|
150
|
|
151 if (*path == '.')
|
|
152 {
|
|
153 /* Ignore ".". */
|
|
154 if (path[1] == '\0' || path[1] == '/')
|
|
155 {
|
|
156 path++;
|
|
157 continue;
|
|
158 }
|
|
159
|
|
160 if (path[1] == '.')
|
|
161 {
|
|
162 if (path[2] == '\0' || path[2] == '/')
|
|
163 {
|
|
164 path += 2;
|
|
165
|
|
166 /* Ignore ".." at root. */
|
|
167 if (new_path == resolved_path + 1)
|
|
168 continue;
|
|
169
|
|
170 /* Handle ".." by backing up. */
|
|
171 while ((--new_path)[-1] != '/')
|
|
172 ;
|
|
173 continue;
|
|
174 }
|
|
175 }
|
|
176 }
|
|
177
|
|
178 /* Safely copy the next pathname component. */
|
|
179 while (*path != '\0' && *path != '/')
|
|
180 {
|
|
181 if (path > max_path)
|
|
182 {
|
|
183 errno = ENAMETOOLONG;
|
|
184 return NULL;
|
|
185 }
|
|
186 *new_path++ = *path++;
|
|
187 }
|
|
188
|
|
189 #ifdef S_IFLNK
|
|
190 /* See if latest pathname component is a symlink. */
|
|
191 *new_path = '\0';
|
|
192 n = readlink(resolved_path, link_path, PATH_MAX - 1);
|
|
193
|
|
194 if (n < 0)
|
|
195 {
|
|
196 /* EINVAL means the file exists but isn't a symlink. */
|
|
197 if (errno != EINVAL)
|
|
198 return NULL;
|
|
199 }
|
|
200 else
|
|
201 {
|
|
202 /* Protect against infinite loops. */
|
|
203 if (readlinks++ > MAX_READLINKS)
|
|
204 {
|
|
205 errno = ELOOP;
|
|
206 return NULL;
|
|
207 }
|
|
208
|
|
209 /* Note: readlink doesn't add the null byte. */
|
|
210 link_path[n] = '\0';
|
|
211
|
|
212 if (*link_path == '/')
|
|
213 /* Start over for an absolute symlink. */
|
|
214 new_path = resolved_path;
|
|
215 else
|
|
216 /* Otherwise back up over this component. */
|
|
217 while (*(--new_path) != '/')
|
|
218 ;
|
|
219
|
|
220 /* Safe sex check. */
|
|
221 if (strlen(path) + n >= PATH_MAX)
|
|
222 {
|
|
223 errno = ENAMETOOLONG;
|
|
224 return NULL;
|
|
225 }
|
|
226
|
|
227 /* Insert symlink contents into path. */
|
|
228 strcat(link_path, path);
|
|
229 strcpy(copy_path, link_path);
|
|
230 path = copy_path;
|
|
231 }
|
|
232 #endif /* S_IFLNK */
|
|
233 *new_path++ = '/';
|
|
234 }
|
|
235
|
|
236 /* Delete trailing slash but don't whomp a lone slash. */
|
|
237 if (new_path != resolved_path + 1 && new_path[-1] == '/')
|
|
238 new_path--;
|
|
239
|
|
240 /* Make sure it's null terminated. */
|
|
241 *new_path = '\0';
|
|
242 return resolved_path;
|
|
243 }
|