pread_write.c 5.0 KB

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