pread_write.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /* Based in part on the files
  8. * ./sysdeps/unix/sysv/linux/pwrite.c,
  9. * ./sysdeps/unix/sysv/linux/pread.c,
  10. * sysdeps/posix/pread.c
  11. * sysdeps/posix/pwrite.c
  12. * from GNU libc 2.2.5, but reworked considerably...
  13. */
  14. #include <sys/syscall.h>
  15. #include <unistd.h>
  16. #include <endian.h>
  17. #ifndef __UCLIBC_HAS_LFS__
  18. # define off64_t off_t
  19. #endif
  20. #ifdef __NR_pread64 /* Newer kernels renamed but it's the same. */
  21. # ifdef __NR_pread
  22. # error "__NR_pread and __NR_pread64 both defined???"
  23. # endif
  24. # define __NR_pread __NR_pread64
  25. #endif
  26. #ifdef __NR_pread
  27. extern __typeof(pread) __libc_pread;
  28. # define __NR___syscall_pread __NR_pread
  29. static __inline__ _syscall6(ssize_t, __syscall_pread, int, fd,
  30. void *, buf, size_t, count, int, dummy, 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. extern __typeof(pread64) __libc_pread64;
  38. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  39. {
  40. return(__syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 32, offset)));
  41. }
  42. weak_alias(__libc_pread64,pread64)
  43. # endif /* __UCLIBC_HAS_LFS__ */
  44. #endif /* __NR_pread */
  45. #ifdef __NR_pwrite64 /* Newer kernels renamed but it's the same. */
  46. # ifdef __NR_pwrite
  47. # error "__NR_pwrite and __NR_pwrite64 both defined???"
  48. # endif
  49. # define __NR_pwrite __NR_pwrite64
  50. #endif
  51. #ifdef __NR_pwrite
  52. extern __typeof(pwrite) __libc_pwrite;
  53. # define __NR___syscall_pwrite __NR_pwrite
  54. static __inline__ _syscall6(ssize_t, __syscall_pwrite, int, fd,
  55. const void *, buf, size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  56. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  57. {
  58. return(__syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 31, offset)));
  59. }
  60. weak_alias(__libc_pwrite,pwrite)
  61. # ifdef __UCLIBC_HAS_LFS__
  62. extern __typeof(pwrite64) __libc_pwrite64;
  63. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  64. {
  65. return(__syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 32, offset)));
  66. }
  67. weak_alias(__libc_pwrite64,pwrite64)
  68. # endif /* __UCLIBC_HAS_LFS__ */
  69. #endif /* __NR_pwrite */
  70. #if ! defined __NR_pread || ! defined __NR_pwrite
  71. static ssize_t __fake_pread_write(int fd, void *buf,
  72. size_t count, off_t offset, int do_pwrite)
  73. {
  74. int save_errno;
  75. ssize_t result;
  76. off_t old_offset;
  77. /* Since we must not change the file pointer preserve the
  78. * value so that we can restore it later. */
  79. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  80. return -1;
  81. /* Set to wanted position. */
  82. if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
  83. return -1;
  84. if (do_pwrite == 1) {
  85. /* Write the data. */
  86. result = write(fd, buf, count);
  87. } else {
  88. /* Read the data. */
  89. result = read(fd, buf, count);
  90. }
  91. /* Now we have to restore the position. If this fails we
  92. * have to return this as an error. */
  93. save_errno = errno;
  94. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  95. {
  96. if (result == -1)
  97. __set_errno(save_errno);
  98. return -1;
  99. }
  100. __set_errno(save_errno);
  101. return(result);
  102. }
  103. # ifdef __UCLIBC_HAS_LFS__
  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. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  139. {
  140. return(__fake_pread_write(fd, buf, count, offset, 0));
  141. }
  142. weak_alias(__libc_pread,pread)
  143. # ifdef __UCLIBC_HAS_LFS__
  144. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset);
  145. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  146. {
  147. return(__fake_pread_write64(fd, buf, count, offset, 0));
  148. }
  149. weak_alias(__libc_pread64,pread64)
  150. # endif /* __UCLIBC_HAS_LFS__ */
  151. #endif /* ! __NR_pread */
  152. #ifndef __NR_pwrite
  153. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset);
  154. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  155. {
  156. return(__fake_pread_write(fd, (void*)buf, count, offset, 1));
  157. }
  158. weak_alias(__libc_pwrite,pwrite)
  159. # ifdef __UCLIBC_HAS_LFS__
  160. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset);
  161. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  162. {
  163. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  164. }
  165. weak_alias(__libc_pwrite64,pwrite64)
  166. # endif /* __UCLIBC_HAS_LFS__ */
  167. #endif /* ! __NR_pwrite */