Merged r939:940 from trunk: Fix PHYSFS_openAppend() on Unix. stable-1.0
authorRyan C. Gordon <icculus@icculus.org>
Thu, 03 Apr 2008 05:07:35 +0000
branchstable-1.0
changeset 940 a624957f6288
parent 935 99e85221ab05
child 949 8badfbbb3b27
Merged r939:940 from trunk: Fix PHYSFS_openAppend() on Unix.
CHANGELOG
platform/posix.c
--- a/CHANGELOG	Wed Mar 12 21:21:40 2008 +0000
+++ b/CHANGELOG	Thu Apr 03 05:07:35 2008 +0000
@@ -4,6 +4,7 @@
 
 -- stuff in the stable-1.0 branch, backported from 2.0.0 dev branch, etc ---
 
+04032008 - Fixed PHYSFS_openAppend() to work as documented on Unix.
 03122008 - Removed a FIXME.
 03112008 - Fixed wrong array index in Windows platform layer (thanks, James!).
 03082008 - Fixed compiler warnings in Windows platform layer (thanks, Dennis!).
--- a/platform/posix.c	Wed Mar 12 21:21:40 2008 +0000
+++ b/platform/posix.c	Thu Apr 03 05:07:35 2008 +0000
@@ -344,13 +344,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 *) malloc(sizeof (int));
     if (retval == NULL)
     {