annotate src/realpath.c @ 269:b2472a1930f2 r20-5b33

Import from CVS: tag r20-5b33
author cvs
date Mon, 13 Aug 2007 10:27:19 +0200
parents 92f8ad5d0d3f
children 558f606b08ae
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #include <sys/stat.h> /* for S_IFLNK */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #ifndef PATH_MAX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #ifdef _POSIX_VERSION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #define PATH_MAX _POSIX_PATH_MAX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 #ifdef MAXPATHLEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #define PATH_MAX MAXPATHLEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 #define PATH_MAX 1024
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #define MAX_READLINKS 32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 #ifdef __STDC__
205
92f8ad5d0d3f Import from CVS: tag r20-4b1
cvs
parents: 203
diff changeset
62 char *realpath(const char *path, char resolved_path [])
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 char *realpath(path, resolved_path)
205
92f8ad5d0d3f Import from CVS: tag r20-4b1
cvs
parents: 203
diff changeset
65 const char *path;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 char resolved_path [];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 char copy_path[PATH_MAX];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 char link_path[PATH_MAX];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 char *new_path = resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 char *max_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 int readlinks = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 int n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 /* 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
77 strcpy(copy_path, path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 path = copy_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 max_path = copy_path + PATH_MAX - 2;
195
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
80 #ifdef WINDOWSNT
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
81 /*
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
82 ** In NT we have two different cases: (1) the path name begins
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
83 ** 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
84 ** with just a slash, which roots to the current drive. In the
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
85 ** first case we are going to leave things alone, in the second
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
86 ** case we will prepend the drive letter to the given path.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
87 ** 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
88 ** I've tried to get the other cases to happen.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
89 ** August Hill, 31 Aug 1997.
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
90 **
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
91 ** Check for a driver letter...C:/...
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
92 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
93 if (*(path + 1) == ':')
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
94 {
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
95 strncpy(new_path, path, 3);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
96 new_path += 3;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
97 path += 3;
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
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 ** No drive letter, but a beginning slash? Prepend the drive
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
102 ** letter...
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
103 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
104 else if (*path == '/')
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 getcwd(new_path, PATH_MAX - 1);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
107 new_path += 3;
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
108 path++;
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
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 ** Just a path name, prepend the current directory
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
113 */
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
114 else
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 getcwd(new_path, PATH_MAX - 1);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
117 new_path += strlen(new_path);
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
118 if (new_path[-1] != '/')
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
119 *new_path++ = '/';
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
120 }
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 #else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 /* If it's a relative pathname use getwd for starters. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 if (*path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 #ifdef HAVE_GETCWD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 getcwd(new_path, PATH_MAX - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 getwd(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 new_path += strlen(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 if (new_path[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 }
195
a2f645c6b9f8 Import from CVS: tag r20-3b24
cvs
parents: 0
diff changeset
140 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 /* Expand each slash-separated pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 while (*path != '\0')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 /* Ignore stray "/". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 if (*path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
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 /* Ignore ".". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 if (path[1] == '\0' || path[1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (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 if (path[2] == '\0' || path[2] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 path += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 /* Ignore ".." at root. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 if (new_path == resolved_path + 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* Handle ".." by backing up. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 while ((--new_path)[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 /* Safely copy the next pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 while (*path != '\0' && *path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 if (path > max_path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 *new_path++ = *path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 #ifdef S_IFLNK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 /* See if latest pathname component is a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 n = readlink(resolved_path, link_path, PATH_MAX - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 if (n < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /* EINVAL means the file exists but isn't a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 if (errno != EINVAL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 /* Protect against infinite loops. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 if (readlinks++ > MAX_READLINKS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 errno = ELOOP;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 /* Note: readlink doesn't add the null byte. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 link_path[n] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 if (*link_path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 /* Start over for an absolute symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 new_path = resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 /* Otherwise back up over this component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 while (*(--new_path) != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 /* Safe sex check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 if (strlen(path) + n >= PATH_MAX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 /* Insert symlink contents into path. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 strcat(link_path, path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 strcpy(copy_path, link_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 path = copy_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 #endif /* S_IFLNK */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 /* Delete trailing slash but don't whomp a lone slash. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 if (new_path != resolved_path + 1 && new_path[-1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 new_path--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 /* Make sure it's null terminated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 return resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 }