pread_write.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /*
  8. * Based in part on the files
  9. * ./sysdeps/unix/sysv/linux/pwrite.c,
  10. * ./sysdeps/unix/sysv/linux/pread.c,
  11. * sysdeps/posix/pread.c
  12. * sysdeps/posix/pwrite.c
  13. * from GNU libc 2.2.5, but reworked considerably...
  14. */
  15. #include "syscalls.h"
  16. #include <unistd.h>
  17. #include <stdint.h>
  18. #ifdef __NR_pread
  19. # define __NR___syscall_pread __NR_pread
  20. static inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  21. size_t, count, off_t, offset_hi, off_t, offset_lo);
  22. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  23. {
  24. return(__syscall_pread(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
  25. }
  26. weak_alias(__libc_pread,pread)
  27. # ifdef __UCLIBC_HAS_LFS__
  28. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  29. {
  30. uint32_t low = offset & 0xffffffff;
  31. uint32_t high = offset >> 32;
  32. return(__syscall_pread(fd, buf, count, __LONG_LONG_PAIR (high, low)));
  33. }
  34. weak_alias(__libc_pread64,pread64)
  35. # endif /* __UCLIBC_HAS_LFS__ */
  36. #endif /* __NR_pread */
  37. #ifdef __NR_pwrite
  38. # define __NR___syscall_pwrite __NR_pwrite
  39. static inline _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  40. size_t, count, off_t, offset_hi, off_t, offset_lo);
  41. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  42. {
  43. return(__syscall_pwrite(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
  44. }
  45. weak_alias(__libc_pwrite,pwrite)
  46. # ifdef __UCLIBC_HAS_LFS__
  47. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  48. {
  49. uint32_t low = offset & 0xffffffff;
  50. uint32_t high = offset >> 32;
  51. return(__syscall_pwrite(fd, buf, count, __LONG_LONG_PAIR (high, low)));
  52. }
  53. weak_alias(__libc_pwrite64,pwrite64)
  54. # endif /* __UCLIBC_HAS_LFS__ */
  55. #endif /* __NR_pwrite */
  56. #if ! defined __NR_pread || ! defined __NR_pwrite
  57. libc_hidden_proto(read)
  58. libc_hidden_proto(write)
  59. libc_hidden_proto(lseek)
  60. libc_hidden_proto(lseek64)
  61. static ssize_t __fake_pread_write(int fd, void *buf,
  62. size_t count, off_t offset, int do_pwrite)
  63. {
  64. int save_errno;
  65. ssize_t result;
  66. off_t old_offset;
  67. /* Since we must not change the file pointer preserve the
  68. * value so that we can restore it later. */
  69. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  70. return -1;
  71. /* Set to wanted position. */
  72. if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
  73. return -1;
  74. if (do_pwrite==1) {
  75. /* Write the data. */
  76. result = write(fd, buf, count);
  77. } else {
  78. /* Read the data. */
  79. result = read(fd, buf, count);
  80. }
  81. /* Now we have to restore the position. If this fails we
  82. * have to return this as an error. */
  83. save_errno = errno;
  84. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  85. {
  86. if (result == -1)
  87. __set_errno(save_errno);
  88. return -1;
  89. }
  90. __set_errno(save_errno);
  91. return(result);
  92. }
  93. # ifdef __UCLIBC_HAS_LFS__
  94. static ssize_t __fake_pread_write64(int fd, void *buf,
  95. size_t count, off64_t offset, int do_pwrite)
  96. {
  97. int save_errno;
  98. ssize_t result;
  99. off64_t old_offset;
  100. /* Since we must not change the file pointer preserve the
  101. * value so that we can restore it later. */
  102. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  103. return -1;
  104. /* Set to wanted position. */
  105. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  106. return -1;
  107. if (do_pwrite==1) {
  108. /* Write the data. */
  109. result = write(fd, buf, count);
  110. } else {
  111. /* Read the data. */
  112. result = read(fd, buf, count);
  113. }
  114. /* Now we have to restore the position. */
  115. save_errno = errno;
  116. if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
  117. if (result == -1)
  118. __set_errno (save_errno);
  119. return -1;
  120. }
  121. __set_errno (save_errno);
  122. return result;
  123. }
  124. # endif /* __UCLIBC_HAS_LFS__ */
  125. #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
  126. #ifndef __NR_pread
  127. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  128. {
  129. return(__fake_pread_write(fd, buf, count, offset, 0));
  130. }
  131. weak_alias(__libc_pread,pread)
  132. # ifdef __UCLIBC_HAS_LFS__
  133. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  134. {
  135. return(__fake_pread_write64(fd, buf, count, offset, 0));
  136. }
  137. weak_alias(__libc_pread64,pread64)
  138. # endif /* __UCLIBC_HAS_LFS__ */
  139. #endif /* ! __NR_pread */
  140. #ifndef __NR_pwrite
  141. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  142. {
  143. /* we won't actually be modifying the buffer,
  144. *just cast it to get rid of warnings */
  145. return(__fake_pread_write(fd, (void*)buf, count, offset, 1));
  146. }
  147. weak_alias(__libc_pwrite,pwrite)
  148. # ifdef __UCLIBC_HAS_LFS__
  149. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  150. {
  151. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  152. }
  153. weak_alias(__libc_pwrite64,pwrite64)
  154. # endif /* __UCLIBC_HAS_LFS__ */
  155. #endif /* ! __NR_pwrite */