author | Ryan C. Gordon <icculus@icculus.org> |
Mon, 29 Dec 2003 08:50:21 +0000 | |
changeset 622 | c8e67ca63ad6 |
parent 596 | 381b6ca0dd85 |
child 644 | 1cb5533d369c |
child 940 | a624957f6288 |
permissions | -rw-r--r-- |
235 | 1 |
/* |
2 |
* Posix-esque support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon. |
|
7 |
*/ |
|
8 |
||
9 |
#if HAVE_CONFIG_H |
|
10 |
# include <config.h> |
|
11 |
#endif |
|
12 |
||
310
f8bca4a93fd5
Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents:
292
diff
changeset
|
13 |
#if (!defined WIN32) |
f8bca4a93fd5
Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents:
292
diff
changeset
|
14 |
|
235 | 15 |
#if (defined __STRICT_ANSI__) |
16 |
#define __PHYSFS_DOING_STRICT_ANSI__ |
|
17 |
#endif |
|
18 |
||
19 |
/* |
|
20 |
* We cheat a little: I want the symlink version of stat() (lstat), and |
|
21 |
* GCC/Linux will not declare it if compiled with the -ansi flag. |
|
22 |
* If you are really lacking symlink support on your platform, |
|
23 |
* you should #define __PHYSFS_NO_SYMLINKS__ before compiling this |
|
24 |
* file. That will open a security hole, though, if you really DO have |
|
25 |
* symlinks on your platform; it renders PHYSFS_permitSymbolicLinks(0) |
|
26 |
* useless, since every symlink will be reported as a regular file/dir. |
|
27 |
*/ |
|
28 |
#if (defined __PHYSFS_DOING_STRICT_ANSI__) |
|
29 |
#undef __STRICT_ANSI__ |
|
30 |
#endif |
|
31 |
#include <stdio.h> |
|
32 |
#if (defined __PHYSFS_DOING_STRICT_ANSI__) |
|
33 |
#define __STRICT_ANSI__ |
|
34 |
#endif |
|
35 |
||
36 |
#include <stdlib.h> |
|
37 |
#include <string.h> |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
38 |
#include <unistd.h> |
235 | 39 |
#include <ctype.h> |
40 |
#include <sys/types.h> |
|
41 |
#include <sys/stat.h> |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
42 |
#include <pwd.h> |
235 | 43 |
#include <dirent.h> |
44 |
#include <errno.h> |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
45 |
#include <fcntl.h> |
235 | 46 |
|
259
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
47 |
#ifdef PHYSFS_HAVE_LLSEEK |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
48 |
#include <linux/unistd.h> |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
49 |
#endif |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
50 |
|
235 | 51 |
#define __PHYSICSFS_INTERNAL__ |
52 |
#include "physfs_internal.h" |
|
53 |
||
54 |
||
55 |
char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname) |
|
56 |
{ |
|
57 |
const char *envr = getenv(varname); |
|
58 |
char *retval = NULL; |
|
59 |
||
60 |
if (envr != NULL) |
|
61 |
{ |
|
249
fed886543e40
Added a bunch of explicit casts when using malloc().
Ryan C. Gordon <icculus@icculus.org>
parents:
240
diff
changeset
|
62 |
retval = (char *) malloc(strlen(envr) + 1); |
235 | 63 |
if (retval != NULL) |
64 |
strcpy(retval, envr); |
|
65 |
} /* if */ |
|
66 |
||
67 |
return(retval); |
|
68 |
} /* __PHYSFS_platformCopyEnvironmentVariable */ |
|
69 |
||
70 |
||
71 |
static char *getUserNameByUID(void) |
|
72 |
{ |
|
73 |
uid_t uid = getuid(); |
|
74 |
struct passwd *pw; |
|
75 |
char *retval = NULL; |
|
76 |
||
77 |
pw = getpwuid(uid); |
|
78 |
if ((pw != NULL) && (pw->pw_name != NULL)) |
|
79 |
{ |
|
249
fed886543e40
Added a bunch of explicit casts when using malloc().
Ryan C. Gordon <icculus@icculus.org>
parents:
240
diff
changeset
|
80 |
retval = (char *) malloc(strlen(pw->pw_name) + 1); |
235 | 81 |
if (retval != NULL) |
82 |
strcpy(retval, pw->pw_name); |
|
83 |
} /* if */ |
|
84 |
||
85 |
return(retval); |
|
86 |
} /* getUserNameByUID */ |
|
87 |
||
88 |
||
89 |
static char *getUserDirByUID(void) |
|
90 |
{ |
|
91 |
uid_t uid = getuid(); |
|
92 |
struct passwd *pw; |
|
93 |
char *retval = NULL; |
|
94 |
||
95 |
pw = getpwuid(uid); |
|
96 |
if ((pw != NULL) && (pw->pw_dir != NULL)) |
|
97 |
{ |
|
249
fed886543e40
Added a bunch of explicit casts when using malloc().
Ryan C. Gordon <icculus@icculus.org>
parents:
240
diff
changeset
|
98 |
retval = (char *) malloc(strlen(pw->pw_dir) + 1); |
235 | 99 |
if (retval != NULL) |
100 |
strcpy(retval, pw->pw_dir); |
|
101 |
} /* if */ |
|
102 |
||
103 |
return(retval); |
|
104 |
} /* getUserDirByUID */ |
|
105 |
||
106 |
||
107 |
char *__PHYSFS_platformGetUserName(void) |
|
108 |
{ |
|
109 |
char *retval = getUserNameByUID(); |
|
110 |
if (retval == NULL) |
|
111 |
retval = __PHYSFS_platformCopyEnvironmentVariable("USER"); |
|
112 |
return(retval); |
|
113 |
} /* __PHYSFS_platformGetUserName */ |
|
114 |
||
115 |
||
116 |
char *__PHYSFS_platformGetUserDir(void) |
|
117 |
{ |
|
118 |
char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME"); |
|
119 |
if (retval == NULL) |
|
120 |
retval = getUserDirByUID(); |
|
121 |
return(retval); |
|
122 |
} /* __PHYSFS_platformGetUserDir */ |
|
123 |
||
124 |
||
125 |
/* -ansi and -pedantic flags prevent use of strcasecmp() on Linux. */ |
|
126 |
int __PHYSFS_platformStricmp(const char *x, const char *y) |
|
127 |
{ |
|
128 |
int ux, uy; |
|
129 |
||
130 |
do |
|
131 |
{ |
|
132 |
ux = toupper((int) *x); |
|
133 |
uy = toupper((int) *y); |
|
558
a62e8e26dfc9
Minor strcasecmp optimization.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
134 |
if (ux != uy) |
a62e8e26dfc9
Minor strcasecmp optimization.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
135 |
return((ux > uy) ? 1 : -1); |
235 | 136 |
x++; |
137 |
y++; |
|
138 |
} while ((ux) && (uy)); |
|
139 |
||
140 |
return(0); |
|
141 |
} /* __PHYSFS_platformStricmp */ |
|
142 |
||
143 |
||
596
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
144 |
int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len) |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
145 |
{ |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
146 |
int ux, uy; |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
147 |
|
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
148 |
if (!len) |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
149 |
return(0); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
150 |
|
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
151 |
do |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
152 |
{ |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
153 |
ux = toupper((int) *x); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
154 |
uy = toupper((int) *y); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
155 |
if (ux != uy) |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
156 |
return((ux > uy) ? 1 : -1); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
157 |
x++; |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
158 |
y++; |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
159 |
len--; |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
160 |
} while ((ux) && (uy) && (len)); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
161 |
|
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
162 |
return(0); |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
163 |
} /* __PHYSFS_platformStrnicmp */ |
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
164 |
|
381b6ca0dd85
Added internal function __PHYSFS_platformStrnicmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
558
diff
changeset
|
165 |
|
467
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
166 |
#if (defined __PHYSFS_NO_SYMLINKS__) |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
167 |
#define doStat stat |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
168 |
#else |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
169 |
#define doStat lstat |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
170 |
#endif |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
171 |
|
235 | 172 |
int __PHYSFS_platformExists(const char *fname) |
173 |
{ |
|
174 |
struct stat statbuf; |
|
467
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
175 |
BAIL_IF_MACRO(doStat(fname, &statbuf) == -1, strerror(errno), 0); |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
176 |
return(1); |
235 | 177 |
} /* __PHYSFS_platformExists */ |
178 |
||
179 |
||
180 |
int __PHYSFS_platformIsSymLink(const char *fname) |
|
181 |
{ |
|
182 |
#if (defined __PHYSFS_NO_SYMLINKS__) |
|
183 |
return(0); |
|
184 |
#else |
|
185 |
struct stat statbuf; |
|
467
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
186 |
BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0); |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
187 |
return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 ); |
235 | 188 |
#endif |
189 |
} /* __PHYSFS_platformIsSymlink */ |
|
190 |
||
191 |
||
192 |
int __PHYSFS_platformIsDirectory(const char *fname) |
|
193 |
{ |
|
194 |
struct stat statbuf; |
|
467
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
195 |
BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0); |
99664d9842cb
Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
429
diff
changeset
|
196 |
return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 ); |
235 | 197 |
} /* __PHYSFS_platformIsDirectory */ |
198 |
||
199 |
||
200 |
char *__PHYSFS_platformCvtToDependent(const char *prepend, |
|
201 |
const char *dirName, |
|
202 |
const char *append) |
|
203 |
{ |
|
204 |
int len = ((prepend) ? strlen(prepend) : 0) + |
|
205 |
((append) ? strlen(append) : 0) + |
|
206 |
strlen(dirName) + 1; |
|
249
fed886543e40
Added a bunch of explicit casts when using malloc().
Ryan C. Gordon <icculus@icculus.org>
parents:
240
diff
changeset
|
207 |
char *retval = (char *) malloc(len); |
235 | 208 |
|
209 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
210 |
||
211 |
/* platform-independent notation is Unix-style already. :) */ |
|
212 |
||
213 |
if (prepend) |
|
214 |
strcpy(retval, prepend); |
|
215 |
else |
|
216 |
retval[0] = '\0'; |
|
217 |
||
218 |
strcat(retval, dirName); |
|
219 |
||
220 |
if (append) |
|
221 |
strcat(retval, append); |
|
222 |
||
223 |
return(retval); |
|
224 |
} /* __PHYSFS_platformCvtToDependent */ |
|
225 |
||
226 |
||
227 |
||
228 |
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, |
|
229 |
int omitSymLinks) |
|
230 |
{ |
|
362
ac8c846a0a66
Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents:
310
diff
changeset
|
231 |
LinkedStringList *retval = NULL, *p = NULL; |
235 | 232 |
DIR *dir; |
233 |
struct dirent *ent; |
|
234 |
int bufsize = 0; |
|
235 |
char *buf = NULL; |
|
236 |
int dlen = 0; |
|
237 |
||
238 |
if (omitSymLinks) |
|
239 |
{ |
|
240 |
dlen = strlen(dirname); |
|
241 |
bufsize = dlen + 256; |
|
249
fed886543e40
Added a bunch of explicit casts when using malloc().
Ryan C. Gordon <icculus@icculus.org>
parents:
240
diff
changeset
|
242 |
buf = (char *) malloc(bufsize); |
235 | 243 |
BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL); |
244 |
strcpy(buf, dirname); |
|
245 |
if (buf[dlen - 1] != '/') |
|
246 |
{ |
|
247 |
buf[dlen++] = '/'; |
|
248 |
buf[dlen] = '\0'; |
|
249 |
} /* if */ |
|
250 |
} /* if */ |
|
251 |
||
252 |
errno = 0; |
|
253 |
dir = opendir(dirname); |
|
254 |
if (dir == NULL) |
|
255 |
{ |
|
256 |
if (buf != NULL) |
|
257 |
free(buf); |
|
258 |
BAIL_IF_MACRO(1, strerror(errno), NULL); |
|
259 |
} /* if */ |
|
260 |
||
362
ac8c846a0a66
Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents:
310
diff
changeset
|
261 |
while ((ent = readdir(dir)) != NULL) |
235 | 262 |
{ |
263 |
if (strcmp(ent->d_name, ".") == 0) |
|
264 |
continue; |
|
265 |
||
266 |
if (strcmp(ent->d_name, "..") == 0) |
|
267 |
continue; |
|
268 |
||
269 |
if (omitSymLinks) |
|
270 |
{ |
|
271 |
char *p; |
|
272 |
int len = strlen(ent->d_name) + dlen + 1; |
|
273 |
if (len > bufsize) |
|
274 |
{ |
|
275 |
p = realloc(buf, len); |
|
276 |
if (p == NULL) |
|
277 |
continue; |
|
278 |
buf = p; |
|
279 |
bufsize = len; |
|
280 |
} /* if */ |
|
281 |
||
282 |
strcpy(buf + dlen, ent->d_name); |
|
283 |
if (__PHYSFS_platformIsSymLink(buf)) |
|
284 |
continue; |
|
285 |
} /* if */ |
|
286 |
||
362
ac8c846a0a66
Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents:
310
diff
changeset
|
287 |
retval = __PHYSFS_addToLinkedStringList(retval, &p, ent->d_name, -1); |
235 | 288 |
} /* while */ |
289 |
||
290 |
if (buf != NULL) |
|
291 |
free(buf); |
|
292 |
||
293 |
closedir(dir); |
|
294 |
return(retval); |
|
295 |
} /* __PHYSFS_platformEnumerateFiles */ |
|
296 |
||
297 |
||
298 |
char *__PHYSFS_platformCurrentDir(void) |
|
299 |
{ |
|
300 |
int allocSize = 0; |
|
301 |
char *retval = NULL; |
|
302 |
char *ptr; |
|
303 |
||
304 |
do |
|
305 |
{ |
|
306 |
allocSize += 100; |
|
307 |
ptr = (char *) realloc(retval, allocSize); |
|
308 |
if (ptr == NULL) |
|
309 |
{ |
|
310 |
if (retval != NULL) |
|
311 |
free(retval); |
|
312 |
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); |
|
313 |
} /* if */ |
|
314 |
||
315 |
retval = ptr; |
|
316 |
ptr = getcwd(retval, allocSize); |
|
317 |
} while (ptr == NULL && errno == ERANGE); |
|
318 |
||
319 |
if (ptr == NULL && errno) |
|
320 |
{ |
|
321 |
/* |
|
322 |
* getcwd() failed for some reason, for example current |
|
323 |
* directory not existing. |
|
324 |
*/ |
|
325 |
if (retval != NULL) |
|
326 |
free(retval); |
|
327 |
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL); |
|
328 |
} /* if */ |
|
329 |
||
330 |
return(retval); |
|
331 |
} /* __PHYSFS_platformCurrentDir */ |
|
332 |
||
333 |
||
334 |
||
335 |
int __PHYSFS_platformMkDir(const char *path) |
|
336 |
{ |
|
337 |
int rc; |
|
338 |
errno = 0; |
|
339 |
rc = mkdir(path, S_IRWXU); |
|
340 |
BAIL_IF_MACRO(rc == -1, strerror(errno), 0); |
|
341 |
return(1); |
|
342 |
} /* __PHYSFS_platformMkDir */ |
|
343 |
||
344 |
||
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
345 |
static void *doOpen(const char *filename, int mode) |
235 | 346 |
{ |
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
347 |
int fd; |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
348 |
int *retval; |
235 | 349 |
errno = 0; |
350 |
||
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
351 |
fd = open(filename, mode, S_IRUSR | S_IWUSR); |
292
99bf6ef5639a
Patched a bug where doOpen() reported success when the file couldn't be
Ryan C. Gordon <icculus@icculus.org>
parents:
259
diff
changeset
|
352 |
BAIL_IF_MACRO(fd < 0, strerror(errno), NULL); |
235 | 353 |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
354 |
retval = (int *) malloc(sizeof (int)); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
355 |
if (retval == NULL) |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
356 |
{ |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
357 |
close(fd); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
358 |
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
359 |
} /* if */ |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
360 |
|
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
361 |
*retval = fd; |
235 | 362 |
return((void *) retval); |
363 |
} /* doOpen */ |
|
364 |
||
365 |
||
366 |
void *__PHYSFS_platformOpenRead(const char *filename) |
|
367 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
368 |
return(doOpen(filename, O_RDONLY)); |
235 | 369 |
} /* __PHYSFS_platformOpenRead */ |
370 |
||
371 |
||
372 |
void *__PHYSFS_platformOpenWrite(const char *filename) |
|
373 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
374 |
return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC)); |
235 | 375 |
} /* __PHYSFS_platformOpenWrite */ |
376 |
||
377 |
||
378 |
void *__PHYSFS_platformOpenAppend(const char *filename) |
|
379 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
380 |
return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND)); |
235 | 381 |
} /* __PHYSFS_platformOpenAppend */ |
382 |
||
383 |
||
384 |
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer, |
|
385 |
PHYSFS_uint32 size, PHYSFS_uint32 count) |
|
386 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
387 |
int fd = *((int *) opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
388 |
int max = size * count; |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
389 |
int rc = read(fd, buffer, max); |
235 | 390 |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
391 |
BAIL_IF_MACRO(rc == -1, strerror(errno), rc); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
392 |
assert(rc <= max); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
393 |
|
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
394 |
if ((rc < max) && (size > 1)) |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
395 |
lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */ |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
396 |
|
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
397 |
return(rc / size); |
235 | 398 |
} /* __PHYSFS_platformRead */ |
399 |
||
400 |
||
401 |
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer, |
|
402 |
PHYSFS_uint32 size, PHYSFS_uint32 count) |
|
403 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
404 |
int fd = *((int *) opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
405 |
int max = size * count; |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
406 |
int rc = write(fd, (void *) buffer, max); |
235 | 407 |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
408 |
BAIL_IF_MACRO(rc == -1, strerror(errno), rc); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
409 |
assert(rc <= max); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
410 |
|
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
411 |
if ((rc < max) && (size > 1)) |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
412 |
lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */ |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
413 |
|
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
414 |
return(rc / size); |
235 | 415 |
} /* __PHYSFS_platformWrite */ |
416 |
||
417 |
||
418 |
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos) |
|
419 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
420 |
int fd = *((int *) opaque); |
235 | 421 |
|
259
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
422 |
#ifdef PHYSFS_HAVE_LLSEEK |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
423 |
unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
424 |
unsigned long offset_low = (pos & 0xFFFFFFFF); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
425 |
loff_t retoffset; |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
426 |
int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
427 |
BAIL_IF_MACRO(rc == -1, strerror(errno), 0); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
428 |
#else |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
429 |
BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
430 |
#endif |
235 | 431 |
|
432 |
return(1); |
|
433 |
} /* __PHYSFS_platformSeek */ |
|
434 |
||
435 |
||
436 |
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque) |
|
437 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
438 |
int fd = *((int *) opaque); |
259
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
439 |
PHYSFS_sint64 retval; |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
440 |
|
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
441 |
#ifdef PHYSFS_HAVE_LLSEEK |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
442 |
loff_t retoffset; |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
443 |
int rc = llseek(fd, 0, &retoffset, SEEK_CUR); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
444 |
BAIL_IF_MACRO(rc == -1, strerror(errno), -1); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
445 |
retval = (PHYSFS_sint64) retoffset; |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
446 |
#else |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
447 |
retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
448 |
BAIL_IF_MACRO(retval == -1, strerror(errno), -1); |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
449 |
#endif |
bd65a0d74e9a
64-bit _llseek() support for the future.
Ryan C. Gordon <icculus@icculus.org>
parents:
255
diff
changeset
|
450 |
|
235 | 451 |
return(retval); |
452 |
} /* __PHYSFS_platformTell */ |
|
453 |
||
454 |
||
455 |
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque) |
|
456 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
457 |
int fd = *((int *) opaque); |
235 | 458 |
struct stat statbuf; |
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
459 |
BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1); |
235 | 460 |
return((PHYSFS_sint64) statbuf.st_size); |
461 |
} /* __PHYSFS_platformFileLength */ |
|
462 |
||
463 |
||
464 |
int __PHYSFS_platformEOF(void *opaque) |
|
465 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
466 |
PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
467 |
PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
468 |
return(pos >= len); |
235 | 469 |
} /* __PHYSFS_platformEOF */ |
470 |
||
471 |
||
472 |
int __PHYSFS_platformFlush(void *opaque) |
|
473 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
474 |
int fd = *((int *) opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
475 |
BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0); |
235 | 476 |
return(1); |
477 |
} /* __PHYSFS_platformFlush */ |
|
478 |
||
479 |
||
480 |
int __PHYSFS_platformClose(void *opaque) |
|
481 |
{ |
|
251
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
482 |
int fd = *((int *) opaque); |
0f4615263e32
Actual POSIX calls to replace ANSI stdio routines.
Ryan C. Gordon <icculus@icculus.org>
parents:
249
diff
changeset
|
483 |
BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0); |
429
72ca216c756a
Patched memory leaks (thanks, Valgrind!)
Ryan C. Gordon <icculus@icculus.org>
parents:
362
diff
changeset
|
484 |
free(opaque); |
235 | 485 |
return(1); |
486 |
} /* __PHYSFS_platformClose */ |
|
487 |
||
488 |
||
489 |
int __PHYSFS_platformDelete(const char *path) |
|
490 |
{ |
|
491 |
BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0); |
|
492 |
return(1); |
|
493 |
} /* __PHYSFS_platformDelete */ |
|
494 |
||
240
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
495 |
|
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
496 |
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname) |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
497 |
{ |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
498 |
struct stat statbuf; |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
499 |
BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1); |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
500 |
return statbuf.st_mtime; |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
501 |
} /* __PHYSFS_platformGetLastModTime */ |
052041af9001
Added PHYSFS_getLastModTime() API. (Thanks, John Hall!)
Ryan C. Gordon <icculus@icculus.org>
parents:
235
diff
changeset
|
502 |
|
310
f8bca4a93fd5
Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents:
292
diff
changeset
|
503 |
#endif /* !defined WIN32 */ |
f8bca4a93fd5
Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents:
292
diff
changeset
|
504 |
|
235 | 505 |
/* end of posix.c ... */ |
506 |