2000-03-16 18:25:47 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2000-03-16 16:54:42 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2000-03-16 18:25:47 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2000-03-16 16:54:42 +00:00
|
|
|
|
|
|
|
typedef unsigned int uint;
|
|
|
|
|
|
|
|
#if WINDOWS
|
2000-03-16 18:25:47 +00:00
|
|
|
#define IS_SLASH(c) ((c) == '/' || (c) == '\\')
|
|
|
|
#define DEFAULT_SLASH '\\'
|
|
|
|
#define TOKENIZER_STRING "/\\"
|
|
|
|
|
|
|
|
#define IS_ABSOLUTE_PATH(path, len) \
|
|
|
|
(len >= 2 && isalpha(path[0]) && path[1] == ':')
|
|
|
|
|
2000-03-16 18:37:34 +00:00
|
|
|
static int php_check_dots(const char *element, uint n)
|
2000-03-16 18:25:47 +00:00
|
|
|
{
|
|
|
|
while (n-- > 0) if (element[n] != '.') break;
|
|
|
|
|
|
|
|
return (n != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IS_DIRECTORY_UP(element, len) \
|
|
|
|
(len >= 2 && !php_check_dots(element, len))
|
|
|
|
|
|
|
|
#define IS_DIRECTORY_CURRENT(element, len) \
|
|
|
|
(len == 1 && ptr[0] == '.')
|
|
|
|
|
|
|
|
|
2000-03-16 16:54:42 +00:00
|
|
|
#else
|
2000-03-16 18:25:47 +00:00
|
|
|
#define IS_SLASH(c) ((c) == '/')
|
|
|
|
#define DEFAULT_SLASH '/'
|
|
|
|
#define TOKENIZER_STRING "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* default macros */
|
|
|
|
|
|
|
|
#ifndef IS_ABSOLUTE_PATH
|
|
|
|
#define IS_ABSOLUTE_PATH(path, len) \
|
|
|
|
(IS_SLASH(path[0]))
|
2000-03-16 16:54:42 +00:00
|
|
|
#endif
|
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
#ifndef IS_DIRECTORY_UP
|
|
|
|
#define IS_DIRECTORY_UP(element, len) \
|
|
|
|
(len == 2 && memcmp(element, "..", 2) == 0)
|
|
|
|
#endif
|
2000-03-16 16:54:42 +00:00
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
#ifndef IS_DIRECTORY_CURRENT
|
|
|
|
#define IS_DIRECTORY_CURRENT(element, len) \
|
|
|
|
(len == 1 && ptr[0] == '.')
|
|
|
|
#endif
|
2000-03-16 16:54:42 +00:00
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
/* for now */
|
|
|
|
#define IS_DIR_OK(s) (1)
|
|
|
|
|
|
|
|
#ifndef IS_DIR_OK
|
|
|
|
#define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
|
|
|
|
#endif
|
|
|
|
|
2000-03-16 16:54:42 +00:00
|
|
|
typedef struct _cwd_state {
|
|
|
|
char *cwd;
|
|
|
|
uint cwd_length;
|
|
|
|
} cwd_state;
|
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
static int php_is_dir_ok(const cwd_state *state)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-16 16:54:42 +00:00
|
|
|
char *virtual_getcwd(cwd_state *state, uint *length)
|
|
|
|
{
|
|
|
|
if (state->cwd_length == 0) {
|
|
|
|
char *retval;
|
|
|
|
|
|
|
|
*length = 1;
|
|
|
|
retval = (char *) malloc(2);
|
|
|
|
retval[0] = DEFAULT_SLASH;
|
|
|
|
retval[1] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WINDOWS
|
|
|
|
/* If we have something like C: */
|
|
|
|
if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
|
|
|
|
char *retval;
|
|
|
|
|
|
|
|
*length = state->cwd_length+1;
|
|
|
|
retval = (char *) malloc(*length+1);
|
|
|
|
memcpy(retval, state->cwd, *length);
|
|
|
|
retval[*length-1] = DEFAULT_SLASH;
|
|
|
|
retval[*length] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*length = state->cwd_length;
|
|
|
|
return strdup(state->cwd);
|
|
|
|
}
|
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
/* returns 0 for ok, 1 for error */
|
|
|
|
int virtual_chdir(cwd_state *state, char *path)
|
2000-03-16 16:54:42 +00:00
|
|
|
{
|
|
|
|
uint path_length = strlen(path);
|
|
|
|
char *ptr = path;
|
2000-03-16 18:25:47 +00:00
|
|
|
char *tok = NULL;
|
2000-03-16 16:54:42 +00:00
|
|
|
uint ptr_length;
|
2000-03-16 18:25:47 +00:00
|
|
|
cwd_state *old_state;
|
2000-03-16 16:54:42 +00:00
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
if (path_length == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
old_state = (cwd_state *) malloc(sizeof(*old_state));
|
|
|
|
old_state->cwd = strdup(state->cwd);
|
|
|
|
old_state->cwd_length = state->cwd_length;
|
|
|
|
|
|
|
|
if (IS_ABSOLUTE_PATH(path, path_length)) {
|
|
|
|
state->cwd = (char *) malloc(1);
|
2000-03-16 16:54:42 +00:00
|
|
|
state->cwd[0] = '\0';
|
|
|
|
state->cwd_length = 0;
|
2000-03-16 18:25:47 +00:00
|
|
|
#if WINDOWS
|
|
|
|
} else if(IS_SLASH(path[0])) {
|
|
|
|
state->cwd = (char *) malloc(3);
|
|
|
|
memcpy(state->cwd, old_state->cwd, 2);
|
|
|
|
state->cwd[2] = '\0';
|
|
|
|
state->cwd_length = 2;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ptr = strtok_r(path, TOKENIZER_STRING, &tok);
|
|
|
|
while (ptr) {
|
|
|
|
ptr_length = strlen(ptr);
|
|
|
|
|
|
|
|
|
|
|
|
if (IS_DIRECTORY_UP(ptr, ptr_length)) {
|
|
|
|
char save;
|
|
|
|
|
|
|
|
save = '\0';
|
|
|
|
|
|
|
|
#define PREVIOUS state->cwd[state->cwd_length - 1]
|
|
|
|
|
|
|
|
while (IS_ABSOLUTE_PATH(state->cwd, state->cwd_length) &&
|
|
|
|
!IS_SLASH(PREVIOUS)) {
|
|
|
|
save = PREVIOUS;
|
|
|
|
PREVIOUS = '\0';
|
|
|
|
state->cwd_length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ABSOLUTE_PATH(state->cwd, state->cwd_length)) {
|
|
|
|
state->cwd[state->cwd_length++] = save;
|
|
|
|
state->cwd[state->cwd_length] = '\0';
|
|
|
|
} else {
|
|
|
|
PREVIOUS = '\0';
|
|
|
|
state->cwd_length--;
|
2000-03-16 16:54:42 +00:00
|
|
|
}
|
2000-03-16 18:25:47 +00:00
|
|
|
} else if (!IS_DIRECTORY_CURRENT(ptr, ptr_length)) {
|
2000-03-16 16:54:42 +00:00
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+ptr_length+1+1);
|
|
|
|
state->cwd[state->cwd_length] = DEFAULT_SLASH;
|
|
|
|
memcpy(&state->cwd[state->cwd_length+1], ptr, ptr_length+1);
|
|
|
|
state->cwd_length += (ptr_length+1);
|
|
|
|
}
|
2000-03-16 18:25:47 +00:00
|
|
|
ptr = strtok_r(NULL, TOKENIZER_STRING, &tok);
|
2000-03-16 16:54:42 +00:00
|
|
|
}
|
2000-03-16 18:25:47 +00:00
|
|
|
|
|
|
|
if (!IS_DIR_OK(state)) {
|
|
|
|
free(state->cwd);
|
|
|
|
|
|
|
|
state->cwd = old_state->cwd;
|
2000-03-16 18:41:02 +00:00
|
|
|
state->cwd_length = old_state->cwd_length;
|
2000-03-16 18:25:47 +00:00
|
|
|
|
|
|
|
free(old_state);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2000-03-16 16:54:42 +00:00
|
|
|
}
|
|
|
|
|
2000-03-16 18:25:47 +00:00
|
|
|
void main(void)
|
2000-03-16 16:54:42 +00:00
|
|
|
{
|
|
|
|
cwd_state state;
|
|
|
|
uint length;
|
2000-03-16 18:25:47 +00:00
|
|
|
|
|
|
|
#if !WINDOWS
|
|
|
|
state.cwd = malloc(PATH_MAX + 1);
|
|
|
|
state.cwd_length = PATH_MAX;
|
|
|
|
|
|
|
|
while (getcwd(state.cwd, state.cwd_length) == NULL && errno == ERANGE) {
|
|
|
|
state.cwd_length <<= 1;
|
|
|
|
state.cwd = realloc(state.cwd, state.cwd_length + 1);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
state.cwd = strdup("d:\\foo\\bar");
|
|
|
|
state.cwd_length = strlen(state.cwd);
|
|
|
|
#endif
|
2000-03-16 16:54:42 +00:00
|
|
|
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
|
|
|
virtual_chdir(&state, strdup("/foo/barbara"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
|
|
|
virtual_chdir(&state, strdup("../andi"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
2000-03-16 18:25:47 +00:00
|
|
|
virtual_chdir(&state, strdup("../../..../.././.../foo/...../"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
2000-03-16 16:54:42 +00:00
|
|
|
virtual_chdir(&state, strdup("/////andi"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
|
|
|
virtual_chdir(&state, strdup("andi/././././././////bar"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
|
|
|
virtual_chdir(&state, strdup("../andi/../bar"));
|
|
|
|
printf("%s\n", virtual_getcwd(&state, &length));
|
|
|
|
}
|