You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.5 KiB

27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.0 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_0.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sander Steffann (sander@steffann.nl) |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #if HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <sys/stat.h>
  23. #ifndef MAXSYMLINKS
  24. #define MAXSYMLINKS 32
  25. #endif
  26. #ifndef S_ISDIR
  27. #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
  28. #endif
  29. char *_php3_realpath(char *path, char resolved_path[]);
  30. char *_php3_realpath(char *path, char resolved_path []) {
  31. char path_construction[MAXPATHLEN]; /* We build the result in here */
  32. char *writepos; /* Position to write next char */
  33. char path_copy[MAXPATHLEN]; /* A work-copy of the path */
  34. char *workpos; /* working position in *path */
  35. #if !(WIN32|WINNT)
  36. char buf[MAXPATHLEN]; /* Buffer for readlink */
  37. int linklength; /* The result from readlink */
  38. #endif
  39. int linkcount = 0; /* Count symlinks to avoid loops */
  40. struct stat filestat; /* result from stat */
  41. #if WIN32|WINNT
  42. char *temppos; /* position while counting '.' */
  43. int dotcount; /* number of '.' */
  44. int t; /* counter */
  45. #endif
  46. /* Set the work-position to the beginning of the given path */
  47. strcpy(path_copy, path);
  48. workpos = path_copy;
  49. #if WIN32|WINNT
  50. /* Find out where we start - Windows version */
  51. if ((*workpos == '\\') || (*workpos == '/')) {
  52. /* We start at the root of the current drive */
  53. /* Get the current directory */
  54. if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
  55. /* Unable to get cwd */
  56. resolved_path[0] = 0;
  57. return NULL;
  58. }
  59. /* We only need the first three chars (for example "C:\") */
  60. path_construction[3] = 0;
  61. workpos++;
  62. } else if (workpos[1] == ':') {
  63. /* A drive-letter is specified, copy it */
  64. strncpy(path_construction, path, 2);
  65. strcat(path_construction, "\\");
  66. workpos++;
  67. workpos++;
  68. } else {
  69. /* Use the current directory */
  70. if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
  71. /* Unable to get cwd */
  72. resolved_path[0] = 0;
  73. return NULL;
  74. }
  75. strcat(path_construction, "\\");
  76. }
  77. #else
  78. /* Find out where we start - Unix version */
  79. if (*workpos == '/') {
  80. /* We start at the root */
  81. strcpy(path_construction, "/");
  82. workpos++;
  83. } else {
  84. /* Use the current directory */
  85. if (getcwd(path_construction, MAXPATHLEN-1) == NULL) {
  86. /* Unable to get cwd */
  87. resolved_path[0] = 0;
  88. return NULL;
  89. }
  90. strcat(path_construction, "/");
  91. }
  92. #endif
  93. /* Set the next-char-position */
  94. writepos = &path_construction[strlen(path_construction)];
  95. /* Go to the end, then stop */
  96. while(*workpos != 0) {
  97. /* Strip (back)slashes */
  98. #if WIN32|WINNT
  99. while(*workpos == '\\') workpos++;
  100. #else
  101. while(*workpos == '/') workpos++;
  102. #endif
  103. #if WIN32|WINNT
  104. /* reset dotcount */
  105. dotcount = 0;
  106. /* Look for .. */
  107. if ((workpos[0] == '.') && (workpos[1] != 0)) {
  108. /* Windows accepts \...\ as \..\..\, \....\ as \..\..\..\, etc */
  109. /* At least Win98 does */
  110. temppos = workpos;
  111. while(*temppos++ == '.') {
  112. dotcount++;
  113. if ((*temppos != '\\') && (*temppos != 0) && (*temppos != '.')) {
  114. /* This is not a /../ component, but a filename that starts with '.' */
  115. dotcount = 0;
  116. }
  117. }
  118. /* Go back dotcount-1 times */
  119. for (t=0 ; t<(dotcount-1) ; t++) {
  120. workpos++; /* move to next '.' */
  121. /* Can we still go back? */
  122. if ((writepos-3) <= path_construction) return NULL;
  123. /* Go back */
  124. writepos--; /* move to '\' */
  125. while(*--writepos != '\\') ; /* skip until previous '\\' */
  126. }
  127. }
  128. /* No special case */
  129. if (dotcount == 0) {
  130. /* Append */
  131. while((*workpos != '\\') && (*workpos != 0)) {
  132. *writepos++ = *workpos++;
  133. }
  134. }
  135. /* Just one '.', go to next element */
  136. if (dotcount == 1) {
  137. while((*workpos != '\\') && (*workpos != 0)) {
  138. *workpos++;
  139. }
  140. /* Avoid double \ in the result */
  141. writepos--;
  142. }
  143. /* If it was a directory, append a slash */
  144. if (*workpos == '\\') {
  145. *writepos++ = *workpos++;
  146. }
  147. *writepos = 0;
  148. #else /* WIN32|WINNT */
  149. /* Look for .. */
  150. if ((workpos[0] == '.') && (workpos[1] != 0)) {
  151. if ((workpos[1] == '.') && ((workpos[2] == '/') || (workpos[2] == 0))) {
  152. /* One directory back */
  153. /* Set pointers to right position */
  154. workpos++; /* move to second '.' */
  155. workpos++; /* move to '/' */
  156. /* Only apply .. if not in root */
  157. if ((writepos-1) > path_construction) {
  158. writepos--; /* move to '/' */
  159. while(*--writepos != '/') ; /* skip until previous '/' */
  160. }
  161. } else {
  162. if (workpos[1] == '/') {
  163. /* Found a /./ skip it */
  164. workpos++; /* move to '/' */
  165. /* Avoid double / in the result */
  166. writepos--;
  167. } else {
  168. /* No special case, the name just started with a . */
  169. /* Append */
  170. while((*workpos != '/') && (*workpos != 0)) {
  171. *writepos++ = *workpos++;
  172. }
  173. }
  174. }
  175. } else {
  176. /* No special case */
  177. /* Append */
  178. while((*workpos != '/') && (*workpos != 0)) {
  179. *writepos++ = *workpos++;
  180. }
  181. }
  182. #if HAVE_SYMLINK
  183. /* We are going to use path_construction, so close it */
  184. *writepos = 0;
  185. /* Check the current location to see if it is a symlink */
  186. if((linklength = readlink(path_construction, buf, MAXPATHLEN)) != -1) {
  187. /* Check linkcount */
  188. if (linkcount > MAXSYMLINKS) return NULL;
  189. /* Count this symlink */
  190. linkcount++;
  191. /* Set end of buf */
  192. buf[linklength] = 0;
  193. /* Check for overflow */
  194. if ((strlen(workpos) + strlen(buf) + 1) >= MAXPATHLEN) return NULL;
  195. /* Remove the symlink-component wrom path_construction */
  196. writepos--; /* move to '/' */
  197. while(*--writepos != '/') ; /* skip until previous '/' */
  198. *++writepos = 0; /* end of string after '/' */
  199. /* If the symlink starts with a '/', empty path_construction */
  200. if (*buf == '/') {
  201. *path_construction = 0;
  202. writepos = path_construction;
  203. }
  204. /* Insert symlink into path_copy */
  205. strcat(buf, workpos);
  206. strcpy(path_copy, buf);
  207. workpos = path_copy;
  208. }
  209. #endif /* HAVE_SYMLINK */
  210. /* If it was a directory, append a slash */
  211. if (*workpos == '/') {
  212. *writepos++ = *workpos++;
  213. }
  214. *writepos = 0;
  215. #endif /* WIN32|WINNT */
  216. }
  217. /* Check if the resolved path is a directory */
  218. if (stat(path_construction, &filestat) != 0) return NULL;
  219. if (S_ISDIR(filestat.st_mode)) {
  220. /* It's a directory, append a / if needed */
  221. if (*(writepos-1) != '/') {
  222. /* Check for overflow */
  223. if ((strlen(workpos) + 2) >= MAXPATHLEN) return NULL;
  224. *writepos++ = '/';
  225. *writepos = 0;
  226. }
  227. }
  228. strcpy(resolved_path, path_construction);
  229. return resolved_path;
  230. }
  231. /*
  232. * Local variables:
  233. * tab-width: 4
  234. * c-basic-offset: 4
  235. * End:
  236. */