annotate src/realpath.c @ 90:99da576a67e7 xemacs-20-0

Import from CVS: tag xemacs-20-0
author cvs
date Mon, 13 Aug 2007 09:10:46 +0200
parents 376386a54a3c
children a2f645c6b9f8
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__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 char *realpath(const char *path, char resolved_path [])
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)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 const char *path;
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;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 /* If it's a relative pathname use getwd for starters. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 if (*path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #ifdef HAVE_GETCWD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 getcwd(new_path, PATH_MAX - 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 getwd(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 new_path += strlen(new_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 if (new_path[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 /* Expand each slash-separated pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 while (*path != '\0')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 /* Ignore stray "/". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 if (*path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 if (*path == '.')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 /* Ignore ".". */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 if (path[1] == '\0' || path[1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 if (path[1] == '.')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 if (path[2] == '\0' || path[2] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 path += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 /* Ignore ".." at root. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 if (new_path == resolved_path + 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 /* Handle ".." by backing up. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 while ((--new_path)[-1] != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 /* Safely copy the next pathname component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 while (*path != '\0' && *path != '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 if (path > max_path)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 return NULL;
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++ = *path++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 #ifdef S_IFLNK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 /* See if latest pathname component is a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 n = readlink(resolved_path, link_path, PATH_MAX - 1);
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 (n < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 /* EINVAL means the file exists but isn't a symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 if (errno != EINVAL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 return NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* Protect against infinite loops. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (readlinks++ > MAX_READLINKS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 errno = ELOOP;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 return NULL;
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 /* Note: readlink doesn't add the null byte. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 link_path[n] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 if (*link_path == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* Start over for an absolute symlink. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 new_path = resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 /* Otherwise back up over this component. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 while (*(--new_path) != '/')
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 /* Safe sex check. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 if (strlen(path) + n >= PATH_MAX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 errno = ENAMETOOLONG;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 return NULL;
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 /* Insert symlink contents into path. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 strcat(link_path, path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 strcpy(copy_path, link_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 path = copy_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 #endif /* S_IFLNK */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 *new_path++ = '/';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 /* Delete trailing slash but don't whomp a lone slash. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 if (new_path != resolved_path + 1 && new_path[-1] == '/')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 new_path--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 /* Make sure it's null terminated. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 *new_path = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 return resolved_path;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 }