Skip to content

Latest commit

 

History

History
294 lines (224 loc) · 6.73 KB

win32.c

File metadata and controls

294 lines (224 loc) · 6.73 KB
 
Aug 23, 2001
Aug 23, 2001
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Unix support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if (defined __STRICT_ANSI__)
#define __PHYSFS_DOING_STRICT_ANSI__
#endif
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
const char *__PHYSFS_platformDirSeparator = "\\";
static const char *win32strerror(void)
{
static char msgbuf[255];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
&msgbuf,
0,
NULL
);
return(msgbuf);
} /* win32strerror */
char **__PHYSFS_platformDetectAvailableCDs(void)
{
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1; /* We count the NULL entry. */
char drive_str[4] = "x:\\";
int i;
for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
{
if (GetDriveType(drive_str) == DRIVE_CDROM)
{
char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
if (tmp)
{
retval = tmp;
retval[cd_count-1] = (char *) malloc(4);
if (retval[cd_count-1])
{
strcpy(retval[cd_count-1], drive_str);
cd_count++;
} /* if */
} /* if */
} /* if */
} /* for */
retval[cd_count - 1] = NULL;
return(retval);
} /* __PHYSFS_detectAvailableCDs */
static char *copyEnvironmentVariable(const char *varname)
{
const char *envr = getenv(varname);
char *retval = NULL;
if (envr != NULL)
{
retval = malloc(strlen(envr) + 1);
if (retval != NULL)
strcpy(retval, envr);
} /* if */
return(retval);
} /* copyEnvironmentVariable */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
DWORD buflen = 0;
char *retval = NULL;
char *filepart = NULL;
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
return(NULL);
SearchPath(NULL, argv0, NULL, &buflen, NULL, NULL);
retval = (char *) malloc(buflen);
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
SearchPath(NULL, argv0, NULL, &buflen, retval, &filepart);
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
LPDWORD bufsize = 0;
LPTSTR retval = NULL;
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
retval = (LPTSTR) malloc(bufsize);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
free(retval);
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
return(NULL); /* no user dir on win32. */
} /* __PHYSFS_platformGetUserDir */
int __PHYSFS_platformGetThreadID(void)
{
return((int) GetCurrentThreadId());
} /* __PHYSFS_platformGetThreadID */
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
return(stricmp(x, y));
} /* __PHYSFS_platformStricmp */
int __PHYSFS_platformExists(const char *fname)
{
return(GetFileAttributes(fname) != 0xffffffff);
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
return(0); /* no symlinks on win32. */
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
} /* __PHYSFS_platformIsDirectory */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
char *retval = malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend)
strcpy(retval, prepend);
else
retval[0] = '\0';
strcat(retval, dirName);
if (append)
strcat(retval, append);
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
return(retval);
} /* __PHYSFS_platformCvtToDependent */
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
void __PHYSFS_platformTimeslice(void)
{
Sleep(10);
} /* __PHYSFS_platformTimeslice */
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks)
{
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
HANDLE dir;
WIN32_FIND_DATA ent;
dir = FindFirstFile(dirname, &ent);
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
for (; FineNextFile(dir, &ent) != 0; )
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
if (l == NULL)
break;
l->str = (char *) malloc(strlen(ent.cFileName) + 1);
if (l->str == NULL)
{
free(l);
break;
} /* if */
strcpy(l->str, ent.cFileName);
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
} /* while */
FindClose(dir);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */
int __PHYSFS_platformFileLength(FILE *handle)
{
fpos_t curpos;
int retval;
fgetpos(handle, &curpos);
fseek(handle, 0, SEEK_END);
retval = ftell(handle);
fsetpos(handle, &curpos);
return(retval);
} /* __PHYSFS_platformFileLength */
char *__PHYSFS_platformCurrentDir(void)
{
LPTSTR *retval;
DWORD buflen = 0;
GetCurrentDirectory(&buflen, NULL);
retval = (LPTSTR) malloc(buflen);
GetCurrentDirectory(&buflen, retval);
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
char *__PHYSFS_platformRealPath(const char *path)
{
return(_fullpath(NULL, path, _MAX_PATH));
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
rc = CreateDirectory(path, NULL);
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
/* end of win32.c ... */