summaryrefslogtreecommitdiff
path: root/main/php_virtual_cwd.c
blob: 1086fa05d80e6a7cb7054c0d01f41a641b5b756f (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>

#ifndef PHP_WIN32
#include <unistd.h>
#endif

typedef unsigned int uint;

#ifdef PHP_WIN32
#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] == ':')

static int php_check_dots(const char *element, uint n) 
{
	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] == '.')

#define strtok_r(a, b, c) strtok((a), (b)) /* Temporary fix for Windows */

#else
#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]))
#endif

#ifndef IS_DIRECTORY_UP
#define IS_DIRECTORY_UP(element, len) \
	(len == 2 && memcmp(element, "..", 2) == 0)
#endif

#ifndef IS_DIRECTORY_CURRENT
#define IS_DIRECTORY_CURRENT(element, len) \
	(len == 1 && ptr[0] == '.')
#endif

/* for now */
#define IS_DIR_OK(s) (1)
	
#ifndef IS_DIR_OK
#define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
#endif
	
typedef struct _cwd_state {
	char *cwd;
	uint cwd_length;
} cwd_state;

#ifndef PHP_WIN32
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);
}
#endif


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;
	}

#ifdef PHP_WIN32
	/* 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);
}

/* returns 0 for ok, 1 for error */
int virtual_chdir(cwd_state *state, char *path)
{
	uint path_length = strlen(path);
	char *ptr = path;
	char *tok = NULL;
	uint ptr_length;
	cwd_state *old_state;

	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);
		state->cwd[0] = '\0';
		state->cwd_length = 0;
#ifdef PHP_WIN32
	} 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--;
			}
		} else if (!IS_DIRECTORY_CURRENT(ptr, ptr_length)) {
			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);
		}
		ptr = strtok_r(NULL, TOKENIZER_STRING, &tok);
	}

	if (!IS_DIR_OK(state)) {
		free(state->cwd);

		state->cwd = old_state->cwd;
		state->cwd_length = old_state->cwd_length;

		free(old_state);

		return (1);
	}
	
	free(old_state->cwd);
	free(old_state);
	return (0);
}

void main(void)
{
	cwd_state state;
	uint length;

#ifndef PHP_WIN32
	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

	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));
	virtual_chdir(&state, strdup("../../..../.././.../foo/...../"));
	printf("%s\n", virtual_getcwd(&state, &length));
	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));
}