pread_write.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #define _GNU_SOURCE
  26. #define _LARGEFILE64_SOURCE
  27. #include <features.h>
  28. #undef __OPTIMIZE__
  29. /* We absolutely do _NOT_ want interfaces silently
  30. * * * renamed under us or very bad things will happen... */
  31. #ifdef __USE_FILE_OFFSET64
  32. # undef __USE_FILE_OFFSET64
  33. #endif
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <sys/syscall.h>
  37. #include <unistd.h>
  38. #include <stdint.h>
  39. #ifdef __UCLIBC_HAS_LFS__
  40. libc_hidden_proto(lseek64)
  41. #else
  42. libc_hidden_proto(lseek)
  43. #endif
  44. #ifdef __NR_pread
  45. #define __NR___syscall_pread __NR_pread
  46. static inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  47. size_t, count, off_t, offset_hi, off_t, offset_lo);
  48. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  49. {
  50. return(__syscall_pread(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
  51. }
  52. strong_alias(__libc_pread,pread)
  53. #if defined __UCLIBC_HAS_LFS__
  54. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  55. {
  56. uint32_t low = offset & 0xffffffff;
  57. uint32_t high = offset >> 32;
  58. return(__syscall_pread(fd, buf, count, __LONG_LONG_PAIR (high, low)));
  59. }
  60. strong_alias(__libc_pread64,pread64)
  61. #endif /* __UCLIBC_HAS_LFS__ */
  62. #endif /* __NR_pread */
  63. #ifdef __NR_pwrite
  64. #define __NR___syscall_pwrite __NR_pwrite
  65. static inline _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  66. size_t, count, off_t, offset_hi, off_t, offset_lo);
  67. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  68. {
  69. return(__syscall_pwrite(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
  70. }
  71. strong_alias(__libc_pwrite,pwrite)
  72. #if defined __UCLIBC_HAS_LFS__
  73. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  74. {
  75. uint32_t low = offset & 0xffffffff;
  76. uint32_t high = offset >> 32;
  77. return(__syscall_pwrite(fd, buf, count, __LONG_LONG_PAIR (high, low)));
  78. }
  79. strong_alias(__libc_pwrite64,pwrite64)
  80. #endif /* __UCLIBC_HAS_LFS__ */
  81. #endif /* __NR_pwrite */
  82. #if ! defined __NR_pread || ! defined __NR_pwrite
  83. static ssize_t __fake_pread_write(int fd, void *buf,
  84. size_t count, off_t offset, int do_pwrite)
  85. {
  86. int save_errno;
  87. ssize_t result;
  88. off_t old_offset;
  89. /* Since we must not change the file pointer preserve the
  90. * value so that we can restore it later. */
  91. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  92. return -1;
  93. /* Set to wanted position. */
  94. if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
  95. return -1;
  96. if (do_pwrite==1) {
  97. /* Write the data. */
  98. result = __write(fd, buf, count);
  99. } else {
  100. /* Read the data. */
  101. result = __read(fd, buf, count);
  102. }
  103. /* Now we have to restore the position. If this fails we
  104. * have to return this as an error. */
  105. save_errno = errno;
  106. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  107. {
  108. if (result == -1)
  109. __set_errno(save_errno);
  110. return -1;
  111. }
  112. __set_errno(save_errno);
  113. return(result);
  114. }
  115. #if defined __UCLIBC_HAS_LFS__
  116. static ssize_t __fake_pread_write64(int fd, void *buf,
  117. size_t count, off64_t offset, int do_pwrite)
  118. {
  119. int save_errno;
  120. ssize_t result;
  121. off64_t old_offset;
  122. /* Since we must not change the file pointer preserve the
  123. * value so that we can restore it later. */
  124. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  125. return -1;
  126. /* Set to wanted position. */
  127. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  128. return -1;
  129. if (do_pwrite==1) {
  130. /* Write the data. */
  131. result = __write(fd, buf, count);
  132. } else {
  133. /* Read the data. */
  134. result = __read(fd, buf, count);
  135. }
  136. /* Now we have to restore the position. */
  137. save_errno = errno;
  138. if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
  139. if (result == -1)
  140. __set_errno (save_errno);
  141. return -1;
  142. }
  143. __set_errno (save_errno);
  144. return result;
  145. }
  146. #endif /* __UCLIBC_HAS_LFS__ */
  147. #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
  148. #ifndef __NR_pread
  149. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  150. {
  151. return(__fake_pread_write(fd, buf, count, offset, 0));
  152. }
  153. strong_alias(__libc_pread,pread)
  154. #if defined __UCLIBC_HAS_LFS__
  155. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  156. {
  157. return(__fake_pread_write64(fd, buf, count, offset, 0));
  158. }
  159. strong_alias(__libc_pread64,pread64)
  160. #endif /* __UCLIBC_HAS_LFS__ */
  161. #endif /* ! __NR_pread */
  162. #ifndef __NR_pwrite
  163. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  164. {
  165. /* we won't actually be modifying the buffer,
  166. *just cast it to get rid of warnings */
  167. return(__fake_pread_write(fd, (void*)buf, count, offset, 1));
  168. }
  169. strong_alias(__libc_pwrite,pwrite)
  170. #if defined __UCLIBC_HAS_LFS__
  171. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  172. {
  173. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  174. }
  175. strong_alias(__libc_pwrite64,pwrite64)
  176. #endif /* __UCLIBC_HAS_LFS__ */
  177. #endif /* ! __NR_pwrite */