-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirs.c
252 lines (213 loc) · 6.76 KB
/
dirs.c
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
/*
* Hack to enable per user settings for NWN.
*
* Copyright 2004 - David Holland [email protected]
* Copyright 2008 - David Holland [email protected]
*
* Do what you want with the code, just maintain
* the copyright, and give credit
*
* Directory functions.
*
* No, none of this code is particularly efficient, but it
* arguably doesn't have to be, as at worst, it's specifically
* targetted towards NWN, and hopefully you don't have more than
* a few hundred save games..
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <dlfcn.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <libgen.h>
#include <assert.h>
#include "code.h"
/* ipendir() implimentation. */
DIR *opendir(const char *name) {
DIR *dir_master;
DIR *dir_local;
struct dirent64 *dir_entry;
DIRSTACK *dir_stack;
char local_name[PATH_MAX];
char short_name[PATH_MAX];
if( __nwu_opendir == NULL ) {
__nwu_initialize();
}
__nwu_log(NWU_LOG_OPENDIR, "OPENDIR: %s\n", name);
dir_stack = malloc(sizeof(DIRSTACK));
dir_stack->num_elements = 0;
dir_stack->cur_element = 0;
dir_stack->elements = NULL;
dir_stack->buffer = NULL;
if ( ! __nwu_possible((char *)name, short_name) ) {
if( (dir_local = __nwu_opendir(short_name) ) != NULL ) {
while( (dir_entry = __nwu_readdir64( dir_local )) != NULL ) {
__nwu_log( NWU_DEBUG_OPENDIR, "LOCAL: %s\n", dir_entry->d_name);
dir_stack->num_elements = dir_stack->num_elements + __nwu_push( dir_entry, &dir_stack->elements );
}
__nwu_closedir(dir_local);
}
} else {
assert( snprintf(local_name, PATH_MAX, "%s/%s/%s", __nwu_homedir, __nwu_workingdir, short_name) >= 0 );
__nwu_log( NWU_DEBUG_OPENDIR, "OPENDIR: Checking home: %s\n", local_name);
if( (dir_local = __nwu_opendir(local_name) ) != NULL ) {
while( (dir_entry = __nwu_readdir64( dir_local )) != NULL ) {
__nwu_log( NWU_DEBUG_OPENDIR, "LOCAL: %s\n", dir_entry->d_name);
dir_stack->num_elements = dir_stack->num_elements + __nwu_push( dir_entry, &dir_stack->elements );
}
__nwu_closedir(dir_local);
}
__nwu_dump( dir_stack );
assert( snprintf(local_name, PATH_MAX, "%s/%s/%s", __nwu_basedir, __nwu_workingdir, short_name) >= 0 );
__nwu_log( NWU_DEBUG_OPENDIR, "OPENDIR: Checking base: %s\n", local_name);
if( (dir_master = __nwu_opendir(local_name) ) != NULL ) {
while( (dir_entry = __nwu_readdir64( dir_master )) != NULL ) {
__nwu_log(NWU_DEBUG_OPENDIR, "MASTER: %s\n", dir_entry->d_name);
dir_stack->num_elements = dir_stack->num_elements + __nwu_push( dir_entry, &dir_stack->elements );
}
__nwu_closedir(dir_master);
}
}
if( dir_stack->num_elements == 0 ) {
free(dir_stack);
errno = ENOENT;
return(NULL);
}
__nwu_dump(dir_stack);
return((DIR *)dir_stack);
}
struct dirent *readdir(DIR *dir) {
int i;
DIRSTACK *dir_stack;
DIRENTRY *dir_entry;
struct dirent *buffer;
dir_stack = (DIRSTACK *)dir;
if( dir_stack->cur_element >= dir_stack->num_elements ) {
return(NULL);
}
if( dir_stack->buffer == NULL ) {
/* 32bit filesize read, only need 32bit type dirent structure */
buffer = (struct dirent *)malloc(sizeof(struct dirent));
} else {
buffer = (struct dirent *)dir_stack->buffer;
}
dir_entry = dir_stack->elements;
i = 0;
while( i < dir_stack->cur_element && dir_entry != NULL ) {
i++;
dir_entry = dir_entry->next;
}
dir_stack->cur_element ++;
buffer->d_ino = dir_entry->entry->d_ino;
buffer->d_off = dir_entry->entry->d_off;
buffer->d_reclen = dir_entry->entry->d_reclen;
buffer->d_type = dir_entry->entry->d_type;
memcpy(buffer->d_name, dir_entry->entry->d_name, NAME_MAX + 1);
dir_stack->buffer = (struct dirent64 *)buffer;
return( (struct dirent *)dir_stack->buffer );
}
struct dirent64 *readdir64(DIR *dir) {
int i;
DIRSTACK *dir_stack;
DIRENTRY *dir_entry;
dir_stack = (DIRSTACK *)dir;
if( dir_stack->cur_element >= dir_stack->num_elements ) {
return(NULL);
}
if( dir_stack->buffer == NULL ) {
/* 32bit filesize read, only need 32bit type dirent structure */
dir_stack->buffer = (struct dirent64 *)malloc(sizeof(struct dirent64));
}
dir_entry = dir_stack->elements;
i = 0;
while( i < dir_stack->cur_element && dir_entry != NULL ) {
i++;
dir_entry = dir_entry->next;
}
dir_stack->cur_element ++;
dir_stack->buffer->d_ino = dir_entry->entry->d_ino;
dir_stack->buffer->d_off = dir_entry->entry->d_off;
dir_stack->buffer->d_reclen = dir_entry->entry->d_reclen;
dir_stack->buffer->d_type = dir_entry->entry->d_type;
memcpy(dir_stack->buffer->d_name, dir_entry->entry->d_name, NAME_MAX + 1);
return( (struct dirent64 *)dir_stack->buffer );
}
int closedir(DIR *dir) {
DIRSTACK *dir_stack;
DIRENTRY *dir_entry;
DIRENTRY *next;
__nwu_log(NWU_LOG_OPENDIR, "CLOSEDIR()\n");
dir_stack = (DIRSTACK *)dir;
dir_entry = dir_stack->elements;
while( dir_entry != NULL ) {
next = dir_entry->next;
free(dir_entry->entry);
free(dir_entry);
dir_entry = next;
}
if( dir_stack->buffer != NULL ) {
free( dir_stack->buffer );
}
free(dir_stack);
errno = 0;
return(0);
}
int mkdir(const char *pathname, mode_t mode) {
char newpath[PATH_MAX];
int retval;
int tmp_errno;
char short_path[PATH_MAX];
if( __nwu_mkdir == NULL ) {
__nwu_initialize();
}
__nwu_log(NWU_LOG_MKDIR, "MKDIR: %s - %04o\n", pathname, mode);
if( __nwu_possible((char *)pathname, short_path) ) {
assert( snprintf(newpath, PATH_MAX, "%s/%s", __nwu_homedir, short_path) >= 0 );
retval = __nwu_mkdir( newpath, mode );
tmp_errno = errno;
if ( __nwu_exists_master( (char *)short_path ) ) {
errno = EEXIST;
return(-1);
}
errno = tmp_errno;
return(retval);
}
return(__nwu_mkdir( pathname, mode ));
}
int rmdir(const char *pathname) {
char newpath[PATH_MAX];
char short_path[PATH_MAX];
if( __nwu_rmdir == NULL ) {
__nwu_initialize();
}
__nwu_log(NWU_LOG_RMDIR, "RMDIR: %s\n", pathname);
if( __nwu_possible((char *)pathname, short_path) ) {
assert( snprintf(newpath, PATH_MAX, "%s/%s", __nwu_homedir, short_path) >= 0 );
return( __nwu_rmdir( newpath ) );
}
return(__nwu_rmdir( pathname ));
}
/* probably not a particularly good implimentation */
int chdir(const char *path) {
char newpath[PATH_MAX];
char short_path[PATH_MAX];
if( path == NULL || strlen(path) <= 0 ) {
errno = EFAULT;
return(-1);
}
__nwu_log(NWU_LOG_CHDIR, "CHDIR: %s\n", path);
if( __nwu_possible((char *)path, short_path) ) {
assert( snprintf(__nwu_workingdir, PATH_MAX, "%s", short_path) >= 0 );
assert( snprintf(newpath, PATH_MAX, "%s/%s", __nwu_homedir, short_path) >= 0 );
return(0);
}
strcpy(__nwu_workingdir, "");
return(0);
}