Fixed PHYSFS_openAppend() on Unix.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 03 Apr 2008 05:05:48 +0000
changeset 939 684c583cb586
parent 938 ecbae90ae88d
child 941 788748c90925
Fixed PHYSFS_openAppend() on Unix. Apparently O_APPEND doesn't behave like I thought it did...all these years. :/
CHANGELOG.txt
platform/posix.c
--- a/CHANGELOG.txt	Wed Mar 12 21:42:16 2008 +0000
+++ b/CHANGELOG.txt	Thu Apr 03 05:05:48 2008 +0000
@@ -2,6 +2,7 @@
  * CHANGELOG.
  */
 
+04032008 - Fixed PHYSFS_openAppend() to work as documented on Unix.
 03122008 - Fixed aliasing bug in Windows platform layer (thanks, Dennis!).
 03102008 - Changed some text files from ISO-8859-1 to UTF-8. Replaced all the
            translations with UTF-8 encoded equivalents.
--- a/platform/posix.c	Wed Mar 12 21:42:16 2008 +0000
+++ b/platform/posix.c	Thu Apr 03 05:05:48 2008 +0000
@@ -236,13 +236,26 @@
 
 static void *doOpen(const char *filename, int mode)
 {
+    const int appending = (mode & O_APPEND);
     int fd;
     int *retval;
     errno = 0;
 
+    /* O_APPEND doesn't actually behave as we'd like. */
+    mode &= ~O_APPEND;
+
     fd = open(filename, mode, S_IRUSR | S_IWUSR);
     BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
 
+    if (appending)
+    {
+        if (lseek(fd, 0, SEEK_END) < 0)
+        {
+            close(fd);
+            BAIL_MACRO(strerror(errno), NULL);
+        } /* if */
+    } /* if */
+
     retval = (int *) allocator.Malloc(sizeof (int));
     if (retval == NULL)
     {