src/platform_posix.c
changeset 1098 4e86cec1143f
parent 1073 b45193ab2033
child 1106 94c3669d0311
equal deleted inserted replaced
1097:6e67897d4628 1098:4e86cec1143f
   298     return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
   298     return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
   299 } /* __PHYSFS_platformOpenAppend */
   299 } /* __PHYSFS_platformOpenAppend */
   300 
   300 
   301 
   301 
   302 PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
   302 PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
   303                                     PHYSFS_uint32 size, PHYSFS_uint32 count)
   303                                     PHYSFS_uint64 len)
   304 {
   304 {
   305     int fd = *((int *) opaque);
   305     int fd = *((int *) opaque);
   306     int max = size * count;
   306     ssize_t rc = 0;
   307     int rc = read(fd, buffer, max);
   307 
   308 
   308     BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
       
   309 
       
   310     rc = read(fd, buffer, (size_t) len);
       
   311     BAIL_IF_MACRO(rc == -1, strerror(errno), (PHYSFS_sint64) rc);
       
   312     assert(rc >= 0);
       
   313     assert(rc <= len);
       
   314     return (PHYSFS_sint64) rc;
       
   315 } /* __PHYSFS_platformRead */
       
   316 
       
   317 
       
   318 PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
       
   319                                      PHYSFS_uint64 len)
       
   320 {
       
   321     int fd = *((int *) opaque);
       
   322     ssize_t rc = 0;
       
   323 
       
   324     BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
       
   325 
       
   326     rc = write(fd, (void *) buffer, (size_t) len);
   309     BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
   327     BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
   310     assert(rc <= max);
   328     assert(rc >= 0);
   311 
   329     assert(rc <= len);
   312     if ((rc < max) && (size > 1))
   330     return (PHYSFS_sint64) rc;
   313         lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
       
   314 
       
   315     return (rc / size);
       
   316 } /* __PHYSFS_platformRead */
       
   317 
       
   318 
       
   319 PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
       
   320                                      PHYSFS_uint32 size, PHYSFS_uint32 count)
       
   321 {
       
   322     int fd = *((int *) opaque);
       
   323     int max = size * count;
       
   324     int rc = write(fd, (void *) buffer, max);
       
   325 
       
   326     BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
       
   327     assert(rc <= max);
       
   328 
       
   329     if ((rc < max) && (size > 1))
       
   330         lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
       
   331 
       
   332     return (rc / size);
       
   333 } /* __PHYSFS_platformWrite */
   331 } /* __PHYSFS_platformWrite */
   334 
   332 
   335 
   333 
   336 int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
   334 int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
   337 {
   335 {