2 * Posix-esque support routines for PhysicsFS.
4 * Please see the file LICENSE.txt in the source's root directory.
6 * This file written by Ryan C. Gordon.
9 #define __PHYSICSFS_INTERNAL__
10 #include "physfs_platforms.h"
12 #ifdef PHYSFS_PLATFORM_POSIX
19 #include <sys/types.h>
26 #ifdef PHYSFS_HAVE_LLSEEK
27 #include <linux/unistd.h>
30 #include "physfs_internal.h"
33 const char *__PHYSFS_platformDirSeparator = "/";
36 char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
38 const char *envr = getenv(varname);
43 retval = (char *) allocator.Malloc(strlen(envr) + 1);
49 } /* __PHYSFS_platformCopyEnvironmentVariable */
52 static char *getUserNameByUID(void)
59 if ((pw != NULL) && (pw->pw_name != NULL))
61 retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
63 strcpy(retval, pw->pw_name);
67 } /* getUserNameByUID */
70 static char *getUserDirByUID(void)
77 if ((pw != NULL) && (pw->pw_dir != NULL))
79 retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
81 strcpy(retval, pw->pw_dir);
85 } /* getUserDirByUID */
88 char *__PHYSFS_platformGetUserName(void)
90 char *retval = getUserNameByUID();
92 retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
94 } /* __PHYSFS_platformGetUserName */
97 char *__PHYSFS_platformGetUserDir(void)
99 char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
101 /* if the environment variable was set, make sure it's really a dir. */
105 if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0))
107 allocator.Free(retval);
113 retval = getUserDirByUID();
116 } /* __PHYSFS_platformGetUserDir */
119 int __PHYSFS_platformExists(const char *fname)
122 BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
124 } /* __PHYSFS_platformExists */
127 int __PHYSFS_platformIsSymLink(const char *fname)
130 BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
131 return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 );
132 } /* __PHYSFS_platformIsSymlink */
135 int __PHYSFS_platformIsDirectory(const char *fname)
138 BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0);
139 return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 );
140 } /* __PHYSFS_platformIsDirectory */
143 char *__PHYSFS_platformCvtToDependent(const char *prepend,
147 int len = ((prepend) ? strlen(prepend) : 0) +
148 ((append) ? strlen(append) : 0) +
150 char *retval = (char *) allocator.Malloc(len);
152 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
154 /* platform-independent notation is Unix-style already. :) */
157 strcpy(retval, prepend);
161 strcat(retval, dirName);
164 strcat(retval, append);
167 } /* __PHYSFS_platformCvtToDependent */
171 void __PHYSFS_platformEnumerateFiles(const char *dirname,
173 PHYSFS_EnumFilesCallback callback,
183 if (omitSymLinks) /* !!! FIXME: this malloc sucks. */
185 dlen = strlen(dirname);
186 bufsize = dlen + 256;
187 buf = (char *) allocator.Malloc(bufsize);
190 strcpy(buf, dirname);
191 if (buf[dlen - 1] != '/')
199 dir = opendir(dirname);
206 while ((ent = readdir(dir)) != NULL)
208 if (strcmp(ent->d_name, ".") == 0)
211 if (strcmp(ent->d_name, "..") == 0)
217 int len = strlen(ent->d_name) + dlen + 1;
220 p = (char *) allocator.Realloc(buf, len);
227 strcpy(buf + dlen, ent->d_name);
228 if (__PHYSFS_platformIsSymLink(buf))
232 callback(callbackdata, origdir, ent->d_name);
237 } /* __PHYSFS_platformEnumerateFiles */
240 int __PHYSFS_platformMkDir(const char *path)
244 rc = mkdir(path, S_IRWXU);
245 BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
247 } /* __PHYSFS_platformMkDir */
250 static void *doOpen(const char *filename, int mode)
252 const int appending = (mode & O_APPEND);
257 /* O_APPEND doesn't actually behave as we'd like. */
260 fd = open(filename, mode, S_IRUSR | S_IWUSR);
261 BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
265 if (lseek(fd, 0, SEEK_END) < 0)
268 BAIL_MACRO(strerror(errno), NULL);
272 retval = (int *) allocator.Malloc(sizeof (int));
276 BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
280 return((void *) retval);
284 void *__PHYSFS_platformOpenRead(const char *filename)
286 return(doOpen(filename, O_RDONLY));
287 } /* __PHYSFS_platformOpenRead */
290 void *__PHYSFS_platformOpenWrite(const char *filename)
292 return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
293 } /* __PHYSFS_platformOpenWrite */
296 void *__PHYSFS_platformOpenAppend(const char *filename)
298 return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
299 } /* __PHYSFS_platformOpenAppend */
302 PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
303 PHYSFS_uint32 size, PHYSFS_uint32 count)
305 int fd = *((int *) opaque);
306 int max = size * count;
307 int rc = read(fd, buffer, max);
309 BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
312 if ((rc < max) && (size > 1))
313 lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
316 } /* __PHYSFS_platformRead */
319 PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
320 PHYSFS_uint32 size, PHYSFS_uint32 count)
322 int fd = *((int *) opaque);
323 int max = size * count;
324 int rc = write(fd, (void *) buffer, max);
326 BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
329 if ((rc < max) && (size > 1))
330 lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
333 } /* __PHYSFS_platformWrite */
336 int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
338 int fd = *((int *) opaque);
340 #ifdef PHYSFS_HAVE_LLSEEK
341 unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
342 unsigned long offset_low = (pos & 0xFFFFFFFF);
344 int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
345 BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
347 BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
351 } /* __PHYSFS_platformSeek */
354 PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
356 int fd = *((int *) opaque);
357 PHYSFS_sint64 retval;
359 #ifdef PHYSFS_HAVE_LLSEEK
361 int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
362 BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
363 retval = (PHYSFS_sint64) retoffset;
365 retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
366 BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
370 } /* __PHYSFS_platformTell */
373 PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
375 int fd = *((int *) opaque);
377 BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
378 return((PHYSFS_sint64) statbuf.st_size);
379 } /* __PHYSFS_platformFileLength */
382 int __PHYSFS_platformEOF(void *opaque)
384 PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
385 PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
387 } /* __PHYSFS_platformEOF */
390 int __PHYSFS_platformFlush(void *opaque)
392 int fd = *((int *) opaque);
393 BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
395 } /* __PHYSFS_platformFlush */
398 int __PHYSFS_platformClose(void *opaque)
400 int fd = *((int *) opaque);
401 BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
402 allocator.Free(opaque);
404 } /* __PHYSFS_platformClose */
407 int __PHYSFS_platformDelete(const char *path)
409 BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
411 } /* __PHYSFS_platformDelete */
414 PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
417 BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
418 return statbuf.st_mtime;
419 } /* __PHYSFS_platformGetLastModTime */
421 #endif /* PHYSFS_PLATFORM_POSIX */
423 /* end of posix.c ... */