pread_write.c 5.2 KB

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