pread_write.c 5.0 KB

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