Browse Source

Fixed PHYSFS_openAppend() on Unix.

Apparently O_APPEND doesn't behave like I thought it did...all these years.  :/
Ryan C. Gordon 18 years ago
parent
commit
61749966b9
2 changed files with 14 additions and 0 deletions
  1. 1 0
      CHANGELOG.txt
  2. 13 0
      platform/posix.c

+ 1 - 0
CHANGELOG.txt

@@ -2,6 +2,7 @@
  * CHANGELOG.
  * CHANGELOG.
  */
  */
 
 
+04032008 - Fixed PHYSFS_openAppend() to work as documented on Unix.
 03122008 - Fixed aliasing bug in Windows platform layer (thanks, Dennis!).
 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
 03102008 - Changed some text files from ISO-8859-1 to UTF-8. Replaced all the
            translations with UTF-8 encoded equivalents.
            translations with UTF-8 encoded equivalents.

+ 13 - 0
platform/posix.c

@@ -236,13 +236,26 @@ int __PHYSFS_platformMkDir(const char *path)
 
 
 static void *doOpen(const char *filename, int mode)
 static void *doOpen(const char *filename, int mode)
 {
 {
+    const int appending = (mode & O_APPEND);
     int fd;
     int fd;
     int *retval;
     int *retval;
     errno = 0;
     errno = 0;
 
 
+    /* O_APPEND doesn't actually behave as we'd like. */
+    mode &= ~O_APPEND;
+
     fd = open(filename, mode, S_IRUSR | S_IWUSR);
     fd = open(filename, mode, S_IRUSR | S_IWUSR);
     BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
     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));
     retval = (int *) allocator.Malloc(sizeof (int));
     if (retval == NULL)
     if (retval == NULL)
     {
     {