1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Author: Sander Steffann (sander@steffann.nl) |
+----------------------------------------------------------------------+
*/
/* $Id: php3_realpath.c,v 1.14 2000/01/01 04:31:13 sas Exp $ */
#include "php.h"
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/stat.h>
#ifndef MAXSYMLINKS
#define MAXSYMLINKS 32
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif
#if WIN32|WINNT
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif
char *_php3_realpath(char *path, char resolved_path[]);
char *_php3_realpath(char *path, char resolved_path []) {
char path_construction[MAXPATHLEN]; /* We build the result in here */
char *writepos; /* Position to write next char */
char path_copy[MAXPATHLEN]; /* A work-copy of the path */
char *workpos; /* working position in *path */
#if !(WIN32|WINNT)
char buf[MAXPATHLEN]; /* Buffer for readlink */
int linklength; /* The result from readlink */
#endif
int linkcount = 0; /* Count symlinks to avoid loops */
struct stat filestat; /* result from stat */
#if WIN32|WINNT
char *temppos; /* position while counting '.' */
int dotcount; /* number of '.' */
int t; /* counter */
#endif
/* Set the work-position to the beginning of the given path */
strcpy(path_copy, path);
workpos = path_copy;
#if WIN32|WINNT
/* Find out where we start - Windows version */
if ((*workpos == '\\') || (*workpos == '/')) {
/* We start at the root of the current drive */
/* Get the current directory */
if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
/* Unable to get cwd */
resolved_path[0] = 0;
return NULL;
}
/* We only need the first three chars (for example "C:\") */
path_construction[3] = 0;
workpos++;
} else if (workpos[1] == ':') {
/* A drive-letter is specified, copy it */
strncpy(path_construction, path, 2);
strcat(path_construction, "\\");
workpos++;
workpos++;
} else {
/* Use the current directory */
if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
/* Unable to get cwd */
resolved_path[0] = 0;
return NULL;
}
strcat(path_construction, "\\");
}
#else
/* Find out where we start - Unix version */
if (*workpos == '/') {
/* We start at the root */
strcpy(path_construction, "/");
workpos++;
} else {
/* Use the current directory */
if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
/* Unable to get cwd */
resolved_path[0] = 0;
return NULL;
}
strcat(path_construction, "/");
}
#endif
/* Set the next-char-position */
writepos = &path_construction[strlen(path_construction)];
/* Go to the end, then stop */
while(*workpos != 0) {
/* Strip (back)slashes */
while(*workpos == PATH_SEPARATOR) workpos++;
#if WIN32|WINNT
/* reset dotcount */
dotcount = 0;
/* Look for .. */
if ((workpos[0] == '.') && (workpos[1] != 0)) {
/* Windows accepts \...\ as \..\..\, \....\ as \..\..\..\, etc */
/* At least Win98 does */
temppos = workpos;
while(*temppos++ == '.') {
dotcount++;
if ((*temppos != '\\') && (*temppos != 0) && (*temppos != '.')) {
/* This is not a /../ component, but a filename that starts with '.' */
dotcount = 0;
}
}
/* Go back dotcount-1 times */
for (t=0 ; t<(dotcount-1) ; t++) {
workpos++; /* move to next '.' */
/* Can we still go back? */
if ((writepos-3) <= path_construction) return NULL;
/* Go back */
writepos--; /* move to '\' */
while(*--writepos != '\\') ; /* skip until previous '\\' */
}
}
/* No special case */
if (dotcount == 0) {
/* Append */
while((*workpos != '\\') && (*workpos != 0)) {
*writepos++ = *workpos++;
}
}
/* Just one '.', go to next element */
if (dotcount == 1) {
while((*workpos != '\\') && (*workpos != 0)) {
*workpos++;
}
/* Avoid double \ in the result */
writepos--;
}
/* If it was a directory, append a slash */
if (*workpos == '\\') {
*writepos++ = *workpos++;
}
*writepos = 0;
#else /* WIN32|WINNT */
/* Look for .. */
if ((workpos[0] == '.') && (workpos[1] != 0)) {
if ((workpos[1] == '.') && ((workpos[2] == '/') || (workpos[2] == 0))) {
/* One directory back */
/* Set pointers to right position */
workpos++; /* move to second '.' */
workpos++; /* move to '/' */
/* Only apply .. if not in root */
if ((writepos-1) > path_construction) {
writepos--; /* move to '/' */
while(*--writepos != '/') ; /* skip until previous '/' */
}
} else {
if (workpos[1] == '/') {
/* Found a /./ skip it */
workpos++; /* move to '/' */
/* Avoid double / in the result */
writepos--;
} else {
/* No special case, the name just started with a . */
/* Append */
while((*workpos != '/') && (*workpos != 0)) {
*writepos++ = *workpos++;
}
}
}
} else {
/* No special case */
/* Append */
while((*workpos != '/') && (*workpos != 0)) {
*writepos++ = *workpos++;
}
}
#ifdef HAVE_SYMLINK
/* We are going to use path_construction, so close it */
*writepos = 0;
/* Check the current location to see if it is a symlink */
if((linklength = readlink(path_construction, buf, MAXPATHLEN)) != -1) {
/* Check linkcount */
if (linkcount > MAXSYMLINKS) return NULL;
/* Count this symlink */
linkcount++;
/* Set end of buf */
buf[linklength] = 0;
/* Check for overflow */
if ((strlen(workpos) + strlen(buf) + 1) >= MAXPATHLEN) return NULL;
/* Remove the symlink-component wrom path_construction */
writepos--; /* move to '/' */
while(*--writepos != '/') ; /* skip until previous '/' */
*++writepos = 0; /* end of string after '/' */
/* If the symlink starts with a '/', empty path_construction */
if (*buf == '/') {
*path_construction = 0;
writepos = path_construction;
}
/* Insert symlink into path_copy */
strcat(buf, workpos);
strcpy(path_copy, buf);
workpos = path_copy;
}
#endif /* HAVE_SYMLINK */
/* If it was a directory, append a slash */
if (*workpos == '/') {
*writepos++ = *workpos++;
}
*writepos = 0;
#endif /* WIN32|WINNT */
}
/* We are only interested in the directory */
/* Check if the resolved path is a directory */
if (stat(path_construction, &filestat) == 0) {
/* path_construction exists and stats available */
if (S_ISDIR(filestat.st_mode)) {
/* It's a directory, append a / or \ if needed */
if (*(writepos-1) != PATH_SEPARATOR) {
/* Check for overflow */
if ((strlen(workpos) + 2) >= MAXPATHLEN) return NULL;
*writepos++ = PATH_SEPARATOR;
*writepos = 0;
}
} else {
/* it's a file, strip filename */
while (*--writepos != PATH_SEPARATOR);
*++writepos = 0;
}
} else {
/* it doesn't exist, try to strip the last element */
/* if last element ends with a /, skip that / */
if (*(writepos-1) == PATH_SEPARATOR) {
*--writepos = 0;
}
while (*--writepos != PATH_SEPARATOR);
*writepos = 0;
/* Now try this */
if (stat(path_construction, &filestat) == 0) {
/* path_construction exists and stats available */
if (S_ISDIR(filestat.st_mode)) {
/* It's a directory, append a / if needed */
if (*(writepos-1) != PATH_SEPARATOR) {
/* Check for overflow */
if ((strlen(workpos) + 2) >= MAXPATHLEN) return NULL;
*writepos++ = PATH_SEPARATOR;
*writepos = 0;
}
} else {
/* User tries to use something that is not a directory as
a directory */
return NULL;
}
} else {
/* Even this doesn't exist */
return NULL;
}
}
strcpy(resolved_path, path_construction);
return resolved_path;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|