#include <stdlib.h>
#include <ctype.h>
+#include "php_virtual_cwd.h"
+
#ifndef PHP_WIN32
#include <unistd.h>
#endif
#endif
#ifdef PHP_WIN32
-typedef unsigned int uint;
+typedef unsigned int int;
#define strtok_r(a,b,c) strtok((a),(b))
#define IS_SLASH(c) ((c) == '/' || (c) == '\\')
#define DEFAULT_SLASH '\\'
#define COPY_WHEN_ABSOLUTE 2
-static int php_check_dots(const char *element, uint n)
+static int php_check_dots(const char *element, int n)
{
while (n-- > 0) if (element[n] != '.') break;
#define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
#endif
-typedef struct _cwd_state {
- char *cwd;
- uint cwd_length;
-} cwd_state;
-
-typedef int (*verify_path_func)(const cwd_state *);
-
static int php_is_dir_ok(const cwd_state *state)
{
struct stat buf;
return (1);
}
+void virtual_cwd_init()
+{
+ /* Initialize the true global cwd */
+}
-char *virtual_getcwd_ex(cwd_state *state, uint *length)
+char *virtual_getcwd_ex(int *length)
{
+ cwd_state *state;
+ CWDLS_FETCH();
+
+ state = &CWDG(cwd);
+
if (state->cwd_length == 0) {
char *retval;
/* Same semantics as UNIX getcwd() */
char *virtual_getcwd(char *buf, size_t size)
{
- cwd_state state; /* Needs to be a ZTS var such as PG(), just included it so that this will compile */
- uint length;
+ int length;
char *cwd;
- cwd = virtual_getcwd_ex(&state, &length);
+ cwd = virtual_getcwd_ex(&length);
if (buf == NULL) {
return cwd;
/* returns 0 for ok, 1 for error */
int virtual_file_ex(cwd_state *state, char *path, verify_path_func verify_path)
{
- uint path_length = strlen(path);
+ int path_length = strlen(path);
char *ptr = path;
char *tok = NULL;
- uint ptr_length;
+ int ptr_length;
cwd_state *old_state;
int ret = 0;
- uint copy_amount = -1;
+ int copy_amount = -1;
if (path_length == 0)
return (0);
*state = *old_state;
ret = 1;
- } else
+ } else {
free(old_state->cwd);
+ }
free(old_state);
return (ret);
}
-int virtual_chdir(cwd_state *state, char *path)
+int virtual_chdir(char *path)
{
- return virtual_file_ex(state, path, NULL); /* Use NULL right now instead of php_is_dir_ok */
+ CWDLS_FETCH();
+
+ return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok);
}
-int virtual_filepath(cwd_state *state, char *path, char **filepath)
+int virtual_filepath(char *path, char **filepath)
{
- cwd_state new_state = *state;
+ cwd_state new_state;
int retval;
+ CWDLS_FETCH();
+
+ new_state = CWDG(cwd);
+ new_state.cwd = strdup(CWDG(cwd).cwd);
- new_state.cwd = strdup(state->cwd);
retval = virtual_file_ex(&new_state, path, php_is_file_ok);
*filepath = new_state.cwd;
return retval;
}
-FILE *virtual_fopen(cwd_state *state, char *path, const char *mode)
+FILE *virtual_fopen(char *path, const char *mode)
{
- cwd_state new_state = *state;
+ cwd_state new_state;
FILE *f;
int retval;
+ CWDLS_FETCH();
+
+ new_state = CWDG(cwd);
+ new_state.cwd = strdup(CWDG(cwd).cwd);
- new_state.cwd = strdup(state->cwd);
retval = virtual_file_ex(&new_state, path, php_is_file_ok);
if (retval) {
return f;
}
+#if 0
+
main(void)
{
cwd_state state;
- uint length;
+ int length;
#ifndef PHP_WIN32
state.cwd = malloc(PATH_MAX + 1);
return 0;
}
+
+#endif
--- /dev/null
+#ifndef VIRTUAL_CWD_H
+#define VIRTUAL_CWD_H
+
+typedef struct _cwd_state {
+ char *cwd;
+ int cwd_length;
+} cwd_state;
+
+typedef int (*verify_path_func)(const cwd_state *);
+
+void virtual_cwd_init();
+char *virtual_getcwd_ex(int *length);
+char *virtual_getcwd(char *buf, size_t size);
+int virtual_chdir(char *path);
+int virtual_filepath(char *path, char **filepath);
+FILE *virtual_fopen(char *path, const char *mode);
+
+typedef struct _cwd_globals_struct {
+ cwd_state cwd;
+} cwd_globals_struct;
+
+#ifdef ZTS
+# define CWDLS_D cwd_globals_struct *cwd_globals
+# define CWDLS_DC , CWDLS_D
+# define CWDLS_C cwd_globals
+# define CWDLS_CC , CWDLS_C
+# define CWDG(v) (cwd_globals->v)
+# define CWDLS_FETCH() cwd_globals_struct *cwd_globals = ts_resource(cwd_globals_id)
+CWD_API extern int cwd_globals_id;
+#else
+# define CWDLS_D void
+# define CWDLS_DC
+# define CWDLS_C
+# define CWDLS_CC
+# define CWDG(v) (cwd_globals.v)
+# define CWDLS_FETCH()
+extern CWD_API cwd_globals_struct cwd_globals;
+#endif
+
+#endif /* VIRTUAL_CWD_H */