annotate src/realpath.c @ 331:c9ae480b1fff r21-0-63

Import from CVS: tag r21-0-63
author cvs
date Mon, 13 Aug 2007 10:49:07 +0200
parents e11d67e05968
children 8626e4521993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 * realpath.c -- canonicalize pathname by removing symlinks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #if defined(HAVE_UNISTD_H) || defined(STDC_HEADERS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #ifdef _POSIX_VERSION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #include <limits.h> /* for PATH_MAX */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #include <sys/param.h> /* for MAXPATHLEN */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #ifndef STDC_HEADERS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
45 #ifdef WINDOWSNT
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
46 #include <direct.h>
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
47 #endif
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
48
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #include <sys/stat.h> /* for S_IFLNK */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 #ifndef PATH_MAX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #ifdef _POSIX_VERSION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #define PATH_MAX _POSIX_PATH_MAX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #ifdef MAXPATHLEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #define PATH_MAX MAXPATHLEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 #define PATH_MAX 1024
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #define MAX_READLINKS 32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #ifdef __STDC__
284
558f606b08ae Import from CVS: tag r21-0b40
cvs
parents: 205
diff changeset
66 char *xrealpath(const char *path, char resolved_path [])
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #else
284
558f606b08ae Import from CVS: tag r21-0b40
cvs
parents: 205
diff changeset
68 char *xrealpath(path, resolved_path)
205
92f8ad5d0d3f Import from CVS: tag r20-4b1
cvs
parents: 203
diff changeset
69 const char *path;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 char resolved_path [];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 char copy_path[PATH_MAX];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 char *new_path = resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 char *max_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 int readlinks = 0;
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
77 #ifdef S_IFLNK
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
78 char link_path[PATH_MAX];
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 int n;
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 284
diff changeset
80 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 /* Make a copy of the source path since we may need to modify it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 strcpy(copy_path, path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 path = copy_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 max_path = copy_path + PATH_MAX - 2;
195
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
86 #ifdef WINDOWSNT
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
87 /*
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
88 ** In NT we have two different cases: (1) the path name begins
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
89 ** with a drive letter, e.g., "C:"; and (2) the path name begins
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
90 ** with just a slash, which roots to the current drive. In the
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
91 ** first case we are going to leave things alone, in the second
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
92 ** case we will prepend the drive letter to the given path.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
93 ** Note: So far in testing, I'm only seeing case #1, even though
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
94 ** I've tried to get the other cases to happen.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
95 ** August Hill, 31 Aug 1997.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
96 **
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
97 ** Check for a driver letter...C:/...
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
98 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
99 if (*(path + 1) == ':')
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
100 {
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
101 strncpy(new_path, path, 3);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
102 new_path += 3;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
103 path += 3;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
104 }
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
105
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
106 /*
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
107 ** No drive letter, but a beginning slash? Prepend the drive
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
108 ** letter...
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
109 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
110 else if (*path == '/')
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
111 {
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
112 getcwd(new_path, PATH_MAX - 1);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
113 new_path += 3;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
114 path++;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
115 }
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
116
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
117 /*
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
118 ** Just a path name, prepend the current directory
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
119 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
120 else
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
121 {
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
122 getcwd(new_path, PATH_MAX - 1);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
123 new_path += strlen(new_path);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
124 if (new_path[-1] != '/')
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
125 *new_path++ = '/';
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
126 }
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
127
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
128 #else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 /* If it's a relative pathname use getwd for starters. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 if (*path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 #ifdef HAVE_GETCWD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 getcwd(new_path, PATH_MAX - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 getwd(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 new_path += strlen(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 if (new_path[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 }
195
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
146 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 /* Expand each slash-separated pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 while (*path != '\0')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 /* Ignore stray "/". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 if (*path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 if (*path == '.')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* Ignore ".". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (path[1] == '\0' || path[1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 if (path[1] == '.')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (path[2] == '\0' || path[2] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 path += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 /* Ignore ".." at root. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 if (new_path == resolved_path + 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 /* Handle ".." by backing up. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 while ((--new_path)[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 /* Safely copy the next pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 while (*path != '\0' && *path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 if (path > max_path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 *new_path++ = *path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 #ifdef S_IFLNK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /* See if latest pathname component is a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 n = readlink(resolved_path, link_path, PATH_MAX - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 if (n < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 /* EINVAL means the file exists but isn't a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 if (errno != EINVAL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 /* Protect against infinite loops. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 if (readlinks++ > MAX_READLINKS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 errno = ELOOP;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 /* Note: readlink doesn't add the null byte. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 link_path[n] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 if (*link_path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 /* Start over for an absolute symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 new_path = resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 /* Otherwise back up over this component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 while (*(--new_path) != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 /* Safe sex check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 if (strlen(path) + n >= PATH_MAX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 /* Insert symlink contents into path. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 strcat(link_path, path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 strcpy(copy_path, link_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 path = copy_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 #endif /* S_IFLNK */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 /* Delete trailing slash but don't whomp a lone slash. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 if (new_path != resolved_path + 1 && new_path[-1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 new_path--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 /* Make sure it's null terminated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 return resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 }