40 |
40 |
41 #ifndef _WIN32 |
41 #ifndef _WIN32 |
42 /* ---------- POSIX implementation ---------- */ |
42 /* ---------- POSIX implementation ---------- */ |
43 |
43 |
44 SYS_DIR sys_opendir(const char *path) { |
44 SYS_DIR sys_opendir(const char *path) { |
45 return opendir(path); |
45 DIR *dir = opendir(path); |
|
46 if(!dir) { |
|
47 return NULL; |
|
48 } |
|
49 SysDir *d = malloc(sizeof(SysDir)); |
|
50 d->dir = dir; |
|
51 d->ent = NULL; |
|
52 return d; |
46 } |
53 } |
47 |
54 |
48 SysDirEnt* sys_readdir(SYS_DIR dir) { |
55 SysDirEnt* sys_readdir(SYS_DIR dir) { |
49 struct dirent *ent = readdir(dir); |
56 if(dir->ent) { |
|
57 free(dir->ent->name); |
|
58 free(dir->ent); |
|
59 dir->ent = NULL; |
|
60 } |
|
61 struct dirent *ent = readdir(dir->dir); |
50 if(ent) { |
62 if(ent) { |
51 SysDirEnt *e = malloc(sizeof(SysDirEnt)); |
63 SysDirEnt *e = malloc(sizeof(SysDirEnt)); |
52 e->name = strdup(ent->d_name); |
64 e->name = strdup(ent->d_name); |
|
65 dir->ent = e; |
53 return e; |
66 return e; |
54 } |
67 } |
55 return NULL; |
68 return NULL; |
56 } |
69 } |
57 |
70 |
58 void sys_closedir(SYS_DIR dir) { |
71 void sys_closedir(SYS_DIR dir) { |
59 closedir(dir); |
72 closedir(dir->dir); |
|
73 if(dir->ent) { |
|
74 free(dir->ent->name); |
|
75 free(dir->ent); |
|
76 } |
|
77 free(dir); |
60 } |
78 } |
61 |
79 |
62 FILE* sys_fopen(const char *path, const char *mode) { |
80 FILE* sys_fopen(const char *path, const char *mode) { |
63 return fopen(path, mode); |
81 return fopen(path, mode); |
64 } |
82 } |
65 |
83 |
66 int sys_stat(const char *path, SYS_STAT *s) { |
84 int sys_stat(const char *path, SYS_STAT *s) { |
67 return stat(path, s); |
85 return stat(path, s); |
|
86 } |
|
87 |
|
88 int sys_rename(const char *oldpath, const char *newpath) { |
|
89 return rename(oldpath, newpath); |
68 } |
90 } |
69 |
91 |
70 |
92 |
71 #else |
93 #else |
72 /* ---------- Windows implementation ---------- */ |
94 /* ---------- Windows implementation ---------- */ |
178 int ret = _wstat64(fpath, s); |
211 int ret = _wstat64(fpath, s); |
179 free(fpath); |
212 free(fpath); |
180 return ret; |
213 return ret; |
181 } |
214 } |
182 |
215 |
|
216 int sys_rename(const char *oldpath, const char *newpath) { |
|
217 wchar_t *o = path2winpath(oldpath, FALSE, NULL); |
|
218 wchara_t *n = path2winpath(newpath, FALSE, NULL); |
|
219 if(!o || !n) { |
|
220 return -1; |
|
221 } |
|
222 |
|
223 struct __stat64 s; |
|
224 if(!_wstat64(n, &s)) { |
|
225 if(_wunlink(n)) { |
|
226 fprintf(stderr, "sys_rename: cannot delete existing file: %ls\n", n); |
|
227 } |
|
228 } |
|
229 |
|
230 int ret = _wrename(o, n); |
|
231 free(o); |
|
232 free(n); |
|
233 return ret; |
|
234 } |
|
235 |
183 #endif |
236 #endif |