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