pread_write.c 4.8 KB

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