pread_write.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <sys/syscall.h>
  16. #include <unistd.h>
  17. #include <stdint.h>
  18. #include <endian.h>
  19. extern __typeof(pread) __libc_pread;
  20. extern __typeof(pwrite) __libc_pwrite;
  21. #ifdef __UCLIBC_HAS_LFS__
  22. extern __typeof(pread64) __libc_pread64;
  23. extern __typeof(pwrite64) __libc_pwrite64;
  24. #endif
  25. #include <bits/kernel_types.h>
  26. #ifdef __NR_pread64 /* Newer kernels renamed but it's the same. */
  27. # ifdef __NR_pread
  28. # error "__NR_pread and __NR_pread64 both defined???"
  29. # endif
  30. # define __NR_pread __NR_pread64
  31. #endif
  32. #ifdef __NR_pread
  33. # define __NR___syscall_pread __NR_pread
  34. /* On Xtensa, 64-bit values are aligned in even/odd register pairs. */
  35. static __inline__ _syscall6(ssize_t, __syscall_pread, int, fd, void *, buf,
  36. size_t, count, int, pad, off_t, offset_hi, off_t, offset_lo)
  37. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  38. {
  39. return __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 31, offset));
  40. }
  41. weak_alias(__libc_pread,pread)
  42. # ifdef __UCLIBC_HAS_LFS__
  43. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  44. {
  45. uint32_t low = offset & 0xffffffff;
  46. uint32_t high = offset >> 32;
  47. return __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR(high, low));
  48. }
  49. weak_alias(__libc_pread64,pread64)
  50. # endif /* __UCLIBC_HAS_LFS__ */
  51. #endif /* __NR_pread */
  52. #ifdef __NR_pwrite64 /* Newer kernels renamed but it's the same. */
  53. # ifdef __NR_pwrite
  54. # error "__NR_pwrite and __NR_pwrite64 both defined???"
  55. # endif
  56. # define __NR_pwrite __NR_pwrite64
  57. #endif
  58. #ifdef __NR_pwrite
  59. # define __NR___syscall_pwrite __NR_pwrite
  60. /* On Xtensa, 64-bit values are aligned in even/odd register pairs. */
  61. static __inline__ _syscall6(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  62. size_t, count, int, pad, off_t, offset_hi, off_t, offset_lo)
  63. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  64. {
  65. return __syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 31, offset));
  66. }
  67. weak_alias(__libc_pwrite,pwrite)
  68. # ifdef __UCLIBC_HAS_LFS__
  69. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  70. {
  71. uint32_t low = offset & 0xffffffff;
  72. uint32_t high = offset >> 32;
  73. return __syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(high, low));
  74. }
  75. weak_alias(__libc_pwrite64,pwrite64)
  76. # endif /* __UCLIBC_HAS_LFS__ */
  77. #endif /* __NR_pwrite */
  78. #if ! defined __NR_pread || ! defined __NR_pwrite
  79. static ssize_t __fake_pread_write(int fd, void *buf,
  80. size_t count, off_t offset, int do_pwrite)
  81. {
  82. int save_errno;
  83. ssize_t result;
  84. off_t old_offset;
  85. /* Since we must not change the file pointer preserve the
  86. * value so that we can restore it later. */
  87. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  88. return -1;
  89. /* Set to wanted position. */
  90. if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
  91. return -1;
  92. if (do_pwrite == 1) {
  93. /* Write the data. */
  94. result = write(fd, buf, count);
  95. } else {
  96. /* Read the data. */
  97. result = read(fd, buf, count);
  98. }
  99. /* Now we have to restore the position. If this fails we
  100. * have to return this as an error. */
  101. save_errno = errno;
  102. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  103. {
  104. if (result == -1)
  105. __set_errno(save_errno);
  106. return -1;
  107. }
  108. __set_errno(save_errno);
  109. return(result);
  110. }
  111. # ifdef __UCLIBC_HAS_LFS__
  112. static ssize_t __fake_pread_write64(int fd, void *buf,
  113. size_t count, off64_t offset, int do_pwrite)
  114. {
  115. int save_errno;
  116. ssize_t result;
  117. off64_t old_offset;
  118. /* Since we must not change the file pointer preserve the
  119. * value so that we can restore it later. */
  120. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  121. return -1;
  122. /* Set to wanted position. */
  123. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  124. return -1;
  125. if (do_pwrite == 1) {
  126. /* Write the data. */
  127. result = write(fd, buf, count);
  128. } else {
  129. /* Read the data. */
  130. result = read(fd, buf, count);
  131. }
  132. /* Now we have to restore the position. */
  133. save_errno = errno;
  134. if (lseek64(fd, old_offset, SEEK_SET) == (off64_t) -1) {
  135. if (result == -1)
  136. __set_errno (save_errno);
  137. return -1;
  138. }
  139. __set_errno (save_errno);
  140. return result;
  141. }
  142. # endif /* __UCLIBC_HAS_LFS__ */
  143. #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
  144. #ifndef __NR_pread
  145. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  146. {
  147. return __fake_pread_write(fd, buf, count, offset, 0);
  148. }
  149. weak_alias(__libc_pread,pread)
  150. # ifdef __UCLIBC_HAS_LFS__
  151. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  152. {
  153. return __fake_pread_write64(fd, buf, count, offset, 0);
  154. }
  155. weak_alias(__libc_pread64,pread64)
  156. # endif /* __UCLIBC_HAS_LFS__ */
  157. #endif /* ! __NR_pread */
  158. #ifndef __NR_pwrite
  159. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  160. {
  161. /* we won't actually be modifying the buffer,
  162. *just cast it to get rid of warnings */
  163. return __fake_pread_write(fd, (void*)buf, count, offset, 1);
  164. }
  165. weak_alias(__libc_pwrite,pwrite)
  166. # ifdef __UCLIBC_HAS_LFS__
  167. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  168. {
  169. return __fake_pread_write64(fd, (void*)buf, count, offset, 1);
  170. }
  171. weak_alias(__libc_pwrite64,pwrite64)
  172. # endif /* __UCLIBC_HAS_LFS__ */
  173. #endif /* ! __NR_pwrite */