comparison src/realpath.c @ 400:a86b2b5e0111 r21-2-30

Import from CVS: tag r21-2-30
author cvs
date Mon, 13 Aug 2007 11:14:34 +0200
parents 74fd4e045ea6
children de805c49cfc1
comparison
equal deleted inserted replaced
399:376370fb5946 400:a86b2b5e0111
26 26
27 #include <sys/types.h> 27 #include <sys/types.h>
28 #include <stdio.h> 28 #include <stdio.h>
29 #include <string.h> 29 #include <string.h>
30 #include <errno.h> 30 #include <errno.h>
31 #include <limits.h>
32
31 #ifdef HAVE_UNISTD_H 33 #ifdef HAVE_UNISTD_H
32 #include <unistd.h> 34 #include <unistd.h>
33 #endif 35 #endif
34 #ifdef _POSIX_VERSION 36
35 #include <limits.h> /* for PATH_MAX */ 37 #if defined (HAVE_SYS_PARAM_H)
36 #else 38 #include <sys/param.h>
37 #include <sys/param.h> /* for MAXPATHLEN */
38 #endif 39 #endif
39 40
40 #ifdef WINDOWSNT 41 #ifdef WINDOWSNT
41 #include <direct.h> 42 #include <direct.h>
42 #endif 43 #endif
72 char link_path[PATH_MAX]; 73 char link_path[PATH_MAX];
73 int n; 74 int n;
74 #endif 75 #endif
75 76
76 /* Make a copy of the source path since we may need to modify it. */ 77 /* Make a copy of the source path since we may need to modify it. */
77 strcpy(copy_path, path); 78 strcpy (copy_path, path);
78 path = copy_path; 79 path = copy_path;
79 max_path = copy_path + PATH_MAX - 2; 80 max_path = copy_path + PATH_MAX - 2;
80 #ifdef WINDOWSNT 81 #ifdef WINDOWSNT
81 /* 82 /*
82 ** In NT we have two different cases: (1) the path name begins 83 ** In NT we have two different cases: (1) the path name begins
151 { 152 {
152 path++; 153 path++;
153 continue; 154 continue;
154 } 155 }
155 156
156 if (path[1] == '.') 157 /* Handle ".." */
157 { 158 if (path[1] == '.' &&
158 if (path[2] == '\0' || path[2] == '/') 159 (path[2] == '\0' || path[2] == '/'))
159 { 160 {
160 path += 2; 161 path += 2;
161 162
162 /* Ignore ".." at root. */ 163 /* Ignore ".." at root. */
163 if (new_path == resolved_path + 1) 164 if (new_path == resolved_path + 1)
164 continue; 165 continue;
165 166
166 /* Handle ".." by backing up. */ 167 /* Handle ".." by backing up. */
167 while ((--new_path)[-1] != '/') 168 while ((--new_path)[-1] != '/')
168 ; 169 ;
169 continue; 170 continue;
170 }
171 } 171 }
172 } 172 }
173 173
174 /* Safely copy the next pathname component. */ 174 /* Safely copy the next pathname component. */
175 while (*path != '\0' && *path != '/') 175 while (*path != '\0' && *path != '/')
183 } 183 }
184 184
185 #ifdef S_IFLNK 185 #ifdef S_IFLNK
186 /* See if latest pathname component is a symlink. */ 186 /* See if latest pathname component is a symlink. */
187 *new_path = '\0'; 187 *new_path = '\0';
188 n = readlink(resolved_path, link_path, PATH_MAX - 1); 188 n = readlink (resolved_path, link_path, PATH_MAX - 1);
189 189
190 if (n < 0) 190 if (n < 0)
191 { 191 {
192 /* EINVAL means the file exists but isn't a symlink. */ 192 /* EINVAL means the file exists but isn't a symlink. */
193 if (errno != EINVAL) 193 if (errno != EINVAL)