pread_write.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* vi: set sw=4 ts=4:
  2. *
  3. * Copyright (C) 2002 by Erik Andersen <andersen@uclibc.org>
  4. * Based in part on the files
  5. * ./sysdeps/unix/sysv/linux/pwrite.c,
  6. * ./sysdeps/unix/sysv/linux/pread.c,
  7. * sysdeps/posix/pread.c
  8. * sysdeps/posix/pwrite.c
  9. * from GNU libc 2.2.5, but reworked considerably...
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU Library General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  19. * for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <features.h>
  26. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64
  27. #undef _FILE_OFFSET_BITS
  28. #define _FILE_OFFSET_BITS 64
  29. #endif
  30. #ifndef __USE_LARGEFILE64
  31. # define __USE_LARGEFILE64 1
  32. #endif
  33. /* We absolutely do _NOT_ want interfaces silently
  34. * renamed under us or very bad things will happen... */
  35. #ifdef __USE_FILE_OFFSET64
  36. # undef __USE_FILE_OFFSET64
  37. #endif
  38. #include <errno.h>
  39. #include <sys/types.h>
  40. #include <sys/syscall.h>
  41. #define _XOPEN_SOURCE 500
  42. #include <unistd.h>
  43. #ifdef __NR_pread
  44. #define __NR___syscall_pread __NR_pread
  45. static inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf, size_t, count,
  46. __off_t, offset_hi, __off_t, offset_lo);
  47. #endif
  48. #ifdef __NR_pwrite
  49. #define __NR___syscall_pwrite __NR_pwrite
  50. static inline _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf, size_t, count,
  51. __off_t, offset_hi, __off_t, offset_lo);
  52. #endif
  53. static ssize_t __fake_pread_write(int fd, void *buf,
  54. size_t count, __off_t offset, int do_pwrite)
  55. {
  56. int save_errno;
  57. ssize_t result;
  58. off_t old_offset;
  59. /* Since we must not change the file pointer preserve the
  60. * value so that we can restore it later. */
  61. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  62. return -1;
  63. /* Set to wanted position. */
  64. if (lseek (fd, offset, SEEK_SET) == (__off_t) -1)
  65. return -1;
  66. if (do_pwrite==1) {
  67. /* Write the data. */
  68. result = write(fd, buf, count);
  69. } else {
  70. /* Read the data. */
  71. result = read(fd, buf, count);
  72. }
  73. /* Now we have to restore the position. If this fails we
  74. * have to return this as an error. */
  75. save_errno = errno;
  76. if (lseek(fd, old_offset, SEEK_SET) == (__off_t) -1)
  77. {
  78. if (result == -1)
  79. __set_errno(save_errno);
  80. return -1;
  81. }
  82. __set_errno(save_errno);
  83. return(result);
  84. }
  85. ssize_t __libc_pread(int fd, void *buf, size_t count, __off_t offset)
  86. {
  87. #ifndef __NR_pread
  88. return(__fake_pread_write(fd, buf, count, offset, 0));
  89. #else
  90. ssize_t result;
  91. /* First try the syscall. */
  92. result = __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(0, offset));
  93. /* Bummer. Syscall failed or is missing. Fake it */
  94. if (result == -1 && errno == ENOSYS) {
  95. result = __fake_pread_write(fd, buf, count, offset, 0);
  96. }
  97. #endif
  98. return result;
  99. }
  100. weak_alias (__libc_pread, pread)
  101. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, __off_t offset)
  102. {
  103. #ifndef __NR_pwrite
  104. return(__fake_pread_write(fd, buf, count, offset, 1));
  105. #else
  106. ssize_t result;
  107. /* First try the syscall. */
  108. result = __syscall_pwrite(fd, buf, count, __LONG_LONG_PAIR(0, offset));
  109. /* Bummer. Syscall failed or is missing. Fake it */
  110. if (result == -1 && errno == ENOSYS) {
  111. result = __fake_pread_write(fd, (void*)buf, count, offset, 1);
  112. }
  113. #endif
  114. return result;
  115. }
  116. weak_alias (__libc_pwrite, pwrite)
  117. #if defined __UCLIBC_HAVE_LFS__
  118. static ssize_t __fake_pread_write64(int fd, void *buf,
  119. size_t count, off64_t offset, int do_pwrite)
  120. {
  121. int save_errno;
  122. ssize_t result;
  123. off64_t old_offset;
  124. /* Since we must not change the file pointer preserve the
  125. * value so that we can restore it later. */
  126. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  127. return -1;
  128. /* Set to wanted position. */
  129. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  130. return -1;
  131. if (do_pwrite==1) {
  132. /* Write the data. */
  133. result = write(fd, buf, count);
  134. } else {
  135. /* Read the data. */
  136. result = read(fd, buf, count);
  137. }
  138. /* Now we have to restore the position. */
  139. save_errno = errno;
  140. if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
  141. if (result == -1)
  142. __set_errno (save_errno);
  143. return -1;
  144. }
  145. __set_errno (save_errno);
  146. return result;
  147. }
  148. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  149. {
  150. #ifndef __NR_pread
  151. return(__fake_pread_write64(fd, buf, count, offset, 0));
  152. #else
  153. ssize_t result;
  154. /* First try the syscall. */
  155. result = __syscall_pread(fd, buf, count,
  156. __LONG_LONG_PAIR((__off_t) (offset >> 32), (__off_t) (offset & 0xffffffff)));
  157. /* Bummer. Syscall failed or is missing. Fake it */
  158. if (result == -1 && errno == ENOSYS) {
  159. result = __fake_pread_write64(fd, buf, count, offset, 0);
  160. }
  161. return result;
  162. #endif
  163. }
  164. weak_alias (__libc_pread64, pread64)
  165. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  166. {
  167. #ifndef __NR_pwrite
  168. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  169. #else
  170. ssize_t result;
  171. /* First try the syscall. */
  172. result = __syscall_pwrite(fd, buf, count,
  173. __LONG_LONG_PAIR((__off_t) (offset >> 32), (__off_t) (offset & 0xffffffff)));
  174. /* Bummer. Syscall failed or is missing. Fake it */
  175. if (result == -1 && errno == ENOSYS) {
  176. result = __fake_pread_write64(fd, (void*)buf, count, offset, 1);
  177. }
  178. return result;
  179. #endif
  180. }
  181. weak_alias (__libc_pwrite64, pwrite64)
  182. #endif